我可以从超类对象数组中获取子类变量吗?
Can I get subclass variables from array of superclass objects?
首先感谢阅读!
我做了一个class"Sportsman",它是"Footballer"的超级class。
所以我制作了一个运动员对象数组,其中也包含足球运动员对象,这里没有问题(我很清楚继承是如何工作的)。
我可以将特定于足球运动员的变量设置为数组中的对象,但是当我想打印我刚刚向对象声明的变量时,我无法调用 get-methods,因为数组是 Sportsman-数组而不是足球运动员数组。
所以这是我的问题:如何从 Sportsman Superclass 数组中打印 Footballer 特定变量?
须知:
我无法为 subclass 对象创建一个单独的数组。他们必须混合!
在将 subclass 对象放入 superclass 对象的数组中时,我明确地将其设为 subclass 对象。但是,我无法在其上使用 subclass 方法。
主要代码:
public class SportApp {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Sportsman[] sportArr = new Sportsman[10];
for(int count=0 ; count < sportArr.length ; count++)
{ System.out.println("Is the sportsman a footballer?");
String answer = input.nextLine();
System.out.println("Last name?");
String lastName = input.nextLine();
System.out.println("name?");
String name = input.nextLine();
switch (answer){
case "yes": System.out.println("Which club does he play in?");
String club = input.nextLine();
System.out.println("At what position?");
String pos = invoer.nextLine();
sportArr[count]=new Footballer(lastName,name,club,pos);
break;
default: System.out.println("What sport?");
String sport = input.nextLine();
sportArr[count]=new Sportsman(lastName,name,sport);
}
}
System.out.println("All sportsmen that don't play football:");
for(int count=0 ; count < sportArr.length ; count++)
{ if(!(sportArr[count] instanceof Footballer))
{ System.out.print("name: ");
sportArr[count].print();} }
System.out.println("All football players sorted by position:");
//Same as previous print, but with added player position and club!
for(int count=0 ; count < sportArr.length ; count++)
{ if(sportArr[count] instanceof Footballer)
{
/*what I've tried:
*System.out.println("front players:");
*if(sportArr[count].getPos()=="front") //the .getPos doesn't work because it wants to invoke it on a Sportsman where getPos doesn't exist
*{ sportArr[count].print();} //as the problem above, it doesn't see the object is also a Footballer so it does the Sportsman print()
*
*I wanted to do a regular sportArr[count].pos to print the Position but even now it doesn't recognise the object as Footballer, so I can't see pos.
*/
}
}}}
您已经在循环中使用 instanceof
进行了类型检查,如果成功,您就知道您有一个 Footballer
。所以,现在你必须转换对象以获得正确的类型:
if(sportArr[count] instanceof Footballer)
{
Footballer fb = (Footballer) sportArr[count];
// Now this should work (note the use of fb, and not using `==` with string literals):
if(fb.getPos().equals("front")) {
// etc..
}
}
首先感谢阅读!
我做了一个class"Sportsman",它是"Footballer"的超级class。 所以我制作了一个运动员对象数组,其中也包含足球运动员对象,这里没有问题(我很清楚继承是如何工作的)。
我可以将特定于足球运动员的变量设置为数组中的对象,但是当我想打印我刚刚向对象声明的变量时,我无法调用 get-methods,因为数组是 Sportsman-数组而不是足球运动员数组。
所以这是我的问题:如何从 Sportsman Superclass 数组中打印 Footballer 特定变量?
须知: 我无法为 subclass 对象创建一个单独的数组。他们必须混合! 在将 subclass 对象放入 superclass 对象的数组中时,我明确地将其设为 subclass 对象。但是,我无法在其上使用 subclass 方法。
主要代码:
public class SportApp {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Sportsman[] sportArr = new Sportsman[10];
for(int count=0 ; count < sportArr.length ; count++)
{ System.out.println("Is the sportsman a footballer?");
String answer = input.nextLine();
System.out.println("Last name?");
String lastName = input.nextLine();
System.out.println("name?");
String name = input.nextLine();
switch (answer){
case "yes": System.out.println("Which club does he play in?");
String club = input.nextLine();
System.out.println("At what position?");
String pos = invoer.nextLine();
sportArr[count]=new Footballer(lastName,name,club,pos);
break;
default: System.out.println("What sport?");
String sport = input.nextLine();
sportArr[count]=new Sportsman(lastName,name,sport);
}
}
System.out.println("All sportsmen that don't play football:");
for(int count=0 ; count < sportArr.length ; count++)
{ if(!(sportArr[count] instanceof Footballer))
{ System.out.print("name: ");
sportArr[count].print();} }
System.out.println("All football players sorted by position:");
//Same as previous print, but with added player position and club!
for(int count=0 ; count < sportArr.length ; count++)
{ if(sportArr[count] instanceof Footballer)
{
/*what I've tried:
*System.out.println("front players:");
*if(sportArr[count].getPos()=="front") //the .getPos doesn't work because it wants to invoke it on a Sportsman where getPos doesn't exist
*{ sportArr[count].print();} //as the problem above, it doesn't see the object is also a Footballer so it does the Sportsman print()
*
*I wanted to do a regular sportArr[count].pos to print the Position but even now it doesn't recognise the object as Footballer, so I can't see pos.
*/
}
}}}
您已经在循环中使用 instanceof
进行了类型检查,如果成功,您就知道您有一个 Footballer
。所以,现在你必须转换对象以获得正确的类型:
if(sportArr[count] instanceof Footballer)
{
Footballer fb = (Footballer) sportArr[count];
// Now this should work (note the use of fb, and not using `==` with string literals):
if(fb.getPos().equals("front")) {
// etc..
}
}