TestOverWrite.java
?
class="java">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;} //重写OverWrite父类的getInfo()方法,访问权限和父类一样,不能更严格 public String getInfo() { return "Name: "+ getName() + "\nage: "+ getAge() + "\nschool: "+ school; } } public class TestOverWrite { public static void main(String arg[]){ Student student = new Student(); Person person = new Person(); person.setName("none"); person.setAge(1000); student.setName("John"); student.setAge(18); student.setSchool("SCH"); System.out.println(person.getInfo());//Name: none age: 1000 System.out.println(student.getInfo());//Name: John age: 18 school: SCH } }
?
?