开发者

Can I use my objects without fully populating them?

I have situation. I have to create a Sports Club system in JAVA. There should be a class your for keeping track of club name, president name and braches the club has. For each sports branch also there should be a class for keeping track of a list of players. Also each player should have a name, number, position and salary. So, I come up with this. Three seperate classes:

public class Team
{
String clubName;
String preName;
Branch []branches;
}

public class Branch
{
Player[] players;

}

public class Player
{
String name;
String pos;
int salary;
int number;
}

The problems are creating Branch[] in another class and same for the Player[]. Is there any simplier thing to do this? For example, I want to add info for only the club name, president name and branches of the club开发者_如何学Go, in this situation, won't i have to enter players,names,salaries etc. since they are nested in each other. I hope i could be clear. For further questions you can ask.


Here's a more complete, formal example of your scenario using conventional Accessors/Mutators (getters/setters), constructors, and Lists. The main() method below illustrates how to use your classes.

public class SportsClub
{
    public static void main(String[] args)
    {
        //Create a team without any branches
        Team myTeam = new Team("Southpaws", "South");

        //Create a new Branch without any players
        Branch myBranch = new Branch();

        //Add myBranch to myTeam
        myTeam.getBranches().add(myBranch);

        //Create a new player
        Player myPlayer = new Player("Bob", "Center", 120, 3);

        //Add myPlayer to myBranch (and therefore myTeam)
        myBranch.getPlayers().add(player);
    }
}


public class Team
{
    private String clubName;
    private String preName;
    private List<Branch> branches;

    public Team(String clubName, String preName)
    {
        this.clubName = clubName;
        this.preName = preName;
        branches = new ArrayList<Branch>();
    }

    public String getClubName() { return clubName; }
    public String getPreName() { return preName; }
    public List<Branch> getBranches() { return branches; }

    public void setClubName(String clubName) { this.clubName = clubName; }
    public void setPreName(String preName) { this.preName = preName; }
    public void setBranches(List<Branch> branches) { this.branches = branches; }
}

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

    public Branch() {}

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

public class Player
{
    private String name;
    private String pos;
    private Integer salary;
    private Integer number;

    public Player(String name, String pos, Integer salary, Integer number)
    {
        this.name = name;
        this.pos = pos;
        this.salary = salary;
        this.number = number;
    }

    public String getName() { return name; }
    public String getPos() { return pos; }
    public Integer getSalary() { return salary; }
    public Integer getNumber() { return number; }

    public void setName(String name) { this.name = name; }
    public void setPos(String pos) { this.pos = pos; }
    public void setSalary(Integer salary) { this.salary = salary; }
    public void setNumber(Integer number) { this.number = number; }
}

To answer your question, yes, you can create these objects without populating the Lists with players. The SportsClub.main() above illustrates that.


I would use a List rather than an array since they're (easily) dynamically resizable, but otherwise, you're on the right track.

Think about encapsulation and visibility too. Make all those fields private and provide accessors.


You could create an empty Branch[] array (or better yet - a list) at initialization and add to it later, that way you don't have to enter all the information upon creation - same goes for Player[].

Something like:

public class Team
{
    String clubName;
    String preName;
    private List<Branch> branches;

    public Team (String club, String pre) {
        clubName = club;
        preName = pre;
        branches = new LinkedList<Branch>();
    }
    public void addBranch (Branch branch) {..}
}

public class Branch
{
    private List<Player> players;
    public Branch () {
        players = new LinkedList<Player>();
    }
    public void addPlayer (Player player) {..}
}

public class Player
{
    String name;
    String pos;
    int salary;
    int number;
}


I think that's good. You should probably have methods in the classes to manage your information though--don't try to do anything serious from "Outside" these classes.

to be more specific: All your members should be private and only used/accessed from within the classes--also in general avoid setters and getters, instead ask the class to do things for you.

For example, if you wanted to know how many players were in a branch, you would call branch.countPlayers, not access the Player array to count the players from outside.

If you wanted to know how many players were in a team, you would call team.countPlayers which would call branch.countPlayers for each Branch, sum them up and return the value.

If you wanted to see which branch a player was in, you would call Team.findPlayer(playerName). Team would call branch.hasPlayer(playerName) on each branch until it returned a true, then Team would return the Branch object that returned true.

etc.

Note that this resolves your "Populated or not" issue. If you simply have methods like "hasBranch()", "addBranch()", "removeBranch()" then it doesn't matter how or when you populate the branches array since you control it all within the Team class you can change it's implementation at any time and not change a single line outside that class.


You won't have to enter anything into the players array, nor the branch[]. Provided you make the fields accessible, of have properties, you will be able to put them in however you like.

The class structure looks good to me, but a List would be better so that you don't have to worry about resizing arrays down the road.


Nothing wrong with your classes. I personally would use a strongly-typed List to store the branches and players:

public class Team
{
  String clubName;
  String preName;
  List<Branch> branches;
}

public class Branch
{
  List<Player> players;

}

Not sure of the requirement, but you'd probably want to have some kind of identifier or name for each Branch, right?

There's nothing in these classes that forces you to create new players just to instantiate a Branch. The list of Players can remain null or empty until you need them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜