Java方法重写
SSS4362 人气:01.含义
子类继承父类后,可以在子类中书写一个与父类同名同参的方法,从而实现对父类中同名同参数的方法的覆盖,我们把这一过程叫做方法的重写(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.去掉重写方法中自动生成的首行,然在其方法体里面写入合适的语句
操作截图展示
加载全部内容