面向对象之继承01

需要了解的知识点
1.清楚继承性的主要作用以及实现
2.继承性的相关限制以及使用规则
继承性的最大特征是解决代码的重用问题
通过简单程序分析,为什么需要继承
要求定义两个描述人与学生的类
Person.java:

class Persion{
    private String name;
    private int age;
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return this.name;
    }
    public void setage(String age){
        this.age = age;
    }
    public int getage(){
        return this.age;
    }
    public String getInfo(){
        return this.name +" " + this.age;
    }
}
Student.java:
class Student{
    private String name;
    private int age;
    private String school;
    public void setschool(String school){
        this.school = school;
    }
    public String getSchool(){
        return this.school;
    }
    public void setSchool(String school){
        this.school = school;
    }
    public String getName(){
        return this.name;
    }
    public void setage(String age){
        this.age = age;
    }
    public int getage(){
        return this.age;
    }
    public String getInfo(){
        return this.name +" " + this.age;
    }
    
}
以上程序里面出现了代码的重复,在自然的关系上,学生是属于人的,学生的范围更小

猜你喜欢

转载自www.cnblogs.com/anyux/p/11895924.html