Lesson: Class and Object
Bir paketin icinde eyni adli cemi 1 eded class duzelde bilerik.
package org.example;
public class Car {
public String model;
public String color;
public int year;
public static String manufacturer;
public static void staticMethod() {
System.out.println("This is static method");
}
public void nonStaticMethod() {
System.out.println("This is non static method");
manufacturer = "Mercedes";
}
}
Car.manufacturer = "General Motors";
System.out.println(Car.manufacturer);
Car car = new Car();
car.nonStaticMethod();
System.out.println(car.manufacturer);
- Weird behaviour
Car c = null;
c.manufacturer = "Cadillac";
// c.model = "Island";
System.out.println(c.manufacturer);
// System.out.println(c.model);
- Referansin qirilmasi
Car one = new Car();
one.model = "Toyota";
one = new Car();
one.model = "Mercedes";
System.out.println(one.model);
- Biz eyni vaxtda klasin icinde eyni adli hem static hem de non static field elan ede bilmerik. Cunki
obyekden static fielde cata bilerik. Eger elan ede bilsek o zaman qariwiqliq yaranar.
- Stack Overflow xetasi nedir?
package org.example;
public class Main {
public static void main(String[] args) {
method1();
}
public static void method1() {
System.out.println("method1");
method2();
}
public static void method2() {
System.out.println("method2");
method1();
}
}
- Pass by value and pass by reference difference
package org.example;
public class Main {
public static void main(String[] args) {
int k = 5;
foo(k);
System.out.println(k);
}
public static void foo(int a) {
a = 10;
}
}
package org.example;
public class Main {
public static void main(String[] args) {
Car c = new Car();
c.model = "Mercedes";
foo(c);
System.out.println(c.model);
}
public static void foo(Car car) {
car.model = "Honda";
}
}
package org.example;
public class Main {
public static void main(String[] args) {
char[] el = {'a'};
foo(el);
System.out.println(el);
}
public static void foo(char[] chars) {
chars[0] = 'b';
}
}
-
package org.example;
public class Main {
public static void main(String[] args) {
Car one = new Car();
Car two = foo(one);
System.out.println(one.model);
System.out.println(two.model);
}
public static Car foo(Car car) {
car.model = "BMW";
return car;
}
}
-- interesting situation:
package org.example2;
public class Main {
Integer a;
public static void main(String[] args) {
new Main().foo("test");
}
public void foo(String s) {
Integer i;
System.out.println(i);
System.out.println(a);
}
}
- Access modifiers (public, private, default, protected)
Getter and Setter anlayishi. Encapsulation nedir?
Комментарии
Отправить комментарий