有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (4) 个答案

  1. # 1 楼答案

    否,传入构造函数的值未分配给字段。levelOfBreathing的值将保持为21

    编辑:

    编辑Animal的构造函数,使其如下所示

    public Animal(Integer levelOfBreathing) {
        this.levelOfBreathing = levelOfBreathing;
    }
    
  2. # 2 楼答案

    不,不是。通过在构造函数中指定levelOfBreathing作为参数,您确认levelOfBreathing的值将来自其他人

    将其更改为类似这样的内容,这会将levelOfBreathing更改为传递到构造函数中的任何内容

      public Animal(int levelOfBreathing) 
      {
         //the this keyword refers to an instance of Animal
         this.levelOfBreathing = levelOfBreathing;
      }
    
  3. # 3 楼答案

    我想你可能想在你的家长课上使用这个:

    public class Animal {
    
        private int levelOfBreathing = 21;
    
        public Animal(Integer levelOfBreathing)   {
            this.levelOfBreathing = levelOfBreathing;
        }
    }
    

    由于某种原因,您似乎为构造函数编写了RuntimeException而不是Animal

    请注意this.levelOfBreathing用于指定字段而不是构造函数参数,因为它们具有相同的名称

  4. # 4 楼答案

    不,我不这么认为。我认为您需要明确设置它,如

      public Animal(int levelOfBreathing) {
         // constructor for the Animal class
         this.levelOfBreathing = levelOfBreathing;
      }