code block
The concept of code blocks:
Code enclosed in {} is a code block. {} is his scope, and the methods and variables defined in {} will be reclaimed by the garbage collector after {} is executed (this sentence is my guess).
Classification of code blocks:
Local code block: In main() , define the life cycle of the variable. Give the variable its life cycle .
Priority of code blocks:
Example:
class Code{ //Static code block: the second highest priority, the second execution static{ int x = 1000 ; System.out.println(x); } // member location { int x = 100 ; System.out.println(x); } //Construction method public Code() { System.out.println("code"); } //construct code block { int y = 200 ; System.out.println(y); } // constructor with parameters public Code(int a) { System.out.println("code"); } //static code block static { int y = 2000 ; System.out.println(y); } } public class CodeDemo { public static void main(String[] args) { //Local code block: the highest priority is executed first { int x = 10 ; System.out.println(x); } //local code block { int y = 20 ; System.out.println(y); } //Create an object of the Code class Code code = new Code() ; System.out.println("--------------------"); Code code2 = new Code() ; System.out.println("--------------------"); Code code3 = new Code(100) ; } }
So the result of our console output is:
1000 2000 100 200 code -------------------- 100 200 code -------------------- 100 200 code
inherit
What is inheritance?
Why introduce inheritance?
Inherited syntax
The grammar stipulates that there cannot be multiple inheritance for inheritance, only single inheritance. But multi-level inheritance can be supported.
The benefits and features of inheritance
Subclasses that inherit the superclass must access the no-argument constructor of the superclass by default. Reason: If the data is not initialized, the parent class should be initialized first, and then the subclass should be initialized - hierarchical initialization.
example
class Father{//This is a parent class int money; private int age; private String name; public void Fa() { System.out.println("i am father"); } private void Fa1() { } public Father() { super(); } public Father(int money, int age, String name) { super(); this.money = money; this.age = age; this.name = name; } public int getMoney() { return money; } public void setMoney(int money) { this.money = money; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } } class Son extends Father{//This is a subclass private String hoby ; int num; public void Aloha(){ System.out.println("i am son"); } public Sound() { super(); } public Son(int money, int age, String name,String hoby) { super(money, age, name); } public String getHoby() { return hoby; } public void setHoby(String hoby) { this.hoby = hoby; } public int getNum() { return num; } public void setNum (int num) { this.num = num; } } public class time8_50 { public static void main(String[] args) { Son s = new Son(); //The private member variables and private member methods of the parent class cannot be accessed through the object s. s.Fa (); s.setNum (10); s.setHoby("pingpang"); System.out.println(s.getNum() + "-"+s.getHoby()); //create object of parent class Father f = new Father(); //After creating the object of the parent class, the child class object can call the parent class's methods and non-private variables s.setAge(80); s.setMoney(210); s.setName("zhangsan"); System.out.println(s.getName() + " " + s.getAge() + " " + s.getMoney() + " son:" + s.getNum() + " " + s.getHoby()); } }
super keyword
E.g:
class Father1{ public int num = 100; } class Son1 extends Father1{ public int num = 10; public void show() { int num = 5; System.out.println(num);//Search order: in the subclass method ---> subclass member position ---> parent class member position can not reach the parent class method System.out.println(this.num);//this can specify member variables when member variables and local variables have the same name System.out.println(super.num);//super can directly point to variables in the parent class when member variables and local variables have the same names as the variables in the parent class. } } public class time11_02 { public static void main(String[] args) { Son1 s =new Son1(); s.show(); } }
Usage of super(&this) keyword
polymorphism
3. There must be a reference to the parent class pointing to the object of the child class (upcasting)
class Fu{
int num = 10 ;
public void Num() {
System.out.println("Test class of parent class");
}
}
class Zi extends Fu{
int num = 20 ;
public void Num() {//Method override
System.out.println("Subclass test class");
}
}
public class Test1 {
public static void main(String[] args) {
Fu f = new Zi();// The reference of the parent class points to the object of the child class
System.out.println(f.num);
f.Num();
}
}
class Animal{ public void eat() { System.out.println("吃"); } public void sleep() { System.out.println("睡"); } } class Dog extends Animal{ public void eat() { System.out.println("狗吃屎"); } public void sleep() { System.out.println("Dog sleeps"); } } class Cat extends Animal{ public void eat() { System.out.println("Cat eats cat food"); } public void sleep() { System.out.println("Cat sleeps"); } } class Pet extends Animal{ private Pet() {//Do not let Pet objects be created } public static void PetTest(Animal a) { a.eat(); a.sleep(); } } public class Test2 { public static void main(String[] args) { Animal c = new Cat(); Animal d = new Dog(); Pet.PetTest(c); Pet.PetTest(d); } }
class Animal2{ public void eat() { } } // cat class class Cat2 extends Animal2 { public void eat() {} public void playGame() {} } //dog class: class Dog2 extends Animal2{ public void eat() {} public void lookDoor() {} } // test class public class DuoTaiDemo5 { public static void main(String[] args) { //in memory is the cat Animal a = new Cat() ; // downcast //restore to cat Cat c = (Cat)a; // becomes a Dog in memory a = new Dog() ; //restore to dog Dog d = (Dog)a ; Cat cc = (Cat)a;//This sentence will convert exception } }
class Person{ public void eat() { System.out.println("Eat..."); } } //southern class SourthPeople extends Person{ public void eat() { System.out.println("Southerners eat rice..."); } // unique function public void business() { System.out.println("Southerners love doing business..."); } } //northern class NorthPeople extends Person{ public void eat(){ System.out.println("Northerners love to eat noodles..."); } // unique function public void yanJiu() { System.out.println("Northerners love to study..."); } } // test class public class DuoTaiTest2 { public static void main(String[] args) { // polymorphism: upcast Person p = new SouthPeople() ;//Upcast cannot access subclass-specific things p.eat(); // downcast SourthPeople sp = (SourthPeople)p;//sp指向SourthPeople sp.business(); System.out.println("-------"); //Actual opening: what class is it, what class object is given SourthPeople sp2 = new SourthPeople() ; sp2.eat(); sp2.business(); } }SouthPeople sp = (SourthPeople)p;//sp points to SouthPeople needs special understanding! ! !