It is useful to consider that instance methods are intended to work with an instance of the class on which the are defined. If this is not intuitive, it can be helpful to use the keyword this
to emphasize this fact until you get used to it.
For example, your method
public Card getRank(Card r)
{
return r;
}
makes little sense in an object-oriented environment. What you are intending to do is retrieve the rank from the Card instance, so there isn't any reason to pass another Card
instance as an argument. The use of this
emphasizes the correct way to write this method:
public class Card
{
private final int rank;
private final int suit;
public Card(int r,int s)
{
this.rank=r;
this.suit=s;
}
public int getRank()
{
return this.rank;
}
:
:
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…