【问题标题】:How to fix "Error: Cannot find symbol" when creating an object?创建对象时如何解决“错误:找不到符号”?
【发布时间】:2019-08-17 15:07:07
【问题描述】:

我不明白我的编译器如何找不到类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);

    }
}

【问题讨论】:

  • 你导入正确的Weight类了吗?
  • 你的体重等级在哪里
  • 我可能已经昏迷了。出于某种未知的原因,我的新手头脑认为我已经做了我的体重等级:public boolean checkWeight (maxWeight Weight)。感谢大家的提醒

标签: java class inheritance methods


【解决方案1】:

您应该像这样定义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

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

【讨论】:

    【解决方案2】:

    您的代码中缺少您的 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
    

    **

    【讨论】:

      【解决方案3】:

      您的体重等级是否在不同的包装中? Weight 类的访问修饰符是什么?

      请在上面验证并查看下面的文档。

      Access Modifiers

      【讨论】:

        【解决方案4】:
        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);
        
        }
        

        【讨论】:

          猜你喜欢
          • 2020-08-22
          • 2013-01-15
          • 1970-01-01
          • 2018-08-16
          • 1970-01-01
          • 2021-10-09
          • 1970-01-01
          • 2011-09-14
          • 2013-11-15
          相关资源
          最近更新 更多