Lesson: Constructor
A constructor is a special method that is used to initialize objects.
*** Obyekt yaradilan zaman ilk ishe dushen hisse constructor sayilir.
Ashagidaki misal clean code deyil.
package org.example;
public class Tree {
public float height;
public String type;
public Tree(String newType, float newHeight) {
type = newType;
height = newHeight;
}
public Tree(String newType) {
type = newType;
height = 1.0F;
}
public Tree(float newHeight) {
type = "Pine";
height = newHeight;
}
public Tree() {
type = "Pine";
height = 1.0F;
}
}
DRY:
package org.example;
public class Tree {
public float height;
public String type;
public Tree(String newType, float newHeight) {
type = newType;
height = newHeight;
}
public Tree(String newType) {
// type = newType;
// height = 1.0F;
this(newType, 1.0F);
}
public Tree(float newHeight) {
// type = "Pine";
// height = newHeight;
this("Pine", newHeight);
}
public Tree() {
// type = "Pine";
// height = 1.0F;
this("Pine", 1.0F);
}
}
Tree tree1 = new Tree("Pine", 2.0F);
tree1.printInfo();
Tree tree2 = new Tree("Oak");
tree2.printInfo();
Tree tree3 = new Tree(8.0F);
tree3.printInfo();
Tree tree4 = new Tree();
tree4.printInfo();
-- this() keyword constructor-daki ilk cagrilan hisse olmalidir, eks halda compile time error verecek:
package org.example;
public class Tree {
public float height;
public String type;
public Tree(String newType, float newHeight) {
type = newType;
height = newHeight;
}
public Tree(String newType) {
this(newType, 1.0F);
}
public Tree(float newHeight) {
System.out.println("Hello");
this("Pine", newHeight);
}
public Tree() {
this("Pine", 1.0F);
}
public void printInfo() {
System.out.println("Type is : " + type + ", and height is : " + height);
}
}
Niye bu problem yaradir?
public Tree(float newHeight) {
System.out.println(height);
this("Pine", newHeight);
}
Ferz edinki obyekti yaradib bitirmeden biz onun fieldini cap edirik.
-- Lakin bele kod(menasiz) yaza bilerik:
public Tree(float newHeight) {
System.out.println("height : " + height);
type = "Pine";
height = newHeight;
}
-- Recursive constructor call:
package org.example;
public class Tree {
public float height;
public String type;
public Tree(String name) {
this(name, 5.5F);
}
public Tree(String name, float newHeight) {
this(name);
}
}
-- this()
package org.example;
public class Tree {
public float height;
public Tree grow() {
height++;
return this;
}
}
Tree tree = new Tree();
tree.grow();
System.out.println(tree.height);
-- Excellent example
package org.example;
public class CreditCard {
public String no;
public double balance;
public Customer owner;
public CreditCard(String no, double balance) {
this.no = no;
this.balance = balance;
}
public CreditCard(String no, double balance, Customer owner) {
this.no = no;
this.balance = balance;
this.owner = owner;
owner.setCc(this);
}
public void setOwner(Customer owner) {
this.owner = owner;
}
}
package org.example;
public class Customer {
public int id;
public String name;
public CreditCard cc;
public Customer(int id, String name) {
this.id = id;
this.name = name;
}
public void setCc(CreditCard cc) {
this.cc = cc;
}
}
package org.example;
public class Main {
public static void main(String[] args) {
Customer c1 = new Customer(1, "Parvin");
CreditCard cc1 = new CreditCard("1", 1000);
cc1.setOwner(c1);
c1.setCc(cc1);
// Alternatively
Customer c2 = new Customer(2, "Ali");
CreditCard cc2 = new CreditCard("2", 2000, c2);
}
}
Комментарии
Отправить комментарий