4.定义一个抽象类AbsAnimal,要求如下 (1)包括属性name,weight,color; (2)定义其有参构造方法; (3)定义受保护的run方法,功能是打印输出“Animal run fa

4.定义一个抽象类AbsAnimal,要求如下
(1)包括属性name,weight,color;
(2)定义其有参构造方法;
(3)定义受保护的run方法,功能是打印输出"Animal run fastly";
(4)在main方法里实例化抽象对象a
AbsAnimal a = new AbsAnimal(….) ,想一想为什么会报错?如果报错注释掉该语句。

package cn.edu.ahtcm.bean;

public abstract class AbsAnimal {
    
    
    String name;
    int weight;
    String color;
    public AbsAnimal(String name,int weight,String color){
    
    
        this.name = name;
        this.weight = weight;
        this.color = color;
    }
    protected void run(){
    
    
        System.out.println("Animal run fastly");
    }
    public static void main(String[] args){
    
    
        /*AbsAnimal a = new AbsAnimal("猫",5,"黑色");
        因为抽象类不能实例化对象*/

    }
}

5.定义一个类Tiger继承抽象类AbsAnimal,要求如下
(1)在main方法中实例化一个Tiger对象
(2)调用run方法,查看输出结果

package cn.edu.ahtcm.bean;

public class Tiger extends AbsAnimal{
    
    

    public Tiger(String name, int weight, String color) {
    
    
        super(name, weight, color);
    }
    public static void main(String[] args){
    
    
            Tiger a = new Tiger("小花",6,"黄色");
            a.run();
    }
}

猜你喜欢

转载自blog.csdn.net/winds_tide/article/details/116949592
今日推荐