1、子类中可以根据需要,对从基类中继承来的方法进行重写。
2、重写方法必须和被重写方法具有相同方法名称、参数列表和返回类型。
3、重写方法不能使用比被重写方法更严格的访问权限。
-----
class Person
{
private String name;
private int age;
public void setName(String name)
{
this.name = name;
}
public void setAge(int age)
{
this.age = age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String getInfo()
{
return "Name: "+name +"\n" +"age: " +age;
}
}
class Student extends Person
{
private String school;
public String getSchool()
{
return school;
}
public void setSchool(String school)
{
this.school = school;
}
public String getInfo()
{
return "Name: "+getName()+"\nAge: "+getAge()+"]nschool: "+school;
}
}
public class TestOverwrite
{
public static void main(String[] args)
{
Student student = new Student();
Person person = new Person();
person.setName("yz");
person.setAge(16);
student.setName("HWH");
student.setAge(15);
student.setSchool("USTC");
System.out.println(person.getInfo());
System.out.println(student.getInfo());
}
}
-----