本篇文章给大家带来了关于java的相关知识,子类继承父类后,可以在子类中书写一个与父类同名同参的方法,从而实现对父类中同名同参数的方法的覆盖,我们把这一过程叫做方法的重写,下面一起来看一下,希望对大家有帮助。
推荐学习:《java视频教程》
1.含义子类继承父类后,可以在子类中书写一个与父类同名同参的方法,从而实现对父类中同名同参数的方法的覆盖,我们把这一过程叫做方法的重写(override)
2.为什么要使用方法重写2.1 当父类的方法满足不了子类的需求的时候,需要在子类中对该方法进行重写
2.2 题目与分析
例如存在一个父类peple,子类chinese,父类中有一个say()方法,输出人在说话,但是我想要的时子类时候调用时输出中国人在说话,很显然直接调用方法不行,所以需要在子类中对say方法进行方法的重写
2.3 示例代码
people类
public class peple { private string name; private string sex; private int age; private int weight; public peple() { } public peple(string name, string sex, int age, int weight) { this.name = name; this.sex = sex; this.age = age; this.weight=weight; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getsex() { return sex; } public void setsex(string sex) { this.sex = sex; } public int getage() { return age; } public void setage(int age) { this.age = age; } public int getweight() { return weight; } public void setweight(int weight) { this.weight = weight; } public void say(){ system.out.println("人在说话"); }}
chinese类
public class chinese extends peple{ public chinese() { } @override public void say() { system.out.println("中国人在说话"); }}
test03类
public class test03 { public static void main(string[] args) { chinese c=new chinese(); c.say(); //当进行方法重写时,调用的是子类的say()方法 }}
2.4 示例代码运行截图
3.如何使用方法重写3.1 基本语法@override权限修饰符 返回值类型 方法名(形参列表){ //子类重写的方法的权限修饰符的访问权限必须大于等于父类的,但是父类不能是private //当父类的返回值类型为基本数据类型或者为void时,子类方法的返回值类型也应该为对应的基本数据类型或者void }
3.2 具体分析3.2.1 子类重写的方法的访问权限应该大于等于父类方法的访问权限
a 示例代码
people类
public class peple { private string name; private string sex; private int age; private int weight; public peple() { } public peple(string name, string sex, int age, int weight) { this.name = name; this.sex = sex; this.age = age; this.weight=weight; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getsex() { return sex; } public void setsex(string sex) { this.sex = sex; } public int getage() { return age; } public void setage(int age) { this.age = age; } public int getweight() { return weight; } public void setweight(int weight) { this.weight = weight; } //没有写访问权限的话,默认是default访问权限 void say(){ system.out.println("人在说话"); }}
chinese类
public class chinese extends peple{ public chinese(){ } //父类say方法的访问权限为default,子类say方法的访问权限为public, // 符合子类方法访问权限大于等于父类方法访问权限的要求 @override public void say() { system.out.println("中国人在说话"); }}
test03类
public class test03 { public static void main(string[] args) { chinese c=new chinese(); c.say(); }}
b 示例代码运行截图
3.2.2 当父类方法的返回值类型为基本数据类型时,子类重写的方法的返回值类型也为对应的基本数据类型
a 示例代码
people类
public class peple { private string name; private string sex; private int age; private int weight; public peple() { } public peple(string name, string sex, int age, int weight) { this.name = name; this.sex = sex; this.age = age; this.weight=weight; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getsex() { return sex; } public void setsex(string sex) { this.sex = sex; } public int getage() { return age; } public void setage(int age) { this.age = age; } public int getweight() { return weight; } public void setweight(int weight) { this.weight = weight; } public double add(int a,int b){ return a+b; }}
chinese类
public class chinese extends peple{ public chinese(){ } @override public double add(int a,int b) { return a+b+1; }}
test03类
public class test03 { public static void main(string[] args) { chinese c=new chinese(); system.out.println("求和之和再加上1的结果为: "+c.add(2, 3)); }}
b 示例代码运行截图
3.2.3 当父类方法的返回值类型为void时,子类重写的方法的返回值类型也为void
a 示例代码
people类
public class peple { private string name; private string sex; private int age; private int weight; public peple() { } public peple(string name, string sex, int age, int weight) { this.name = name; this.sex = sex; this.age = age; this.weight=weight; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getsex() { return sex; } public void setsex(string sex) { this.sex = sex; } public int getage() { return age; } public void setage(int age) { this.age = age; } public int getweight() { return weight; } public void setweight(int weight) { this.weight = weight; } public void eat(){ system.out.println("人的主食一般是熟食"); }}
chinese类
public class chinese extends peple{ public chinese(){ } @override public void eat() { system.out.println("中国人的主食是大米或者小麦"); }}
test03类
public class test03 { public static void main(string[] args) { chinese c=new chinese(); c.eat(); }}
b 示例代码运行截图
3.2.4 当父类的方法被static修饰的时候,子类是不能重写该方法的
a 错误示例代码
people类
public class peple { private string name; private string sex; private int age; private int weight; public peple() { } public peple(string name, string sex, int age, int weight) { this.name = name; this.sex = sex; this.age = age; this.weight=weight; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getsex() { return sex; } public void setsex(string sex) { this.sex = sex; } public int getage() { return age; } public void setage(int age) { this.age = age; } public int getweight() { return weight; } public void setweight(int weight) { this.weight = weight; } public static void eat(){ system.out.println("人的主食一般是熟食"); }}
chinese类
public class chinese extends peple{ public chinese(){ } @override public void eat() { system.out.println("中国人的主食是大米或者小麦"); }}
test03类
public class test03 { public static void main(string[] args) { chinese c=new chinese(); c.eat(); }}
b 示例代码运行截图
编译时idea给出的错误提示
强制运行后给出的错误提示
3.3 方法重写的一些小技巧3.3.1 复制法
步骤
1.先直接复制(ctrl+c)父类中需要被子类重写的那个方法
2.粘贴(ctrl+v)到子类中
3.修改子类中的功能,便于实现父类实现不了的需求
操作截图展示
3.3.2 编译器提示法
步骤
1.先在子类类体中,非方法内位置,写入一个英文@符号
2.选中提示中的overide/implement methods…
3.双击后弹出一个重写方法选择列表弹窗
4.根据提示选中对应的需要重写的方法
5.点击ok按钮后会在子类中生成一个你选中方法的重写方法
6.把生成的重写方法中的自动生成的首行去掉,然后根据需求在方法体里面写入合适的语句
操作截图展示
3.3.3 快捷键法
步骤
1.把鼠标移动到重写方法应该生成的位置
2.同时按下键盘上的alt键和insert键,
3.在弹出框中,选中override methods
4.双击后会弹出一个界面,在界面中选中需要被子类重写的方法
5.点击ok按钮后会生成所需要的重写方法
6.去掉重写方法中自动生成的首行,然在其方法体里面写入合适的语句
操作截图展示
推荐学习:《java视频教程》
以上就是完全掌握java中的方法重写的详细内容。