OOP继承作业(动物到狮子超类继承)

我需要:使用 Lion 类扩展动物类并具有不同的功能(完成)。


添加一个名为 Liontype 类的字段,并添加一个根据重量对狮子类型进行分类的方法。(需要从超类派生)


并打印出来。


我的代码中有错误,我一直在尝试修复它。提前感谢您的帮助。


public class Animal {

  private int numTeeth = 0;

  private boolean spots = false;

  private int weight = 0;


  public Animal(int numTeeth, boolean spots, int weight){

    this.setNumTeeth(numTeeth);

    this.setSpots(spots);

    this.setWeight(weight);

  }


  public int getNumTeeth(){

    return numTeeth;

  }


  public void setNumTeeth(int numTeeth) {

    this.numTeeth = numTeeth;

  }


  public boolean getSpots() {

    return spots;

  }


  public void setSpots(boolean spots) {

    this.spots = spots;

  }


  public int getWeight() {

    return weight;

  }


  public void setWeight(int weight) {

    this.weight = weight;

  }

}


//Extend animal class

 public class Lion extends Animal{

  public Lion (int numTeeth, boolean spots, int weight){

  super(numTeeth, spots, weight);

  //Add new attributes

    int age = 0;

    int cubs = 0;

  }


 public static void main(String args[]){

   //Create lions and assign attributes

   Lion lion1 = new Lion();

   lion1.numTeeth = 12;

   lion1.spots = 1;

   lion1. weight = 86;

   lion1.age = 7;

   lion1.cubs = 3;


    //Print attributes

    System.out.println("Lion1 attributes:");

    System.out.println("Number of teeth : " + numTeeth);

    System.out.println("Number of spots : " + spots);

    System.out.println("Weight of lion : " + weight + " kgs");

    System.out.println("Age : " + age);

    System.out.println("No of cubs : " + cubs);

    System.out.println(" ");


   Lion lion2 = new Lion();

   lion2.numTeeth = 16;

   lion2.spots = 0;

   lion2. weight = 123;

   lion2.age = 13;

   lion2.cubs = 5;


    System.out.println("Lion2 attributes:");

    System.out.println("Number of teeth : " + numTeeth);

    System.out.println("Number of spots : " + spots);

    System.out.println("Weight of lion : " + weight + " kgs");

    System.out.println("Age : " + age);

    System.out.println("No of cubs : " + cubs);

    System.out.println(" ");

}




不负相思意
浏览 143回答 2
2回答

哈士奇WWW

除了 Dmitry 指出的错误外,您main还有以下错误: public static void main(String args[]){   //Create lions and assign attributes   Lion lion1 = new Lion();   lion1.numTeeth = 12;   lion1.spots = 1;   lion1. weight = 86;   lion1.age = 7;   lion1.cubs = 3;numTeeth spots weight并且所有其他字段都设置为私有。您的Lion班级无法直接访问这些字段。你应该使用你的 getters 和 setters 你从Animal同样在打印属性时Lion: //Print attributes    System.out.println("Lion1 attributes:");    System.out.println("Number of teeth : " + numTeeth);    System.out.println("Number of spots : " + spots);    System.out.println("Weight of lion : " + weight + " kgs");    System.out.println("Age : " + age);    System.out.println("No of cubs : " + cubs);    System.out.println(" ");您的字段是对象的属性。尝试直接打印字段会给你一个编译器错误,因为这些是你的Lion1对象的属性。您需要像这样使用点运算符:System.out.println("Number of Teeth" + Lion1.getNumTeeth());

忽然笑

是的,你的代码中有很多问题会在编译阶段得到。也许您错误地指定了示例。因此,请提供您的问题的详细信息。我会指出一些显而易见的:你声明了局部变量年龄 = 0; 国际幼崽= 0;在实际上不使用Lion新属性扩展类的构造函数中。将这些属性作为字段添加到类中Animal:private int age = 0; private int cubs = 0;然后在类的构造函数中初始化它们Lion(如果需要)。在方法中public static void main(String args[]),您试图使用 它没有的Lion类字段。age, cubs见第 1 点。该类有public Integer getWeight()2Liontype个错误。首先,变量weight未定义,其次缺少返回语句,尽管该方法必须返回一个Integer值。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java