Java构造函数具有默认值

发布于 2025-01-26 05:08:26 字数 549 浏览 2 评论 0原文

读书。

我需要创建构造函数,在一个情况下,它在下面具有默认值示例。 当然,此代码是错误的,如果“狗”构造函数不会要求参数:体重,年龄并将填补班级字段:体重= 1,年龄= 1。 例如,在另一种情况下,猫会要求所有参数?这个问题通常如何解决?

public Animal(String spacies, String name, double weight, byte age, boolean isALive){

    if (spacies.equals("dog")) {

        this.name = name;
        this.weight= 1;
        this.age = 1;
        this.isALive = isALive;

    } else {
        this.spacies = spacies;
        this.name = name;
        this.weight = weight;
        this.age = age;
        this.isALive = isALive;
    }
}

thx for reading.

I need to create constructor, which in one case has default values example below.
this code is wrong of course, Is it possible to do that it means in case "dog" constructor will not ask for parameters: weight, age and will fill up class fields: weight=1, age=1.
In another case for example cat will ask for all parameters? How usually this problem is solved?

public Animal(String spacies, String name, double weight, byte age, boolean isALive){

    if (spacies.equals("dog")) {

        this.name = name;
        this.weight= 1;
        this.age = 1;
        this.isALive = isALive;

    } else {
        this.spacies = spacies;
        this.name = name;
        this.weight = weight;
        this.age = age;
        this.isALive = isALive;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明月松间行 2025-02-02 05:08:26

最好的方法是使用继承。动物是抽象实体。您应该从这些动物中获得特定的动物。
可以有不同的方法。试图提供一个简单的解决方案。

    public abstract class Animal {


    private final String species;
    private final String name;
    private final double weight;
    private final byte age;
    private final boolean isALive;

    public Animal(String species, String name, double weight, byte age, boolean isAlive) {
        this.species = species;
        this.name = name;
        this.weight = weight;
        this.age = age;
        this.isALive = isAlive;
    }

}

class Dog extends Animal {
    public Dog(String dogName, boolean isAlive) {
        super("Dog", dogName, 1.0, (byte) 1,isAlive);
    }
}

在研究OOPS概念时,请尝试与现实世界有关。

Best way is to use inheritance. Animal is abstract entity. You should derive specific animals from those.
There can be different approaches. Tried to provide one simple solution.

    public abstract class Animal {


    private final String species;
    private final String name;
    private final double weight;
    private final byte age;
    private final boolean isALive;

    public Animal(String species, String name, double weight, byte age, boolean isAlive) {
        this.species = species;
        this.name = name;
        this.weight = weight;
        this.age = age;
        this.isALive = isAlive;
    }

}

class Dog extends Animal {
    public Dog(String dogName, boolean isAlive) {
        super("Dog", dogName, 1.0, (byte) 1,isAlive);
    }
}

Try relating to real world when working on OOPS concepts.

隐诗 2025-02-02 05:08:26

创建一个设置这些值的第二个构造函数

public Animal(String spacies, String name, boolean isALive){
    this("dog", name, 1, 1, true)
}

Create a second constructor that sets these values

public Animal(String spacies, String name, boolean isALive){
    this("dog", name, 1, 1, true)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文