Method rewriting of Java inheritance
•
Java
In Java inheritance, a subclass can obtain all the structures of the parent class, and can also add properties and methods different from the parent class. However, in one case, the behavior of a subclass of the same method is not the same as that of the parent class. In this case, it is necessary to rewrite the method of the parent class. The following is the code implementation of the rewrite:
Code embodiment
package com.my.pac12;
/**
* @author Summerday
* @date 2019/12/11 21:26
*/
public class Person {
public void say(){
System.out.println("say something..");
}
public static void see(){
System.out.println("see something..");
}
public int returnNumber(int number){
return number;
}
private void run(){
System.out.println("running ..");
}
public Person returnPerson(){
return this;
}
}
class student extends Person{
//方法名相同,形参列表相同
public void say(){
System.out.println("student say something..");
}
//返回类型为父类或父类的子类
public Person returnPerson(){
System.out.println("子类返回类型可以是父类返回类型或者是其子类类型");
return this;
}
//并不是重写,只是重新定义了新方法
public void run(){
System.out.println("student is running..");
}
//不是重写,而是发生在父类与子类之间的重载
public int returnNuber(int number,int otherNumber){
return number+otherNumber;
}
public static void main(String[] args) {
student s = new student();
s.say();
student.see();
s.see();
s.run();
//涉及向上转型
Person sn = s.returnPerson();
//调用的是父类的方法
System.out.println(s.returnNumber(5));
//调用子类重载父类的方法
System.out.println(s.returnNuber(5,5));
}
}
concept
matters needing attention
"Two are the same, two are small and one is large"
//父类
public int returnNumber(int number){
return number;
}
/*基本类型:子类返回值类型小于等于父类返回值类型,下面的语句不允许*/
//子类
public long returnNumber(int number)
//父类
public void say(){
System.out.println("say something..");
}
/*void类型只能由同样void类型的方法重写*/
//子类
public void say(){
System.out.println("student say something..");
}
//父类
public Person returnPerson(){
return this;
}
/*引用类型:子类返回值类型需要与父类相同或者是父类的子类*/
//子类
public Person returnPerson(){
System.out.println("子类返回类型可以是父类返回类型或者是其子类类型");
return this;
}
//父类
public void say(){
System.out.println("say something..");
}
/*子类方法的访问权限大于等于父类方法的访问权限,下面的语句不允许*/
//子类
private(protected or 缺省) void say()
Other precautions
//父类
public static void see(){
System.out.println("see something..");
}
/*两者必须同为类方法(static修饰)或者同为实例方法,下面的语句不允许*/
//子类
public void see()
//父类
private void run(){
System.out.println("running ..");
}
/*子类无法重写方法,下面的语句是假象,其实是重新定义了一个新方法*/
//子类
public void run(){
System.out.println("student is running..");
}
Rewriting and overloading
//父类
public int returnNumber(int number){
return number;
}
/*发生在父类与子类之间的重载*/
//子类
public int returnNuber(int number,int otherNumber){
return number+otherNumber;
}
@Override annotation
@The override annotation assists in method rewriting and does not affect the code itself.
The above figure also verifies three examples that are not method Rewriting:
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码