简体   繁体   English

Java - 调用构造函数时如何使用类属性中的值

[英]Java - how to use a value from a class property when calling a constructor

I have the following problem:我有以下问题:

  1. Build the classes Animal , Cat , and Bug .构建类AnimalCatBug
  2. Define the properties color and leg_number on the relevant and necessary classes.在相关和必要的类上定义属性colorleg_number Have them be initialized within a constructor.让它们在构造函数中初始化。
  3. Add the functionality that would allow us to call a method move with the Cat and Bug classes.添加允许我们使用CatBug类调用方法move的功能。 Have the method return a string "I'm moving with leg_number legs!", with the " leg_number " being leg_number as set on the class.让该方法返回一个字符串“我正在用leg_number腿移动!”,其中“ leg_number ”是leg_number中设置的leg_number
  4. Add a new class called Bird .添加一个名为Bird的新类。 Add the property wing_number .添加属性wing_number Add the functionality that would allow us to call a method move with the Bird class.添加允许我们使用Bird类调用方法move的功能。 Have the method return the string "I'm moving with leg_number legs!"让该方法返回字符串“我正在用leg_number腿移动!” if wing_number doesn't have an applicable value.如果wing_number没有适用的值。 If wing_number does have an applicable value, return the string "I'm flying".如果wing_number确实具有适用的值,则返回字符串“我正在飞行”。

My code thus far is as follows:到目前为止,我的代码如下:

class Animal {

    protected String color;
    protected int leg_number;

    public Animal(String color, int leg_number) {
        this.color = color;
        this.leg_number = leg_number;
    }

    public void move() {
        System.out.println("I'm moving with " + leg_number + " legs!");
    }
}

class Cat extends Animal {
    String color;
    int leg_number;

    public Cat(String color, int leg_number) {
        super(color, leg_number);
        this.color = "orange";
        this.leg_number = 4;
    }

    @Override
    public void move() {
        System.out.println("I'm moving with " + leg_number + " legs!");
    }
}

class Bug extends Animal {
    String color;
    int leg_number;

    public Bug(String color, int leg_number) {
        super(color, leg_number);
        this.color = "green";
        this.leg_number = 16;
    }

    @Override
    public void move() {
        System.out.println("I'm moving with " + leg_number + " legs!");
    }
}

class Bird extends Animal {
    String color;
    int leg_number;
    int wing_number;

    public Bird(String color, int leg_number, int wing_number) {
        super(color, leg_number);
        this.color = "yellow";
        this.leg_number = 2;
        this.wing_number = wing_number;
    }

    public void move(int wing_number) {
        if (wing_number > 0) {
            System.out.println("I'm flying");
        } else {
            System.out.println("I'm moving with " + leg_number + " legs!");
        }
    }
}

I think I did what the instructions called for in setting the value for leg_number in the constructor.我想我按照说明在构造函数中设置 leg_number 的值做了什么。 I'm not sure how to use it in the constructor call, though.不过,我不确定如何在构造函数调用中使用它。 It seems that if I have Animal myCat = new Cat("orange", 4);看来,如果我有Animal myCat = new Cat("orange", 4); that I'm not following the instructions for the assignment.我没有按照作业的说明进行操作。 But I'm not sure what else to put there.但我不知道还有什么可以放在那里。 I've tried using (color, leg_number) and (this.color, this.leg_number) , but as expected, they didn't work (I assume because the object hasn't been instantiated yet).我试过使用(color, leg_number)(this.color, this.leg_number) ,但正如预期的那样,它们不起作用(我假设是因为对象尚未实例化)。 I tried setting the values in the parameter list of the constructor itself, but that didn't work either.我尝试在构造函数本身的参数列表中设置值,但这也不起作用。 I tried having the constuctor take no parameters, but I have to call the super constructor which does require parameters, so I get an error that way.我尝试让构造函数不带参数,但我必须调用确实需要参数的super构造函数,所以我会以这种方式得到错误。

What am I missing here?我在这里缺少什么?

EDIT: Where I'm trying to instantiate these is in the Main method.编辑:我试图在 Main 方法中实例化这些。

You don't need String color and int leg_number in your sub-classes.您的子类中不需要String colorint leg_number You also don't need to override move() method in all sub-classes, except Bird .您也不需要覆盖所有子类中的move()方法,除了Bird Additionally, your move() method in Bird shouldn't take any parameters but use wing_number property instead.此外,您在Bird move()方法不应采用任何参数,而应使用wing_number属性。 Having said that, try the following:话虽如此,请尝试以下操作:

class Animal {

    protected String color;
    protected int leg_number;

    public Animal(String color, int leg_number) {
        this.color = color;
        this.leg_number = leg_number;
    }

    public void move() {
        System.out.println("I'm moving with " + leg_number + " legs!");
    }
}
class Cat extends Animal {
    public Cat(String color, int leg_number) {
        super(color, leg_number);
    }
}
class Bug extends Animal {
    public Bug(String color, int leg_number) {
        super(color, leg_number);
    }
}
class Bird extends Animal {
    private int wing_number;

    public Bird(String color, int leg_number, int wing_number) {
        super(color, leg_number);
        this.wing_number = wing_number;
    }

    @Override
    public void move() {
        if (wing_number > 0) {
            System.out.println("I'm flying");
        } else {
            super.move();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Java中调用Kotlin时,如何使用默认值省略构造函数参数? - How to omit the constructor parameter with a default value when calling Kotlin in Java? Java中的骰子游戏(从带有构造函数的类中调用) - dice game in java (calling from a class with a constructor) 当从另一个类调用方法时-重新获得一个难以理解的值JAVA - when calling method from another class - retuns an incomprehensible value JAVA 如何在JAVA中将构造函数中的值传递给另一个类中的setter? - How to pass a value from a constructor to a setter in another class in JAVA? 在Java中从派生类调用基类构造函数 - Calling a base class constructor from derived class in Java Java,除了其中一个之外,如何停止继承类调用特定的抽象类构造函数? - Java, how to stop inheriting classes from calling a particular abstract class constructor, apart from one of them? 如何在类的构造函数中使用泛型类型(Java) - How use a generic type in constructor of class (Java) 将Java转换为Scala,如何处理调用超类构造函数? - Converting Java to Scala, how to deal with calling super class constructor? Java:当java.io具有受保护的构造函数时,如何从java.io为Reader类创建新的类对象 - Java: How to create new class object for Reader class from java.io when it has protected constructor 从派生类调用构造函数 - calling a constructor from a derived class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM