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

  • 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.

 

查看全部
yg1a20
yg1a20
2020/12/01 22:39
  • java
  • 点赞
  • 收藏
  • 回答
    私信

1个回复