第2课第3节_Java面向对象编程_继承性_P

 

面向对象程序的三大特性之继承性:继承性的主要作用就是复用代码.继承性也有一定的限制,如图一

第2课第3节_Java面向对象编程_继承性_P

1、我们在第2课第2节_Java面向对象编程_封装性_P 中定义了persion.我在这代码基础上定义一个学习类,并继承persion类。

class Persion{
    private int age;

    public void setAge(int age){
        if (age < 0 || age > 200)
            age = 0;
        else
            this.age = age;
    }

    public int getAge(){
        return age;
    }
}

class Student extends Persion{

}

public class Ext{
    public static void main(String args[]){
        Student stu = new Student();
        stu.setAge(10);

        System.out.println(stu.getAge());
        
    }
}

编译运行结果

第2课第3节_Java面向对象编程_继承性_P

2、在上面的代码基础上,我们的子类继承了父类,我们可以添加自己的属性还有方法.

class Persion{
    private int age;

    public void setAge(int age){
        if (age < 0 || age > 200)
            age = 0;
        else
            this.age = age;
    }

    public int getAge(){
        return age;
    }
}

class Student extends Persion{
    private String school;

    public void setSchool(String school){
        this.school = school;
    }

    public String getSchool(){
        return school;
    }

    public Student(String school){
        this.school = school;
    }
    

}

public class Ext2{
    public static void main(String args[]){
        Student stu = new Student("ustc");
        stu.setAge(10);

        System.out.println(stu.getAge());
        System.out.println(stu.getSchool());
        
    }
}

编译运行结果

第2课第3节_Java面向对象编程_继承性_P

3、子类继承父类的方法和属性都可以进行覆写,我们在子类覆写父类的printInfo方法。

class Persion{
    private int age;

    public void setAge(int age){
        if (age < 0 || age > 200)
            age = 0;
        else
            this.age = age;
    }

    public int getAge(){
        return age;
    }

    public void printInfo(){
        System.out.println("age = " + age);
    }
}

class Student extends Persion{
    private String school;

    public void setSchool(String school){
        this.school = school;
    }

    public String getSchool(){
        return school;
    }

    public Student(String school){
        this.school = school;
    }

    public void printInfo(){
        System.out.println("school = " + school + " age = " + getAge());
    }
}

public class Ext3{
    public static void main(String args[]){
        Student stu = new Student("ustc");
        stu.setAge(10);

        System.out.println(stu.getAge());
        System.out.println(stu.getSchool());
        stu.printInfo();
        
    }
}

编译运行结果

第2课第3节_Java面向对象编程_继承性_P

4、实例化子类对象时,先调用父类的构造方法,再调用子类的构造方法,super()函数在子类中可以指定调用父类构造函数的类型。

class Persion{
    private int age;

    public void setAge(int age){
        if (age < 0 || age > 200)
            age = 0;
        else
            this.age = age;
    }

    public int getAge(){
        return age;
    }

    public void printInfo(){
        System.out.println("age = " + age);
    }

    public Persion(){System.out.println("Persion()");};
    public Persion(int age){
        System.out.println("Persion(int age)");
        this.age = age;
    }    
}

class Student extends Persion{
    private String school;

    public void setSchool(String school){
        this.school = school;
    }

    public String getSchool(){
        return school;
    }

    public Student(String school){
        super(5);
        System.out.println("Student(String school)");
        this.school = school;
    }

    public void printInfo(){
        System.out.println("school = " + school + " age = " + getAge());
    }
}

public class Ext4{
    public static void main(String args[]){
        Student stu = new Student("ustc");
        //stu.setAge(10);

        System.out.println(stu.getAge());
        System.out.println(stu.getSchool());
        stu.printInfo();
        
    }
}

编译运行结果

第2课第3节_Java面向对象编程_继承性_P

5、抽象类规定子类必须实现的方法,起“模板”作用,缺点不能实例化对象,子类必须覆写全部抽象方法

abstract class Father {
    private int money;    

    public int getMoney() {return money; }
    public void setMoney(int money) {this.money = money; }

    public abstract void study();
}

class Son extends Father{
    public void study() {System.out.println("I am study"); }
    
}

public class Ext6 {
    public static void main (String args[]) {
        //Father f = new Father();
        Son son = new Son();
        son.study();

    }
}

编译运行结果:

第2课第3节_Java面向对象编程_继承性_P

6、作用:跟抽象类相似,起“模板”作用;子类可以继承多个接口,突破“单继承”的限制

abstract class Father{
    private int money;

    public int getMoney(){return money;}
    public void setMoney(int money){
        this.money = money;
    }
    public abstract void study();
}


interface A{
    public static final int i = 10;
    public abstract int getNum();
}

interface B{
    public static String name = "InterfaceB";
    public abstract String getName();
}

    

class Son extends Father implements A,B{
    public int getNum(){return i;}
    public String getName(){return name;}
    
    public void study(){
        System.out.println("I am study");
    }
}


public class Ext6{
    public static void main(String args[]){
        Son son = new Son();
        System.out.println(son.getName());
        System.out.println(son.getNum());
        son.study();
        
    }
}

编译运行结果:

第2课第3节_Java面向对象编程_继承性_P

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

上一篇:经典k3cloud学习文章


下一篇:Spring设值注入(一)