方法重写的定义
在子类中可以根据需要对父类中继承来的方法进行改造,也称为方法的重写、覆盖。在执行程序时,子类方法经覆盖父类方法。
要求
1. 子类重写的方法必须和父类重写的方法具有相同的方法名称、参数列表
2.子类重写的方法返回值类型不能大于父类被重写的方法返回值类性
3.子类重写的方法访问权限不能小于父类被重写的方法的权限;子类不能重写父类中声明的private 权限的方法
4.子类方法抛出的异常不能大于父类被重写方法时异常
注意
子类与父类中同名同参的方法必须同时声明为非static的(即为重写),或者同时声明为static的(即为重写)。因为static属于类的,子方法无法覆盖父类的方法
示例父类
package com.chenxi.java; public class Person { String name ; int age; public Person(){ } public Person(String name,int age){ this.name= name; this.age = age; } public void eat(){ System.out.println("吃饭"); } public void wald(int distance){ System.out.println(name+"走了"+distance+"公里!"); } public void show(){ System.out.println("我是一个人"); } public Object info(){ return null; } }
示例子类
package com.chenxi.java; /* */ public class Student extends Person { String major; public Student (){ } public Student (String major){ this.major = major; } public void stuby(){ System.out.println("学习 。专业"+major); } @Override//对父类方法的重写 public void eat() { System.out.println("吃有营养的食物"); } public String info(){ return null; } public void wald(int distance){ System.out.println("重写"); } }
测试类
package com.chenxi.java; /* 方法的重写(override/overwrite) 1.重写:子类方法继承父类以后,可以对父类中同名同参的方法,进行覆盖操作 2.应用:重写以后,当创建子类对象以后。通过子类对象调用子父类中同名同参的方法时,实际执行的时子类重写父类后的方法 重写的规定 方法的声明 :权限修饰符 返回值类型 方法名(形参列表) throws 异常类型{ 方法体 } 约定俗称 :子类中的叫重写方法,父类中叫被重写的方法 子类重写的方法名和方法形参列表与被重写的方法名和形参列表形同 子类重写的方法的权限修饰符不小于父类重写的权限修饰符 子类不能重写父类中的private权限的方法 返回值类型: 父类被重写的方法的返回值类型是void,则子类重写的返回值类型也只能是void 父类被重写的方法返回值类型是A类性,则子类重写的方法返回值类型可以是A类或者A类的子类 父类被重写的方法返回值类型是基本数据类型,则子类冲写的方法返回值类型必须是型同的进本数据类型 子类重写的方法抛出的异常类型不大于父类被重写的方法抛出的异常类型 */ public class PersonTest { public static void main(String[] args) { Student s1= new Student("阅读"); s1.stuby(); s1.eat(); s1.info(); } } 测试 学习 。专业阅读 吃有营养的食物 重写 Process finished with exit code 0
子类和父类中同名或者同参的方法要么都声明为非static的(考虑重写),要么都声明为static的(不是重写)
测试
父类
package com.chenxi.exer; public class Mankind { private int sex ; private int salary; public Mankind(){ } public Mankind(int sex,int salary){ this.sex=sex; this.salary=salary; } public void maOrWoman(){ if (sex==1){ System.out.println("man"); }else if ( sex == 0){ System.out.println("woman"); } } public void employeed() { // if (salary == 1) { // System.out.println("man"); // } else if (salary == 0) { // System.out.println("woman"); // } else { // System.out.println("人妖"); // } // } if (salary == 0) { System.out.println("no job"); } else if (salary != 0) { System.out.println("job"); } } public void setSex(int sex) { this.sex = sex; } public void setSalary(int salary) { this.salary = salary; } public int getSalary() { return salary; } public int getSex() { return sex; } }
子类
package com.chenxi.exer; public class kids extends Mankind { private int yearsOld; public kids(){ } public kids(int yearsOld){ this.yearsOld=yearsOld; } public void setYearsOld(int yearsOld) { this.yearsOld = yearsOld; } public int getYearsOld() { return yearsOld; } public void employeed() { System.out.println("kind should study and no job."); } public void printAge(){ System.out.println("I am"+ yearsOld+" years old"); } }
测试类
package com.chenxi.exer; public class KidsTest { public static void main(String[] args) { kids somekid= new kids(12); somekid.printAge(); somekid.setYearsOld(4); somekid.setSex(8); somekid.employeed(); } } 结果 I am12 years old kind should study and no job.