[英]Using subclass objects in superclass object
I'm kinda new to Java
, so I have a, rather dumb, question: Is it possible to have and use subclass'es objects in superclass which too have a constructor? 我对
Java
有点陌生,所以我有一个相当愚蠢的问题:是否可以在超类中也有构造函数的子类的对象拥有和使用?
Here's an example what I want to do: I have Superclass Team
and subclass Player
. 这是我要执行的示例:我有Superclass
Team
和Subclass Player
。 I want to create Player
object, which contains variables like name
, surname
, height
, etc. . 我想创建
Player
对象,其中包含诸如name
, surname
, height
等变量。 And I want to access this Player
object in Team
class, which have it's name, positions and other variables, to assign the Player
object to his position in the Team
. 我想在
Team
类中访问此Player
对象,该对象具有名称,位置和其他变量,以将Player
对象分配给他在Team
位置。 Lets say, Player: John Doe
is in Team
named Red
and he is a Goalkeeper
. 可以说,
Player: John Doe
( Player: John Doe
)在名为Red
Team
,他是一名Goalkeeper
。
And all together, is it possible to access all of this in main
function? 总之,是否可以在
main
功能中访问所有这些内容? For example I want to print out all Team Red's players names. 例如,我要打印出所有Red Team的球员姓名。
Sorry, it's kinda complicated to explain what I want to do, but isn't this have to do something with extends
? 抱歉,要解释我想做什么有点复杂,但这不是必须对
extends
做些什么吗?
The short answer is that yes, this is possible. 简短的答案是,可以。
The longer answer is that you're misusing the terms superclass and subclass . 更长的答案是您在滥用术语超类和子类 。 The relationship you describe is not an is-a relationship of inheritance, but a has-a relationship of composition.
你描述的关系不是继承的IS-的关系,而是一个具有-A的组合关系。
In other words, your Player class does not extend your Team class. 换句话说,您的Player类不会扩展您的Team类。 Your Team class would contain instances of Player.
您的Team类将包含Player实例。 And yes, in that case your Team class could access the methods and variables of those instances, just like you can any other Java class.
是的,在那种情况下,您的Team类可以访问那些实例的方法和变量,就像您可以使用任何其他Java类一样。 It might look something like this:
它可能看起来像这样:
class Team{
List<Player> players = new ArrayList<Player>();
public void addPlayer(Player p, String position){
p.position = position;
players.add(p);
}
}
And then from a main() method, you might do something like this: 然后从main()方法中,您可以执行以下操作:
public class Main{
public static void main(String... args){
Team team = new Team("Fighting Cats");
Player p = new Player("Johnny Appleseed");
team.addPlayer(p, "Apple Picker");
}
}
Yes, see the below code. 是的,请参见下面的代码。
public class Team{
public Player player;
}
class Player extends Team{
public Team team;
}
Although, I doubt your logic and design when you say that Player
extends Team
, which essentially says that a Player
is a Team
. 虽然,当您说
Player
扩展Team
时,我怀疑您的逻辑和设计,这从本质上说Player
是Team
。 This would mean that each Player
object would have a list of players and positions (as subtypes inherit non-private states and behaviours from supertypes), which doesn't make much sense semantically. 这意味着每个
Player
对象都有一个玩家和位置的列表(因为子类型从超类型继承非私有状态和行为),从语义上讲并没有多大意义。
I suggest that you instead approach it like the following. 我建议您改为像下面这样处理它。
public class Team{
public LinkedList<Player> players;
}
class Player{
public Position position; // Use some way of defining the player's position.
}
This means that Team
would have a Player
, rather Player
being a Team
这意味着
Team
将拥有一个Player
,而不是将Player
作为一个Team
You have to create 3 classes Team, Player & a Client class to input the data. 您必须创建3类Team,Player和Client类来输入数据。 I am writing the skeleton code for you.
我正在为您编写框架代码。 It will give you an idea as to how to write your code
它将为您提供有关如何编写代码的想法
public class Team {
ArrayList<Player> player;
public ArrayList<Player> getPlayer() {
return player;
}
public void setPlayer(ArrayList<Player> player) {
this.player = player;
}
}
Player 播放器
class Player{
String name;
Integer score;
Integer Rank;
//getter setters
}
Client class 客户类
public class Client {
public static void main(String[] args) {
Player pOne = new Player();
pOne.setRank(2);
pOne.setName("ABD");
Player pTwo=new Player();
pTwo.setRank(2);
pTwo.setName("SPC");
pTwo.setTotal(60);
ArrayList<Player> playerList = new ArrayList<Player>();
playerList.add(pOne);
playerList.add(pTwo);
Team team=new Team();
one.setPlayer(playerListOne);
//get the player using Team class's getPlayer method & print
}
You need not use extends
but rather focus on the OO design. 您无需使用
extends
,而可以专注于OO设计。
What you are looking for is a composition and not inheritance (ie A Team
"has a" Player
. "A Player
is a Team
" does not make much sense) 您要寻找的是组成而不是继承(即,一个
Team
“拥有”一个Player
。“一个Player
就是一个Team
”没有多大意义)
class Player {
String name;
String surname;
String height;
}
class Team {
String name;
List<Player> players;
}
When we think about team and player, its relation is team has a player . 当我们考虑团队和球员时,它的关系就是团队有一个球员 。 Use the same relation when you design the classes.
设计类时,请使用相同的关系。 Position is a property which is relevant in the scope of a Player.
位置是与播放器范围相关的属性。 So introduce that property in correct level.
因此,以正确的级别介绍该属性。 Better to keep Team class with its own properties.
最好让Team类拥有自己的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.