简体   繁体   English

从 class 创建新的 object 时出现“找不到符号”错误?

[英]'Cannot find symbol' Error when creating a new object from a class?

public class Monster{

public final String TOMBSTONE = "Here Lies a Dead monster";


private int health = 500;
private int attack = 20;
private int movement = 2;

public String name = "Big Monster";


public int getAttack()
{
    return attack;
}

public int getMovement()
{
    return movement;
}

public int getHealth()
{
    return health;
}

public Monster(int health, int attack, int movement)
{
    this.health = health;
    this.attack = attack;
    this.movement = movement;

}

public Monster()
{

}}


public class Frank {

public static void main(String[] args){

    Monster NewMonster = new Monster();

    NewMonster.name = "Frank";

    System.out.println(NewMonster.name + " has an attack value of " + NewMonster.getAttack());

}

} }

When trying to create a new object from my Monster class I get this error:尝试从我的 Monster class 创建新的 object 时,出现此错误:

Frank.java:5: error: cannot find symbol
            Monster NewMonster = new Monster();
            ^

symbol: class Monster location: class Frank符号:class 怪物位置:class 弗兰克

I am very new to Java so sorry if this is a simple/easy fix but everything I have researched does not give me a solution to this error.我对 Java 很陌生,所以很抱歉,如果这是一个简单/容易的修复,但我研究的所有内容都没有给我解决这个错误的方法。

Thanks in advance for any replies/feedback.提前感谢您的任何回复/反馈。

You are not allowed to have multiple public classes in one file in Java. Java 的一个文件中不允许有多个公共类。 Therefore you need to either remove a public modifier of one of your classes or put the main method in your public class.因此,您需要删除其中一个类的公共修饰符,或者将主要方法放在公共 class 中。 I did the latter, since it is not necessary to place the main method in a seperate class just to instantiate your object.我做了后者,因为没有必要将主要方法放在单独的 class 中来实例化您的 object。

I have updated your code and it runs now我已经更新了你的代码,它现在可以运行了

public class Monster{

    public static void main(String[] args) {

        Monster NewMonster = new Monster();

        NewMonster.name = "Frank";

        System.out.println(NewMonster.name + " has an attack value of 
        " + NewMonster.getAttack());
    }

    public final String TOMBSTONE = "Here Lies a Dead monster";


    private int health = 500;
    private int attack = 20;
    private int movement = 2;

    public String name = "Big Monster";


    public int getAttack() {
        return attack;
    }

    public int getMovement(){
        return movement;
    }

    public int getHealth(){
        return health;
    }

    public Monster(int health, int attack, int movement){
        this.health = health;
        this.attack = attack;
        this.movement = movement;
    }

    public Monster(){}
}

1 You have to define only one class a public which will be saved as per your java file name (.java) 1 您必须只定义一个 class 一个公共,它将根据您的 java 文件名(.java)保存

2 object references will be always in lower case 2 object 引用将始终为小写

class Monster { class 怪物{

public final String TOMBSTONE = "Here Lies a Dead monster";

private int health = 500;
private int attack = 20;
private int movement = 2;

public String name = "Big Monster";

public int getAttack() {
    return attack;
}

public int getMovement() {
    return movement;
}

public int getHealth() {
    return health;
}

public Monster(int health, int attack, int movement) {
    this.health = health;
    this.attack = attack;
    this.movement = movement;

}

public Monster() {

}

} // only Frank can be a public class in a single java file, or else create two different java classes and import. } // 只有 Frank 可以是单个 java 文件中的公共 class,否则创建两个不同的 java 类并导入。 public class Frank {公共 class 弗兰克 {

public static void main(String[] args){公共 static 无效主(字符串 [] 参数){

Monster newMonster = new Monster();

newMonster.name = "Frank";

System.out.println(newMonster.name + " has an attack value of " + newMonster.getAttack());

} } } }

Output: Frank has an attack value of 20 Output:弗兰克的攻击值为20

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在创建新的类对象时,另一个“找不到符号”错误 - Yet another 'cannot find symbol' error when creating a new class object 创建 object 时找不到符号错误 - Cannot Find Symbol Error when creating an object 在新的 class 中创建方法; 在主 class 中找不到符号错误 - creating method in new class; cannot find symbol error in main class 使用继承在IO类中创建对象时找不到符号 - Cannot find symbol when creating an object in IO class using inheritance 创建对象时如何解决“错误:找不到符号”? - How to fix “Error: Cannot find symbol” when creating an object? 创建对象实例时出现“找不到符号”错误 - "Cannot find symbol" error when creating an instance of an object 声明新的午餐包对象时出现“错误:找不到符号”? - 'error: cannot find symbol' when declaring a new lunchBag object? Java:从其他类引用对象时找不到符号 - Java: cannot find symbol when reference an object from other class 创建新的字符串数组时类型不兼容,访问另一个类变量时找不到符号 - Incompatible types when creating new string array and cannot find symbol when accessing another class variable 创建对象并调用方法时出错,错误:找不到符号 - Error creating an object and calling a method, error: cannot find symbol
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM