我的变量无法解析为java中的类型

时间:2018-01-16 21:01:21

标签: java types get set

它一直在说“牛”无法解析为某种类型。我是java新手,想要一些帮助!

public class UnoMas {
    public static void main(String[] args) {
        Cow c;
        c = new Cow("Gerard, Golgari Lich Lord");
        c.setPower(0);
        c.setToughness(0);
        speakToTheGolgari(c);
        System.out.println(c.getPower);
        System.out.println(c.getToughness);
    }

    public static void speakToTheGolgari(Cow d) {
        d.setPower(d.getPower() + 2);
        d.setToughness(d.getToughness() + 2);
    }
}

1 个答案:

答案 0 :(得分:0)

我假设这是一个您打算创建的自定义类? 既然你是Java的新手,我也假设你不知道你应该创建它吗?

无论如何,创造你的'牛''如下所示的类(随意删除冗余构造函数):

public class Cow {
    private String name;
    private int power;
    private int toughness;

    public Cow() {
    }

    public Cow(String name) {
        this.name = name;
        this.power = power;
        this.toughness = toughness;
    }

    public Cow(String name, int power) {
        this.name = name;
        this.power = power;
        this.toughness = toughness;
    }

    public Cow(String name, int power, int toughness) {
        this.name = name;
        this.power = power;
        this.toughness = toughness;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPower() {
        return power;
    }

    public void setPower(int power) {
        this.power = power;
    }

    public int getToughness() {
        return toughness;
    }

    public void setToughness(int toughness) {
        this.toughness = toughness;
    }
}