p69 super explanation
1) super and this keywords
- super represents the parent class reference, which can only be used in subclasses, and this represents the current class reference
- super(); Statement is a parameterless construct that calls the parent class, this(); Statement is a parameterless construct that calls the current class, but a recursive call compilation error will be generated
- Super (argument); Is a parameterized construct that calls the parent class, this (argument); Is a parameterized construct that calls the current class
- there is a hidden code "super();" in the nonparticipated and parameterized constructors of subclasses, which calls the nonparametric construction of the parent class by default
- the call to super must be the first statement in the constructor
- if the parent class defines a parameterless constructor, resulting in the loss of parameterless construction, the definition of parameterless construction must be displayed, otherwise compilation errors will occur
- of course, you can also display the definition "super (argument);" in the subclass and call the parameterized constructor of the parent class in a parameterized way
- if a subclass defines a parameterized constructor, it is also necessary to define a parameterless constructor, or create an object in a parameterized manner
super. Attribute name
this. Property name
super. Method name ();
this. Method name ();
super([argument]);
this([argument]);
//Example 5-1: usage of this and super
//Test class public class A05{ public static void main(String[] args){ //Create subclass objects Student anStudent = new Student(); //myname //student //person anStudent.test("myname"); //hello world //hello world //hello java anStudent.test1(); } } //The subclass inherits the parent class public class Student extends Person{ //Properties of subclasses private String name = "student"; //Subclass method public void print(){ System.out.println("hello world"); } //Subclass method public void test(String name){ System.out.println(name); //Call pass value System.out.println(this.name); //Call this class property System.out.println(super.name); //Calling parent class properties } //Subclass method public void test1(){ print(); //Call the methods of this class this.print(); //Call the methods of this class super.print(); //Call the method of the parent class } } //Parent class public class Person{ //Properties of the parent class protected String name = "person"; //Method of parent class public void print(){ System.out.println("hello java"); } }
// Example 6: how does a subclass call the constructor of its parent class? How do I call the constructor when I create an object? Usage of super and this to construction
public class A06{ public static void main(String[] args){ //The essence of creating an object is to call the constructor /* This is the parametric construction of Person Person No parameter execution Student No parameter execution */ new Student(); //new Student("zhangsan"); } } //Subclass. There is a hidden code in the constructor of the subclass, which calls the parameterless construction of the parent class by default public class Student extends Person{ //This is a parameterless construct public Student(){ super(); System.out.println("Student No parameter execution"); } //This is a parametric structure public Student(String name){ super(); System.out.println("This is Student Parametric structure of"); } } //Parent class public class Person{ //This is a parameterless construct public Person(){ this("xiaohua"); System.out.println("Person No parameter execution"); } //This is a parametric structure public Person(String name){ System.out.println("This is Person Parametric structure of"); } }
p72 instanceof operator and type conversion
1,instanceof keyword
- is the former an example of the latter, does the former belong to the latter, and is the former a latter
- is a logical operator that determines whether the object on the left is an instance of the class on the right, and returns a Boolean value
- first, compile and check whether the type on the left side of the syntax has an instance relationship with the class on the right. If yes, the compilation passes, otherwise the compilation error occurs
- then run the check to determine that the reference / object on the left is the instance on the right. If yes, it returns true, otherwise it returns false
- the expression is:
Reference / object instanceof class
2. Type conversion of reference variables
- when converting a subtype reference to a parent type, it can be automatically converted from low-level to high-level. At this time, the parent class reference points to the subclass object, and the unique elements of the subclass will be lost
- when converting a reference of a parent type to a subtype, the conversion from high-level to low-level needs to be forced. At this time, a subclass reference pointing to a subclass object is obtained
For example: Student s1 = new Student();
Person s2 = s1; // Automatic type conversion
Student s3 = (Student)s2; // Cast type
// Example 9-1: testing the usage of the instanceof operator
//Test class public class T09{ public static void main(String[] args){ //Object > String //Object > Person > Teacher //Object > Person > Student Student s1 = new Student(); Person s2 = new Student(); Object s3 = new Student(); //test instanceof Usage of operator System.out.println(s1 instanceof Student);//true System.out.println(s1 instanceof Person); //true System.out.println(s1 instanceof Object); //true //System.out.println(s1 instanceof Teacher);//Compilation error //System.out.println(s1 instanceof String); //Compilation error System.out.println(s2 instanceof Student);//true System.out.println(s2 instanceof Person); //true System.out.println(s2 instanceof Object); //true System.out.println(s2 instanceof Teacher);//false //System.out.println(s2 instanceof String); //Compilation error System.out.println(s3 instanceof Student);//true System.out.println(s3 instanceof Person); //true System.out.println(s3 instanceof Object); //true System.out.println(s3 instanceof Teacher);//false System.out.println(s3 instanceof String); //false System.out.println(new Student() instanceof Student);//true System.out.println(new Student() instanceof Person); //true System.out.println(new Student() instanceof Object); //true //System.out.println(new Student() instanceof Teacher);//Compilation error //System.out.println(new Student() instanceof String); //Compilation error } }
// Example 9-2: testing the type conversion of a reference variable
//Test type conversion of reference variables public class T09{ public static void main(String[] args){ //Object > Person > Student Student s1 = new Student(); Person s2 = s1;//amount to Person s2 = (Person)s1; System.out.println(s1.name);//ok System.out.println(s1.age);//ok s1.run();//ok s1.eat();//ok //s1 Up conversion, automatic conversion, s2 Fully inherited parent class,However, the elements of subclass objects cannot be accessed System.out.println(s2.name); s2.run(); //System.out.println(s2.age);//Symbol not found //s2.eat();//Symbol not found } } //Student class public class Student extends Person{ int age; //rewrite run()method public void run(){ System.out.println("Student run"); } //Methods unique to subclasses public void eat(){ System.out.println("Student eat"); } } //Parent class public class Person{ String name; //Initialize with constructor public Person(){ this.name = "chinese"; } public void run(){ System.out.println("Person run"); } }
// Example 9-3: type down conversion of test reference
//Type down conversion of test reference public class T09{ public static void main(String[] args){ //Object > Person > Student Person s2 = new Student(); Student s3 = (Student)s2; //Student s3 = s2;//Error, incompatible type, Person Cannot convert to Student //Conversion from high-level to low-level requires forced conversion,s3 Point to subclass object System.out.println(s3.name);//ok System.out.println(s3.age);//ok ((Student)s2).run();//ok ((Student)s2).eat();//ok } } //Student Class inherits the parent class public class Student extends Person{ int age; //rewrite run()method public void run(){ System.out.println("Student run"); } //Methods unique to subclasses public void eat(){ System.out.println("Student eat"); } } //Parent class public class Person{ String name; public void run(){ System.out.println("Person run"); } }
//Even the smallest sail can sail far 2021-11-09