其他分享
首页 > 其他分享> > 继承

继承

作者:互联网

语法:class 子类 extends 父类{} //定义子类时,显示继承父类

package com.qf.day13.t1.inherit;

public class TestExtends {
    public static void main(String[] args) {
        
        Dog dog1 = new Dog();
        dog1.breed = "哈士奇"; //继承自父类
        dog1.age = 2;  //继承自父类
        dog1.sex = "公";  //继承自父类
        dog1.furColor = "灰白"; //子类独有的
        
        dog1.eat();  //继承自父类
        dog1.sleep();  //继承自父类
        dog1.run(); //子类独有的
        
        //------------------------------------------
        
        Bird bird1 = new Bird();
        bird1.breed = "麻雀";
        bird1.age = 1;
        bird1.sex = "雄";
        bird1.furColor = "棕";
        
        bird1.eat();
        bird1.sleep();
        bird1.fly();
        
    }
}

//父类
class Animal{
    String breed;
    int age;
    String sex;
    
    public void eat() {}
    public void sleep() {}
}


class Dog extends Animal{ //完成继承关系
    String furColor;

    public void run() {}
}


class Bird extends Animal{
    String furColor;

    public void fly() {}
}


class Fish extends Animal{
    public void swim() {}
}


class Snake extends Animal{
    public void climb() {}
}

 

标签:继承,void,public,自父,bird1,class,dog1
来源: https://www.cnblogs.com/ht955/p/16368668.html