错误:应为类、委托、枚举、接口或结构

本文关键字:枚举 接口 结构 委托 错误 | 更新日期: 2023-09-27 18:20:06

修复了代码,并且在末尾仍然显示错误。其中public void displayCharacterDetails()是.

public class Character
{
    private string char_name;
    private string char_descr;
    private byte char_level;
    private byte char_attack;
    private byte char_defence;
    private bool char_defeat;
    public Character(string name, string desc, byte level, byte attack, byte defence, bool defeat)
    {
        char_name = name;
        char_descr = "";
        char_level = level;
        char_attack = attack;
        char_defence = defence;
        char_defeat = defeat;
    }
    public string GetCharacterName()
    {
        return char_name;
    }
    public string GetCharacterDescription()
    {
        return char_descr;
    }
    public void SetCharacterDescription(string descr)
    {
        char_descr = descr;
    }
    public byte GetCharLevel()
    {
        return char_level;
    }
    public byte GetCharacterAttack()
    {
        return char_attack;
    }

    public byte GetCharacterDefence()
    {
        return char_defence;
    }
    public void resetCharacter(string name, string descr, byte level, byte attack, byte defence, bool defeat)
    {
        char_name = name;
        char_descr = "";
        char_level = level;
        char_attack = attack;
        char_defence = defence;
        char_defeat = defeat;
    }
    public void displayCharacterDetails() 
    { 
System.out.println("Character name: " + char_name); 
System.out.println("Character description" + char_descr); 
System.out.println("Character Level, Attack, Defence: " + char_level + " ,"+ char_attack + "  ,"+ char_defence);  
    }
}

错误:应为类、委托、枚举、接口或结构

删除末尾的分号;

public Character(string char_name, string char_desc, byte char_level, 
byte char_attack, 
byte char_defence, bool char_defeat);   //<< - remove the semicolon 

将第一行替换为:

public Character(string char_name, string char_desc, byte char_level, byte char_attack, byte char_defence, bool char_defeat)

从方法声明中删除;

此外,这里缺少一个括号:

public byte getCharacterAttack() 
{ 

而且,不能像私有字段那样命名方法。

您有一个名为char_level的字段(如使用this.char_level的构造函数中所示)和一个名称相同的方法:

public byte char_level() 
{ 
return char_level; 
}  

重命名字段或解决该问题的方法。

最后,在您的构造函数中,您有名为lkike byte char_level的参数,但您有this.char_level = level;。将其替换为每个参数的this.char_level = char_level;,或者简单地仅调用每个参数byte level


所以基本上你应该有这个:

public class Character
{
    private string char_name;
    private string char_descr;
    private byte char_level;
    private byte char_attack;
    private byte char_defence;
    private bool char_defeat;
    public Character(string name, string desc, byte level, byte attack, byte defence,
                     bool defeat)
    {
        char_name = name;
        char_descr = "";
        char_level = level;
        char_attack = attack;
        char_defence = defence;
        char_defeat = defeat;
    }
    public string GetCharacterName()
    {
        return char_name;
    }
    public string GetCharacterDescription()
    {
        return char_descr;
    }
    public void SetCharacterDescription(string descr)
    {
        char_descr = descr;
    }
    public byte GetCharLevel()
    {
        return char_level;
    }
    public byte GetCharacterAttack()
    {
        return char_attack;
    }
}

我重命名了您的方法以遵循C#约定。我还删除了this关键字,这些参数名称不需要它。此外,请避免在留置权之间留出太多空白,因为很难阅读。


或者你可以简单地使用C#属性:

public class Character
{
    public string Name { get; set; }
    public string Descr { get; set; }
    public byte Level { get; set; }
    public byte Attack { get; set; }
    public byte Defence { get; set; }
    public bool Defeat { get; set; }
{

并创建这样的新字符:

Character character = new Character { Name = "the name", Descr = "the descr" } //etc.
public Character(string char_name, string char_desc, byte char_level,  
     byte char_attack, byte char_defence, bool char_defeat);  

不是如何声明构造函数。你想要这个:

public Character(string char_name, string char_desc, byte char_level,  
     byte char_attack, byte char_defence, bool char_defeat)  
{  
   ...  
}

注意删除了分号;

您的赋值向后看,如果您传入一个名为char_name的参数,并且看起来有一个名称为name的类字段,那么构造函数中的代码行应该是this.name=char_name;在您的set方法中也存在同样的问题。

在构造函数后面加一个分号。