如何正确配置多个构造函数? [英] How to correctly configure multiple constructors?

查看:115
本文介绍了如何正确配置多个构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于继承进行分配,并且我创建了2个构造函数,假定它们可以做不同的事情.一个构造函数没有任何参数,并且应该产生一个预定义的值,另一个构造函数具有2个参数,它们由名称和类型为String和int的年龄组成.我以某种方式重新配置了两个构造函数,以使它们都不产生应有的外观.以下是这些构造函数在其中调用的类:

I'm doing an assignment based around inheritance and I have created 2 constructors that are suppose to do different things. One constructor does not have any parameters and should produce a pre-defined value, the other constructor has 2 parameters which consist of a name and an age of types String and int. I have somehow reconfigured the two constructors so that they both do not produce what they should be. Here is the classes that these constructors are invoked in:

动物(超一流)

abstract public class Animal implements Comparable<Animal>
{
    int age;
    String name;

    Animal(String name, int age)   
    {
        this.age = age;
        this.name = name;
    } 

    Animal()
    {   
         this("newborn", 0);
    }        

    public int getAge()
    {
         return age;
    }

    public void setName(String newName)
    {
        name = newName;
    }

    String getName()
    {
        return name;
    }
}

食肉动物

public class Carnivore extends Animal
{
    Carnivore(String name, int age)   
    {
        this.age = age;
        this.name = name;
    }

    Carnivore()
    {
        super();
    }

    @Override
    public int compareTo(Animal o)
    {
        //To change body of generated methods, choose Tools | Templates.
        throw new UnsupportedOperationException("Not supported yet."); 
    }
}

public class Wolf extends Carnivore
{
    String name;
    int age;

    Wolf(String name, int age)   
    {  
        this.name = name;
        this.age = age;
    }

    Wolf()
    {
        super();
    }   

    String getName()
    {
        return name;
    }
}

主要方法

System.out.println("************1st constructor of Wolf************");
Wolf wolfExample = new Wolf("Bob", 2) {};        
System.out.println("Name = " + wolfExample.getName());
System.out.println("Age = " + wolfExample.getAge());

System.out.println("************2nd constructor of Wolf************");    
Wolf newWolf = new Wolf();
System.out.println("Name = " + newWolf.getName());
System.out.println("Age = " + newWolf.getAge());

实际输出

************1st constructor of Wolf************
Name = Bob
Age = 0
************2nd constructor of Wolf************
Name = null
Age = 0

预期产量

************1st constructor of Wolf************
Name = Bob
Age = 2
************2nd constructor of Wolf************
Name = newborn
Age = 0

年龄正在返回其默认值,第二个构造函数的名称也返回null,但我不太确定为什么.这是我第一次与多个构造函数一起工作,因此我有点困惑,因为它可以工作,所以任何帮助将不胜感激,谢谢.

The ages are returning their default value and the name for the second constructor is also returning null but I'm not too sure why. This is my first time working with multiple constructors so I'm a little confused as to ow it works so any help would be much appreciated, thanks.

推荐答案

您的基类似乎正确,但是您需要更改实现.

Your base class seems correct, but you need to change your implementations.

您的WolfCarnivore构造函数应为:

Wolf(String name, int age)   
{
    super(name, age);
}

原因是,您正在为每种类型设置 local 实例变量,但是调用超类的getAge()方法-这将获得超类的值age,该值的值尚未实际上已分配到任何地方,并且默认值为 0 .对于name,这是相同的,默认为 null .

Reason being, you are setting the local instance variables for each type, but calling getAge() method of the super class - this is getting the super's value of age, whose's value has not actually been assigned anywhere, and is given a default value of 0. This goes the same for name, which defaults to null.

您需要使用传递的变量调用super,并且不需要为每个扩展对象重新定义它们.

You need to call super with the passed variables, and do not need to redefine them for each extended object.

这篇关于如何正确配置多个构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆