Reputation: 65
I have to write a small text-based game for school in which I have a Player class and a Computer class. In the Game class I need to differentiate between these two classes so I can print instructions ONLY when the player is not a computer.
public class Player {
private String name;
public Player(String name) {
this.name = name;
}
}
public class Computer extends Player {
public Computer(String name) {
super(name);
}
}
I've tried the following:
player1 = new Player;
if(player1.getClass() != Computer.class) {
System.out.println("Your turn, player 1.");
}
This should print "Your turn, player 1." because a player is not a computer, right? Well it doesn't and I have no idea how to do it otherwise.
I have tried instanceof, but that always returns true because Computer is an instance of Player.
Help would be very much appreciated!
~ Daan
Upvotes: 3
Views: 18011
Reputation: 7462
Computer is always an instance of Player, but a Player is not always a Computer. Try this instead:
if (!player.instanceof(Computer)) {...}
But keep in mind that if you initialize player1 as a new Player, as you do, it will never be an instance of Computer. So, if you want to initialize a Player as a Computer, you will need to use the Computer's constructor. The following code will return "true":
Player player1 = new Computer();
System.out.println(player1.instanceof(Computer));
Upvotes: 0
Reputation: 2896
You could create a method that returns the instructions for a player in the Player class. Every subclass can then override the getInstructions method in order to provide more specific instructions. For a computer this would be an empty string. You could do it like this:
public class Player {
private String name;
public Player(String name) {
this.name = name;
}
public String getInstructions() {
return "Instructions";
}
}
public class Computer extends Player {
public Computer(String name) {
super(name);
}
@Override
public String getInstructions() {
return "";
}
}
Then you can just print the instructions if they are not empty.
Player p = new Player("asdf");
Player c = new Computer("qwer");
String instruction = p.getInstruction();
if (!instruction.empty()) {
System.out.printLine(instruction);
}
instruction = c.getInstruction();
if (!instruction.empty()) {
System.out.printLine(instruction);
}
Of course you can pass an Output Stream to the getInstruction method an write it directly to the output stream. Then you wouldn't need the ifs.
Upvotes: 0
Reputation: 763
You can do this with instanceof:
if(!(player1 instanceof Computer))
...but that's a bad design. Since a computer is-a player (inheritance), they should both have the same abilities (API). A really simple idea would be to have them both implement an IPlayer interface with a printGreeting() method:
public interface IPlayer {
void printGreeting();
}
public class Player implements IPlayer {
private String name;
public Player(String name) {
this.name = name;
}
public void printGreeting() {
system.out.println('Your turn! (or whatever)');
}
}
public class Computer extends Player {
public Computer(String name) {
super(name);
}
@Override
public void printGeeting() {
// NO-OP
}
}
Upvotes: 4
Reputation: 42174
This smells like a symptom of a bad design. You shouldn't usually have to check what kind of class a value is. In this case you could have a boolean in the Player class specifying whether the player is human.
If you really want to go with this design though, what you're looking for is the "instanceof" operator. Do a google search of "java instanceof" for more info.
Edit: I see now that you tried instanceof already. Why are you comparing instanceof to the Player class? Don't you want to compare instanceof to the Computer class?
Upvotes: 0