我需要帮助创建构造函数并返回布尔值和字符串

时间:2016-05-15 00:06:32

标签: java boolean

我需要帮助修复我的代码与上面列出的基本概念。为了避免混乱,我在这里拍摄了方向的屏幕截图:https://imgur.com/SdiotUi

但是,当我运行我的代码时,它无法正常工作。我知道有很多错误但是我在修复它们时遇到了麻烦,即使我花了几个小时用谷歌搜索正确的方法来做到这一点。

当我创建第一个构造函数时,我不确定我是否正确分配名称和腿,我无法返回“true”,我收到一个错误,调用父类采取一个参数,我不认为我正确地覆盖了抽象类。

我的代码:

public class Animal1 {

    private String animalName;
    public int numberOfLegs;
    public Animal1(String name){
        name = animalName;
        name = "John";
    }
    public Animal1(String name, int legs){
        name = animalName;
        legs = numberOfLegs;
        name = "Jack";
        legs = 4;
    }

    public String getName(){
        return animalName;
    }

    public int getLegs(){
        return numberOfLegs;
    }

    public void isAMammal(){ 
        return true;
    }

    public void isCarnivorous(){
        return true;
    }

    public abstract class getHello{

    }
}

public class Cat1 extends Animal1{

    public Cat1(String name){
        Animal1.name;
    }

    public abstract class getHello{   
        return "Meow";
    }
}

public class Dog1 extends Animal1{

    public Dog1(String name){
        Animal1.name;
    }

    public abstract class getHello{
        return "Woof";
    }
}

2 个答案:

答案 0 :(得分:0)

首先,看起来你的一些方法被声明为类。我假设你想让它们成为抽象的方法。他们需要改为:

public abstract String getHello();

请注意,抽象方法只能在抽象类中声明。因此,您需要将Animal1重新定义为抽象。

public abstract class Animal1

接下来,当您实现抽象方法时,将其定义为

public String getHello()

如果您使用像Eclipse这样的IDE,它将自动提供生成此方法。

最后,在像Cat1这样的子类中使用构造函数时,您正试图设置" name"好像它是一个静态变量并绕过你已经为Animal1设置的构造函数。更正此问题的最佳方法是更改​​Cat1和Dog1中的构造函数以调用超级构造函数。

public Cat1(String name){
    super(name);
}

答案 1 :(得分:0)

public abstract class Animal1 { // If you want to have an abstract method, declare the class as abstract

    private final String animalName;
    private final int numberOfLegs; // better of using private and make it final since it's not going to change.

    public Animal1(final String name, final int legs){ //better making the input parameters final since they are not supposed to be changed
        //name = animalName; 
        //legs = numberOfLegs;//it assigned the field to an input parameter. that will take no effect on the object created.
        animalName = name;
        numberOfLegs = legs;
    }

    public String getName(){
        return animalName;
    }

    public int getLegs(){
        return numberOfLegs;
    }

    public boolean isAnimal(){ //boolean function needs a return type too!!
        return true;
    }

    public boolean isCarnivorous(){
        return true;
    }

    public abstract String getHello(); // an abstract method has same requirement as a normal method besides the abstract modifier. it will need a return type. And it ends with a semicolon

}

public class Cat1 extends Animal1{

    public Cat1(final String name){
        super(name, 4); //use super to call parent constructor
    }

    @Override
    public String getHello(){   
        return "Meow";
    }
}

public class Dog1 extends Animal1{

    public Dog1(final String name){
        super(name, 4);
    }

    @Override
    public String getHello(){
        return "Woof";
    }
}