在Java中,是否可以在父类中为子类赋值?

huangapple go评论75阅读模式
英文:

Is it possible to give a value to a child class in its parent class in Java?

问题

我为此进行了大量搜索,但无论在哪里都找不到,但如果您认为以前已经有人问过,请给我发送链接。

我有一个名为chesspiece的父类,我有6个子类:
价值为1的Pawn,
价值为2的Knight,
价值为3的Bishop,
价值为5的Rook,
价值为9的Queen,
价值为1000的King。

我有两个实例变量:value和棋子的颜色。

我想知道如果你有价值1,你属于Pawn类,如果你有价值2,你属于Knight类。
所以,现在我正在使用一个方法来做这个,但它只返回一个字符串,没有其他内容。这是我的代码:

public class ChessPiece {
	private boolean isWhite;
	private int value;

	public ChessPiece(boolean isWhite, int value) {
		this.isWhite = isWhite;
		this.value = value;
	}

	public String getNamebyValue() {
		switch (value) {
			case 1:
				return "Pawn";
			case 2:
				return "Knight";
			case 3:
				return "Bishop";
			case 5:
				return "Rook";
			case 9:
				return "Queen";
			case 1000:
				return "King";
		}
		return null;
	}
}

这是我的子类:

public class Bishop extends ChessPiece {

	public Bishop(boolean isWhite) {
		super(isWhite, 3);
	}
}
英文:

I searched a lot for this but couldn't find it anywhere, but if you think that it had been asked before, please send me the link.

I have a parent class called chesspiece and I have 6 child classes:
Pawn with the value of 1,
Knight with the value of 2,
Bishop with the value of 3,
Rook with the value of 5,
Queen with the value of 9
and king with the value of 1000.

I have two instance variable: value and the color of the piece

I want to know is it possible to say if you have the value of 1 you belong to the pawn class or if you have the value of 2, you belong to the knight class.
So, right now I'm doing this using a method but it only returns a String and nothing more. Here's my code:

public class ChessPiece {
	private boolean 	isWhite;
	private int 		value;
	
	public ChessPiece (boolean isWhite, int value) {
		this.isWhite = isWhite;
		this.value = value;
	}
public String getNamebyValue() {
		switch (value) {
		  case 1:
		    return "Pawn";
		  case 2:
			return "Knight";
		  case 3:
			return "Bishop";
		  case 5:
			return "Rook";
		  case 9:
			return "Queen";
		  case 1000:
			return "King";
		}
		return null;
	} 

And here's my child class:

public class Bishop extends ChessPiece {

	public Bishop(boolean isWhite) {
		super(isWhite, 3);

	}

答案1

得分: 3

ChessPiece.java

public class ChessPiece {
    private boolean isWhite;
    private int value;
    private String name;

    public ChessPiece(boolean isWhite) {
        this.isWhite = isWhite;
    }

    public String getName() { return name; }
}

Bishop.java

public class Bishop extends ChessPiece {

    public Bishop(boolean isWhite) {
        super(isWhite);
        this.name = "Bishop";
        this.value = 3;
    }
    ...
}

In other words, let "class inheritance" do as much of the work as possible for you.

To answer your question: it's possible to share a variable between the parent and subclasses (as shown above).

It's also possible to override member variables in the child. Look here: link to StackOverflow

It all depends on your design - on your requirements, and on how you choose to implement those requirements.

英文:

Suggestion:

ChessPiece.java

public class ChessPiece {
    private boolean     isWhite;
    private int         value;
    private String      name;

    public ChessPiece (boolean isWhite) {
        this.isWhite = isWhite;
    }

    public String getName() { return name; }
    
}

Bishop.java

public class Bishop extends ChessPiece {

    public Bishop(boolean isWhite) {
        super(isWhite);
        this.name = "Bishop";
        this.value = 3;
    }
    ...
}

In other words, let "class inheritance" do as much of the work as possible for you.

To answer your question: it's possible to share a variable between the parent and subclasses (as shown above).

It's also possible to override member variables in the child. Look here: https://stackoverflow.com/questions/10722110/overriding-member-variables-in-java-variable-hiding

It all depends on your design - on your requirements, and on how you choose to implement those requirements.

答案2

得分: 1

你不应该为每种棋子类型创建一个棋子类,它们之间的差异不足够大。应该使用enum代替。

public class ChessPiece {
    private boolean isWhite;
    private ChessPieceType pieceType;

    enum ChessPieceType {
        Pawn(1),
        Knight(2),
        Bishop(3),
        //...
        King(1000);

        private int value;

        private ChessPieceType(int value) { 
            this.value = value; 
        }

        public int getValue() {
            return this.value;
        }
    }

    public ChessPiece (ChessPieceType type, boolean isWhite) {
        this.pieceType = type;
        this.isWhite = isWhite;
    }

    public ChessPieceType getPieceType() {
        return this.pieceType;
    }
}

用法示例:

ChessPiece piece = new ChessPiece(ChessPiece.ChessPieceType.Bishop, true);
ChessPiece.ChessPieceType pieceType = piece.getPieceType();

System.out.println(pieceType); //Bishop
System.out.println(pieceType.getValue()); //3
英文:

You shouldn't create chess for every piece type, there isn't enough difference between them. Use enum instead

public class ChessPiece {
    private boolean isWhite;
    private ChessPieceType pieceType;

    enum ChessPieceType {
        Pawn(1),
        Knight(2),
        Bishop(3),
        //...
        King(1000);
     
        private int value;
     
        private ChessPieceType(int value) { 
            this.value = value; 
        }
  
        public int getValue() {
            return this.value;
        }
   }

    public ChessPiece (ChessPieceType type, boolean isWhite) {
        this.pieceType = type;
        this.isWhite = isWhite;
    }

    public ChessPieceType getPieceType() {
        return this.pieceType;
    }
}

Uses

ChessPiece piece = new ChessPiece(ChessPiece.ChessPieceType.Bishop, true);
ChessPiece.ChessPieceType pieceType = piece.getPieceType();

System.out.println(pieceType); //Bishop
System.out.println(pieceType.getValue()); //3

答案3

得分: 0

以下是翻译好的内容:

你可以做这个,但不是你正在编码的方式。

我建议方法返回一个ChessPiece类型,而不是一个字符串,然后通过调用该方法和一个数字来实例化该类型。

public class ChessPiece {
    private boolean isWhite;
    private int value;

    public ChessPiece(boolean isWhite, int value) {
        this.isWhite = isWhite;
        this.value = value;
    }

    public ChessPiece getNamebyValue(int value) {
        switch (value) {
            case 1:
                return new Pawn();
            case 2:
                return new Knight();
            case 3:
                return new Bishop();
            case 5:
                return new Rook();
            case 9:
                return new Queen();
            case 1000:
                return new King();
            default:
                return new ChessPiece();
        }
    }
}

然后...

ChessPiece cp = new getNameByValue(1000);

这将返回一个类型为King的对象。

另外,如果你的switch语句中没有break语句,它会在通过每个语句时返回最后一个值,直到遇到break语句为止。

英文:

You can do this, but not the way you're coding it.

I would suggest having the method return a type of ChessPiece instead of a String, and then instantiating the type with calling that method and a number.

  public class ChessPiece {
private boolean     isWhite;
private int         value;

public ChessPiece (boolean isWhite, int value) {
    this.isWhite = isWhite;
    this.value = value;
}
public ChessPiece getNamebyValue(int value) {
    switch (value) {
      case 1:
        return new Pawn();
        break;
      case 2:
        return new Knight();
      break;
       case 3:
        return new Bishop();
        break;
      case 5:
        return new Rook();
       break;
      case 9:
        return new Queen();
       break;
      case 1000:
        return new King();
      break;
      default:
      return new ChessPiece();
      break;
    }
    return null;
} 

and then....

       ChessPiece cp= new getNameByValue(1000);

That will return an object of type King.

Also, if you don't have break statements in your switch, it will simply return the last value as it goes through each statement until there is a break.

答案4

得分: 0

如果我理解正确,您可能想要查看工厂设计模式,您将向函数发送“配方”以创建您的对象(在您的情况下为值),并返回一个适当的对象。

英文:

If I have understood you correctly you may want to look at Factory Design Pattern you will send a function the "recipe" to create your object (in your case the value) and return an appropriate object

答案5

得分: -1

根据这种情况,将 getNamebyValue() 更改如下:

public ChessPiece getNamebyValue() {
    switch (value) {
      case 1:
        return new Pawn();
      case 2:
        return new Knight();
      case 3:
        return new Bishop();
      case 5:
        return new Rook();
      case 9:
        return new Queen();
      case 1000:
        return new King();
    }
    return null;
  }

并且按照您展示的方式为 Bishop 创建子类。

英文:

In that case change the getNamebyValue() as below:

public ChessPiece getNamebyValue() {
    switch (value) {
      case 1:
        return new Pawn();
      case 2:
        return new Knight();
      case 3:
        return new Bishop();
      case 5:
        return new Rook()";
      case 9:
        return new Queen();
      case 1000:
        return new King();
    }
    return null;
  }

and create the subclasses as you shown for Bishop

huangapple
  • 本文由 发表于 2020年3月15日 14:11:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/60690226.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定