连接两个类java面向对象 - 团队和播放器

时间:2018-02-02 22:03:08

标签: java class object member

我希望创建两个新类,我可以将它们连接起来。 我想创建一个球员和球队。我的目标是将球员分配给球队。

我想我的球员类就像;

public class Player {
    String first_name;
    String last_name;
    int age;
}

和我的团队一样;

public class Team {
    String Member1;
    String Member2;
}

和我要包括的主要课程;

Player Player1 = new Player("Peter", "Jensen", 24);

所以我想要的是计算特定球队中球员的平均年龄。任何想法或提示?

5 个答案:

答案 0 :(得分:3)

在现实生活中什么是团队?它是一群玩家所以你需要让TeamPlayers组成,然后提出一个问题"哪里有大量的信息来满足普通玩家年龄的计算? "这些信息放在Team类(玩家列表)中,我们需要放置计算平均年龄为Team级的方法。它被称为信息专家原则,是众多优秀的oop设计原则之一。

https://en.wikipedia.org/wiki/GRASP_(object-oriented_design)#Information_expert

所以这样代码就是这样:

玩家类:

  public class Player {
        private String firstName;
        private String lastName;
        private int age; 

        public Player(String firstName, String lastName, int age) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.age = age;
        }

        public String firstName() {
            return firstName;
        }

        public String lastName() {
            return lastName;
        }

        public String age() {
            return age;
        }

    }

团队课程:

  public class Team {
        private List<Player> players;

        Team(List<Player> players) { 
           this.players = players;
        }

        public void add(Player player) {
            players.add(player);
        }

        public void remove(Player player) {
            players.remove(player);
        }

        public double averageAge() {
            double sum = 0;
            double amount = players.size();
            for (Player player : players) {
                sum += player.age();
            }
            return sum / amount;
        }

    }

之后我们能够以各种方便的方式使用这些类,例如:

public class Test {

    public static void main(String[] args) {
        Team team = new Team(Arrays.asList(new Player[] {
            new Player("Peter", "Jensen", 24),
            new Player("Foo", "Bar", 50)
        }));
        System.out.println(team.averageAge()); // 37
    }
}

简单询问团队成员的平均年龄,团队会回复你。

答案 1 :(得分:2)

Team课程内创建一个列表或一组玩家(如果允许有固定数量的玩家)以代表玩家团队

示例:

class Team {
   private List<Player> players = new ArrayList<>();

   public void setPlayers(List<Player> players){
          this.players = players;
   }

   public List<Player> getPlayers(){
       return Collections.unmodifiableList(players);
   }
}

然后创建对象列表:

Team team = new Team();
team.setPlayers(new ArrayList<>(Arrays.asList( new Player("Peter", "Jensen", 24),
                new Player("Danny", "peters", 19),
                new Player("Michael", "Jones", 32))));

那么你可以像这样计算平均年龄:

double averageAge = team.getPlayers()
                        .stream()
                        .mapToDouble(Player::getAge)
                        .average().orElse(0); // 0 if no player present in the list

note - 上面的示例使用了一个列表,但您可以轻松地使用数组,它也使用流API,如果您不熟悉它,那么您可以这样做像这样:

double averageAge = 0; // default if no player is present
List<Player> players = team.getPlayers();
for (Player player : players) {
     averageAge += player.getAge();
}

if(players.size() >= 1) {
    averageAge /= players.size();
}

如果由于某种原因你也希望能够一次添加一个给定的玩家,那么你可以在void addPlayer(Player player){...}类中创建一个Team方法来获取给定的玩家并将其添加到列表中球员

答案 2 :(得分:0)

你可以在玩家类中添加getter和setter方法。如果你想要你可以在玩家类中做这样的事情:

public static Player newInstance(String n,String Sn,int age){
    Player a=new Player(n,Sn,age);
    return a;

}

现在你可以轻松地将玩家添加到team.But成员不能是String.My建议使用ArrayList。你可以找到很多关于它的文章。

你可以使用'for(播放器a:ListName)'它会让你的事情变得更容易。

答案 3 :(得分:0)

在你的团队中,你需要存储一组玩家:

public class Team{
    private List<Player> players;

    public Team() {
        players = new ArrayList<?>();
    }

    public void addPlayer(Player player) {
        players.add(player);
    }

    public Int getAverageAge() {
        Int count =0;
        Int sumAge=0;
        for(Player player:players) {
            sumAge+=player.getAge();//this getter should be in Player class
            count++;
        }
        return sumAge/count;
    }
}

使用addPlayer()方法将您的播放器添加到主类

中的列表中

答案 4 :(得分:-1)

我的解决方案

public class Team {
List<Player> allPlayer;

// constructor, getter and setters
}

public class Player {
String name;
// constructor, getter and setters
}

感谢您的帮助!