SE_21_Lesson_7: Constructor
Constuctor - is a special method that is used to initialize objects. The constructor is called when an object of a class is created.
-> Menasiz constructor (dummy contructor):
package org.example;
public class Tree {
public String type;
public float height;
public Tree() {
type = "Oak";
height = 1.0F;
}
}
Bu zaman butun obyektler standart wekil alacaqdir.
-> Smart constructor:
package org.example;
public class Tree {
public String type;
public float height;
public Tree(String newType, float newHeight) {
type = newType;
height = newHeight;
}
}
-> Constructor overloading:
package org.example;
public class Tree {
public String type;
public float height;
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;
}
}
-> Constructorlarin bir birini cagirmasi:
package org.example;
public class Tree2 {
public String type;
public float height;
public Tree2(String newType, float newHeight) {
type = newType;
height = newHeight;
}
public Tree2(String newType) {
this(newType, 1.0F);
}
public Tree2(float newHeight) {
this("Pine", newHeight);
}
public Tree2() {
this("Pine", 1.0F);
}
}
-> this keyword constructor-da ilk setirde olmalidir, eks halda compile time error alariq.package org.example;
public class Tree2 {
public String type;
public float height;
public Tree2(String newType, float newHeight) {
type = newType;
height = newHeight;
}
public Tree2(String newType) {
this(newType, 1.0F);
}
public Tree2(float newHeight) {
System.out.println(height);
this("Pine", newHeight);
}
public Tree2() {
this("Pine", 1.0F);
}
}
Burada obyekti yaradib bitirmeden filed-ini print edirik.
-> Qarshiliqli recursive this constructorlarda ishletmek olmaz
package org.example;
public class Tree2 {
public String type;
public float height;
public Tree2(String newType, float newHeight) {
this();
type = newType;
height = newHeight;
}
public Tree2(String newType) {
this(newType, 1.0F);
}
public Tree2(float newHeight) {
this("Pine", newHeight);
}
public Tree2() {
this("Pine", 1.0F);
}
}
->Class-in icinde static int deyisheni yaratmaq ve constructor ile onu 1 vahid artirmaq. Yeni her defe obyekt yaradilanda hemin deyishen artacaq.
package org.example;
public class Test {
public static int count;
public Test() {
count++;
}
}
package org.example;
public class Main {
public static void main(String[] args) {
new Test();
new Test();
new Test();
new Test();
new Test();
System.out.println(Test.count);
}
}
-> this keyword. illegal forward reference problemi ve this keyword-u ile bu problemi aradan qaldirmaq, ve method ile aradan qaldirmaq:
package org.example;
public class ThisDemo {
int i = k;
int k = 5;
int j = 2;
}
package org.example;
public class ThisDemo {
int i = this.k;
int k = 5;
int j = 2;
}
package org.example;
public class Main2 {
public static void main(String[] args) {
ThisDemo one = new ThisDemo();
System.out.println(one.i);
System.out.println(one.k);
System.out.println(one.j);
}
}
package org.example.test;
public class Main {
int i = getJ();
int j = 10;
int getJ() {
return j;
}
public static void main(String[] args) {
Main main = new Main();
System.out.println(main.i);
}
}
Lakin:
package org.example;
public class ThisDemo {
int k = 5;
int i = this.k;
int j = 2;
}
Bu wekilde olduqda cavabda deyiwir.
-> this keywordu metodlarda:
package org.example;
public class ThisDemo {
int k = 5;
int i = this.k;
int j = 2;
public void foo1() {
this.foo2();
System.out.println("foo1");
}
public void foo2() {
System.out.println("foo2");
}
}
-> this keywordunu menali iwledilmesi:
package org.example;
public class ThisDemo {
int k = 5;
int i = this.k;
int j = 2;
public void foo() {
int i = this.i;
System.out.println(i);
}
}
-> this keywordunun obyektin ozunu qaytarmasi:
package org.example;
public class Person {
public int height;
public Person grow() {
height++;
return this;
}
}
package org.example;
public class Main2 {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.height);
person = person.grow();
System.out.println(person.height);
}
}
*** Bunu yoxlamaq ucun cari adresleini cap etmek kifayet edecek.
-> ***
package org.lesson4;
public class Person {
public int height;
public Person grow() {
height++;
return new Person();
}
}
package org.lesson4;
public class Main {
public static void main(String[] args) {
Person one = new Person();
System.out.println(one.height);
Person two = one.grow();
System.out.println(one.height);
System.out.println(two.height);
}
}
->***
package org.lesson4;
public class Person {
public int height;
public Person(int height) {
this.height = height;
}
public Person grow() {
height++;
return new Person(height);
}
}
package org.lesson4;
public class Main {
public static void main(String[] args) {
Person one = new Person(5);
System.out.println(one.height);
Person two = one.grow();
System.out.println(one.height);
System.out.println(two.height);
}
}
-> this keywordune en gozel numune:
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;
}
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", name='" + name + '\'' +
", cc=" + cc +
'}';
}
}
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;
}
@Override
public String toString() {
return "CreditCard{" +
"no='" + no + '\'' +
", balance=" + balance +
'}';
}
}
package org.example;
public class Main2 {
public static void main(String[] args) {
Customer c1 = new Customer(1, "Parvin");
CreditCard cc1 = new CreditCard("1", 2000);
cc1.setOwner(c1);
c1.setCc(cc1);
System.out.println(c1);
// Alternative
Customer c2 = new Customer(2, "Narmin");
CreditCard cc2 = new CreditCard("2", 3000, c2);
System.out.println(c2);
}
}
Yuxaridaki numunede 2 setr koddan azad olduq.
-> == obyektlerin beraberliyinin yoxlanilmasi
package org.example;
public class Main2 {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.height);
Person grow = person.grow();
System.out.println(grow.height);
System.out.println(person == grow);
}
}
Комментарии
Отправить комментарий