(The first point of this season is to recall the difference between method overloading and overriding, super and this keyword. In the future, it will be mainly explained by examples, and the basic concepts of inheritance and arrays in JAVA will be recalled first, and then the graphical representation of inheritance in JAVA will be explained later.
Memories from last season:
1. The use and concept of inheritance, various limitations of inheritance
2. The instantiation process of the subclass object
3. Method Override
4. Use of super
There are two titles that are often involved in interviews~~~
Interview 1: Explain the difference between method overriding and method overloading:
Interview 2: The difference between super and this
property override (less used)
Let's just verify
classA
{
Stringname=redking;
};
classBextendsA
{
//The subclass defines a name property that is the same as in the parent class
Stringname=Michael;
publicvoidprint()
{
//Same as System.out.println(this.name);
System.out.println(name);
}
};
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
Bb=newB();
b.print();
}
};
The program prints the name attribute defined in the subclass: Michael
If we want to print the name attribute in the parent class, we can modify it to super.name
classA
{
Stringname=redking;
};
classBextendsA
{
//The subclass defines a name property that is the same as in the parent class
Stringname=Michael;
publicvoidprint()
{
//Same as System.out.println(this.name);
System.out.println(name);
//If we want to print the name attribute in the parent class, we can modify it to super.name
System.out.println(super.name);
}
};
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
Bb=newB();
b.print();
}
};
The name attribute in the parent class is output ha~ This is called attribute duplication
Attributes are usually required to be encapsulated. After being encapsulated, subclasses must not be able to see the content in the parent class, so basically they cannot be overwritten.
Can super and this call structure method be written together in a structure method? The answer is no~
When Super calls the structure method, it must be placed in the first line of the structure method, and this must also be placed in the first line when calling the structure method. If both are placed in the first line, they must conflict.
Some people think that the super method can not be called, let's see the following Demo02
classA
{
publicA(){}
};
classBextendsA
{
//There are three structure methods in it
publicB()
{
this(abc,888);
}
publicB(Stringname)
{
this();
}
publicB(Stringname,intage)
{
this(name);
}
};
This creates a problem, which we also mentioned when we talked about this keyword~
When using this() to call structure methods, you must leave an exit. Otherwise, the compilation will fail~
classA
{
publicA(){}
};
classBextendsA
{
//There are three structure methods in it
publicB()
{
//It's better to change this(abc,888); to super() as an export~
super();
}
publicB(Stringname)
{
this();
}
publicB(Stringname,intage)
{
this(name);
}
};
Now the compilation is passed~
Top knowledge points this season:
1. The class diagram of inheritance shows that
2. Inherited title
Now let's take a look at Demo03:
classA
{
};
classBextendsA
{
};
This drop inheritance contact us to show with a class diagram
Knowing the above, let's take a look at the following exercises:
classPerson
{
privateStringname;
privateStringaddr;
privatecharsex;
privateintage;
//Usually the structure method with few parameters is written on it~~
publicPerson(){}
publicPerson(Stringname, Stringaddr)
{
this.setName(name);
this.setAddr(addr);
this.setSex('male');
this.setAge(27);
}
publicPerson(Stringname,Stringaddr,charsex,intage)
{
this.setName(name);
this.setAddr(addr);
this.setSex(sex);
this.setAge(age);
}
publicvoidsetName(Stringname)
{
this.name=name;
}
publicvoidsetAddr(Stringaddr)
{
this.addr=addr;
}
//M: indicates male; F: indicates female
publicvoidsetSex(charsex)
{
this.sex=sex;
}
publicvoidsetAge(intage)
{
this.age=age;
}
publicStringgetName()
{
returnthis.name;
}
publicStringgetAddr()
{
returnthis.addr;
}
publicchargetSex()
{
returnthis.sex;
}
publicintgetAge()
{
returnthis.age;
}
//All content should be handed over to external output
publicStringgetInfo()
{
return name: this.name
,Address: this.addr
, Gender: (this.sex=='M'? Male: Female)
, age: this.age;
}
};
classStudentextendsPerson
{
privatefloatmath;
privatefloatenglish;
publicStudent()
{
// super() is implied by default;
super();
}
publicStudent(Stringname, Stringaddr)
{
super(name,addr);
}
publicStudent(Stringname,Stringaddr,charsex,intage,floatmath,floatenglish)
[Zero Basics JAVA] JavaSE Object-Oriented Part-14. Object-Oriented Advanced (02).doc)