Java programlama dilinde Class kullanımı ve Constructor(Yapıcı metot kullanımı)
Cars Sınıfı
public class Cars {
//attribute
String color;
int year;
String modelName;
int seatCount;
public Cars()//constructor yapıcı
{
color="White";
year=2000;
seatCount=5;
modelName="Fiat";
}
public Cars(String c,int y) //formal parameter
{
color=c;
year=y;
modelName="Renault";
}
public Cars(int y, String c)
{
color=c;
year=y;
modelName="BMW";
}
public Cars(int y, String c, String m)
{
color=c;
year=y;
modelName=m;
}
}
Cars Sınıfının kullanımı.
public class Unit2Cars {
public static void main(String[] args) {
// TODO Auto-generated method stub
Cars x=new Cars();
x.color="blue";
x.modelName="Ford";
x.seatCount=5;
x.year=2020;
Cars y=new Cars();
y.color="yellow";
y.modelName="Opel";
y.year=2012;
Cars z=new Cars();
z.color="red";
Cars a=new Cars("pink", 2023);//actual parameter
Cars b=new Cars(2023,"black");
Cars c=new Cars(2021, "green", "Mustang");
/*
System.out.println(x.color);
System.out.println(y.color);
System.out.println(z.color);
System.out.println(z.year)*/
System.out.println(a.color);
System.out.println(a.year);
System.out.println(a.modelName);
System.out.println(b.color);
System.out.println(b.year);
System.out.println(b.modelName);
System.out.println(c.modelName);
}
}