Java 简单构造函数生成错误

Java 简单构造函数生成错误,java,constructor,private-methods,public-method,Java,Constructor,Private Methods,Public Method,对于战斗机类的简单构造函数,我遇到了很多错误。此外,它们似乎是表达错误的非法开始或;大部分情况下,预计会出现错误 这是我的代码: class Fighter(String name, int health, int attack, int defense){ //default constructor //input constructor private String name; private int health; private int

对于战斗机类的简单构造函数,我遇到了很多错误。此外,它们似乎是表达错误的非法开始;大部分情况下,预计会出现错误

这是我的代码:

class Fighter(String name, int health, int attack, int defense){
   //default constructor
   //input constructor

      private String name;
      private int health;
      private int attack;
      private int defense;

      private String setName(){
         this.name = name;
      }

      public String getName(){
         return name;
      }

      private int setHealth(){
         this.health = health;
      }

      public int getHealth(){
         return health;
      }

      private int setAttack(){
         this.attack = attack;
      }

      public int getAttack(){
         return attack;
      }

      private int setDefense(){
         this.defense = defense;
      }

      public int getDefense(){
         return defense;
      }
    }

我在做什么来产生所有这些错误?它与私有或公共方法有关吗?

方法在类中,而不是构造函数中。我想你想要的是

class Fighter {
    public Fighter(String name, int health, int attack, int defense) {
        setName(name);
        setHealth(health);
        setAttack(attack);
        setDefense(defense);
    }

    private String name;
    private int health;
    private int attack;
    private int defense;

    private void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    private void setHealth(int health) {
        this.health = health;
    }

    public int getHealth() {
        return health;
    }

    private void setAttack(int attack) {
        this.attack = attack;
    }

    public int getAttack() {
        return attack;
    }

    private void setDefense(int defense) {
        this.defense = defense;
    }

    public int getDefense() {
        return defense;
    }
}
这不是定义构造函数的方式。在创建对象时调用构造函数(
newfighter()
)。它是一种特殊的方法,因此只能包含语句。这:

private String setName(){
     this.name = name;
}
不是一个语句,所以这就是为什么会有错误。以上是另一种单独的方法。您应该在类中定义此类内容,而不是在构造函数中。像这样把它们拔出来:

public class Fighter {
    public Fighter(String name, int health, int attack, int defense) {
        setName(name);
        setHealth(health);
        setAttack(attack);
        setDefense(defense);
    }

    private String name;
    private int health;
    private int attack;
    private int defense;

    private void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    private void setHealth(int health) {
        this.health = health;
    }

    public int getHealth() {
        return health;
    }

    private void setAttack(int attack) {
        this.attack = attack;
    }

    public int getAttack() {
        return attack;
    }

    private void setDefense(int defense) {
        this.defense = defense;
    }

    public int getDefense() {
        return defense;
    }
}

现在所有的方法都在构造函数之外。字段、方法和构造函数是类的组件,而不是构造函数的组件。

这不是定义构造函数的方式。