开发者

Is levelOfBreathing set correctly to where it is a private integer property

public class Animal {

    public static void main(Str开发者_开发技巧ing[] args){

    }

    private int levelOfBreathing = 21;
    public Animal(Integer levelOfBreathing) {


No, the value passed into the constructor isn't being assigned to the field. The value of levelOfBreathing would remain 21.

Edit:

Edit the constructor of Animal to look like this.

public Animal(Integer levelOfBreathing) {
    this.levelOfBreathing = levelOfBreathing;
}


No, I don't believe so. I think you will need to explicitly set it, as in,

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


I think you may want this for your parent class:

public class Animal {

    private int levelOfBreathing = 21;

    public Animal(Integer levelOfBreathing)   {
        this.levelOfBreathing = levelOfBreathing;
    }
}

It looks like you wrote RuntimeException instead of Animal for the constructor for some reason.

Note that this.levelOfBreathing is used to specify the field rather than the constructor parameter, since they have the same name.


No it isn't. By specifying levelOfBreathing as a parameter into your constructor you are acknowledging that the value of levelOfBreathing will be coming from someone else.

change it to something like this, this will change levelOfBreathing to whatever you pass into the constructor

  public Animal(int levelOfBreathing) 
  {
     //the this keyword refers to an instance of Animal
     this.levelOfBreathing = levelOfBreathing;
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜