【问题标题】:my variable cannot be resolved into a type in java我的变量无法解析为 java 中的类型
【发布时间】:2018-01-16 21:01:20
【问题描述】:

它一直说 'Cow' 不能被解析为一个类型。我是 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);
    }
}

【问题讨论】:

  • 你确定有一个“牛”类吗?牛.java ?
  • 你需要一个名为 Cow 的类才能声明该类型的对象。
  • 当你初始化它时,你必须给它一个像 Cow c = new Cow(rest here); 这样的类型假设你有一个奶牛类

标签: java types get set


【解决方案1】:

我假设这是您要创建的自定义类? 由于您是 Java 新手,我还假设您不知道应该创建它?

在任何情况下,按照以下方式创建您的“Cow”类(随意删除多余的构造函数):

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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    • 2011-11-27
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    相关资源
    最近更新 更多