创建对象时如何解决“错误:找不到符号”?

时间:2019-03-27 05:50:10

标签: java class inheritance methods

考虑到我不是已经在我的Weight方法中做到了,我不理解我的编译器如何找不到类public boolean checkWeight?编译器指向以下行:

`Weight first = new Weight();`

`Weight second = new Weight();`

public class Guard {

    int stashWeight;

    public Guard ( int maxWeight ) {
        this.stashWeight = maxWeight;
    }

    public boolean checkWeight (Weight maxWeight) {
        if ( maxWeight.weight >= stashWeight ) {
            return true;
        } else {
            return false;
        }
    }

    public static void main (String[] args ) {
        Weight first = new Weight();
        first.weight = 3000;
        Weight second = new Weight();
        second.weight = 120;

        Guard grams = new Guard(450);
        System.out.println("Officer, can I come in?");
        boolean canFirstManGo = grams.checkWeight(first);
        System.out.println(canFirstManGo);

        System.out.println();

        System.out.println("Officer, how about me?");
        boolean canSecondManGo = grams.checkWeight(second);
        System.out.println(canSecondManGo);

    }
}

4 个答案:

答案 0 :(得分:0)

您应该像这样定义Weight类:

public class Weight {

    public int weight;

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }
}

输出:

Officer, can I come in?
true

Officer, how about me?
false

或使用Weight关键字导入import类。

答案 1 :(得分:0)

您的代码中缺少您的Weight类,请参见以下代码

class Weight {

    public int weight;

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }
}

public class Guard {

    int stashWeight;

    public Guard(int maxWeight) {
        this.stashWeight = maxWeight;
    }

    public boolean checkWeight(Weight maxWeight) {
        if (maxWeight.weight >= stashWeight) {
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) {
        Weight first = new Weight();
        first.weight = 3000;
        Weight second = new Weight();
        second.weight = 120;

        Guard grams = new Guard(450);
        System.out.println("Officer, can I come in?");
        boolean canFirstManGo = grams.checkWeight(first);
        System.out.println(canFirstManGo);

        System.out.println();

        System.out.println("Officer, how about me?");
        boolean canSecondManGo = grams.checkWeight(second);
        System.out.println(canSecondManGo);

    }
}

输出

**

Officer, can I come in?
true
Officer, how about me?
false

**

答案 2 :(得分:0)

您的体重课程是否在其他包装中? Weight类的访问修饰符是什么?

请在上方进行验证,然后在下面的文档中查看。

Access Modifiers

答案 3 :(得分:0)

  1. 方法参数“ maxWeight”的类型为“ Weight”,Java无法为其找到定义来解析程序依赖项。未能通过类路径找到该类将导致错误“找不到符号”。 您可能想要定义“重量”类并将其导入,就像其他人对您的回答一样。
  2. 您真的需要“体重”课吗?您只需将传递的参数与“ Guard”的stashWeight进行比较。

您的公共方法可能只是:

public boolean checkWeight(int maxWeight) {
        return maxWeight >= stashWeight;

}

主要方法可以更新为:

public static void main(String[] args) {
    int firstWeight = 3000;
    int secondWeight = 120;

    Guard grams = new Guard(450);
    System.out.println("Officer, can I come in?");
    boolean canFirstManGo = grams.checkWeight(firstWeight);
    System.out.println(canFirstManGo);

    System.out.println();

    System.out.println("Officer, how about me?");
    boolean canSecondManGo = grams.checkWeight(secondWeight);
    System.out.println(canSecondManGo);

}