1:static keywords
A static variable can use the class name to invoke the You can also use objects to invoke
But the same static variable in the same class is stored in the same location.
So whatever changes the value of the static variable of that object, the value of the same variable of the other object will change.
A variable used in a static function must be static
You can understand that.
A non-static variable used in a static function is equivalent to this. Variable and this is not clear.
static code blocks are executed when the class is loaded
To assign a value to a static variable in a class
2: Inheritance
Subclasses can inherit member variables of the parent class and member functions do not inherit constructors
When instantiating a subclass object, the constructor of the parent class must be called and the constructor of the parent class must be called First we can also use the Super function in the constructor of the subclass Super also must be the first function
Super's role: You can avoid writing duplicate code
A simple inheritance
Persion class:
class persion{ String name; int Age ; Persion () { System.out.println ("Persion parameterless constructor"); } int Age ) { this. Name = name; this. Age = Age ; System.out.println ("persion with a parametric constructor"); } void eat () { System.out.println ("eat"); } }
Student class:
class extends persion{ int grade; Student () { System.out.println ("Student parameterless constructor"); } Student (String name,int age,int. grade) { this. Grade = grade; // this.age = age; // this.name= name; System.out.println ("Student with parameter constructor");} }
Main function:
class text{ publicstaticvoid main (String args[]) { new Student ("Li Peng", 2,3); System.out.println (astudent.name); System.out.println (astudent.age); System.out.println (Astudent.grade); } }
Results:
visible when the parameter is 3 The parameterless constructor of the parent class is called first, and then the constructor for the 3 arguments of the subclass so: If the subclass does not assign a value to the parent class's variable, the parent class variable is not assigned, so the subclass must assign a value to the parent class variable
① can assign a value directly to a parent class variable in a subclass
② can call the Super method to invoke the parent class constructor
Modified Student Class:
class Student extends persion{ int grade; Student () {System.out.println ( "Student parameterless constructor" int age,int grade) { super (Name,age); this . Grade = grade; // this.age = age; // this.grade = grade; SYSTEM.OUT.PRINTLN ("Student has the parameter constructor"
Operation Result:
This will allow the code to be reduced as much as possible
The path of Java Learning (iv)