using System;
class Fruit {
// Constructor เป็นแบบ string พารามิเตอร์
public Fruit(string name) {
Console.WriteLine("Name : " + name);
}
// Constructor เป็นแบบ int พารามิเตอร์
public Fruit(int price) {
Console.WriteLine("Price : " + price);
}
}
class Program {
static void Main() {
Fruit f1 = new Fruit("Apple"); // string
Fruit f2 = new Fruit(20); // int
Console.ReadLine();
}
}
Name : Apple
Price : 20
using System;
class Fruit {
// Constructor เป็นแบบ string,int พารามิเตอร์
public Fruit(string name, int price) {
Console.WriteLine("Name : " + name);
Console.WriteLine("Price : " + price);
}
// Constructor เป็นแบบ int,string พารามิเตอร์
public Fruit(int calories, string color) {
Console.WriteLine("Calories(kcal) : " + calories);
Console.WriteLine("Color : " + color);
}
}
class Program {
static void Main() {
Fruit f1 = new Fruit("Apple",20); // string , int
Fruit f2 = new Fruit(52,"Red"); // int , string
Console.ReadLine();
}
}
Name : Apple
Price : 20
Calories(kcal) : 52
Color : Red
using System;
class Fruit {
// Constructor แบบมี 1 พารามิเตอร์
public Fruit(string name) {
Console.WriteLine("Name : " + name);
}
// Constructor แบบมี 2 พารามิเตอร์
public Fruit(string color, int price) {
Console.WriteLine("Color : " + color);
Console.WriteLine("Price : " + price);
}
}
class Program {
static void Main() {
Fruit f1 = new Fruit("Apple"); // เรียกใช้ Constructor ที่มีพารามิเตอร์ 1 ตัว
Fruit f2 = new Fruit("Red", 20); // เรียกใช้ Constructor ที่มีพารามิเตอร์ 2 ตัว
Console.ReadLine();
}
}
class Fruit {
// Constructor แบบมี 1 พารามิเตอร์
public Fruit(String name) {
System.out.println("Name : " + name);
}
// Constructor แบบมี 2 พารามิเตอร์
public Fruit(String color, int price) {
System.out.println("Color : " + color);
System.out.println("Price : " + price);
}
}
public class Main {
public static void main(String[] args) {
Fruit f1 = new Fruit("Apple"); // เรียกใช้ Constructor ที่มีพารามิเตอร์ 1 ตัว
Fruit f2 = new Fruit("Red", 20); // เรียกใช้ Constructor ที่มีพารามิเตอร์ 2 ตัว
}
}
#include <iostream>
using namespace std;
class Fruit {
public:
// Constructor แบบมี 1 พารามิเตอร์
Fruit(string name) {
cout << "Name: " << name << endl;
}
// Constructor แบบมี 2 พารามิเตอร์
Fruit(string color, int price) {
cout << "Color: " << color << endl;
cout << "Price: " << price << endl;
}
};
int main() {
Fruit f1("Apple"); // เรียกใช้ Constructor ที่มีพารามิเตอร์ 1 ตัว
Fruit f2("Red", 20); // เรียกใช้ Constructor ที่มีพารามิเตอร์ 2 ตัว
}
class Fruit:
def __init__(self, name=None, color=None, price=None):
if name:
print(f"Name : {name}")
if color and price:
print(f"Color : {color}")
print(f"Price : {price}")
# เรียกใช้ Constructor ที่มีพารามิเตอร์ 1 ตัว
f1 = Fruit(name="Apple")
# เรียกใช้ Constructor ที่มีพารามิเตอร์ 2 ตัว
f2 = Fruit(color="Red", price=20)