yg1a20 2020-12-01 22:39 采纳率: 7.1%
浏览 5

代码和问题如下,有人能帮一下吗

  • public class Wolf extends Carnivore{
        public Wolf(String name,int age){
            super(name,age);
        }
        public void makeNoise(){
            System.out.println("make wolf bark");
        }
    }
    
    
    public abstract class Carnivore extends Animal{
        public Carnivore(String name,int age){
            super(name,age);
        }
        public void makeNoise(){
            
        }
        public void eat(Food type) throws Exception{
            if (type instanceof Meat){
                System.out.println("right");
            }
            else{
                throw new Exception("error");
            }
        }
    }
    
    
    
    public class Main{
        public static void main(String[] args){
            Wolf wolf1 = new Wolf("wolf",10);
            Parrot parrot1 = new Parrot("parrot",10);
            Sheep sheep1 = new Sheep("sheep",10);
            Meat meat1 = new Meat("pork");
            Plant plant1 = new Plant("grass");
            wolf1.makeNoise();
            parrot1.makeNoise();
            try{
                wolf1.eat(plant1);
                sheep1.eat(meat1);
            }
            catch(Exception e){
                System.err.println("wrong");
            }
        }
    }
    
    
    create a second Animal constructor which takes no arguments.
  • call the other Animal constructor from within this second constructor, providing "newborn" and 0 (zero) as the arguments.
  • test this in your main method by creating a new Wolf with zero arguments.
  • you will have to create constructors with no arguments in both Wolf and Carnivore that call the super constructor.

 

  • 写回答

2条回答 默认 最新

  • 峰子哥哥 2020-12-02 11:09
    关注

    从报错信息来看 构造函数有问题,建议上传全部代码看看

    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题