Why not inherit from List?

前端 未结 27 1812
悲哀的现实
悲哀的现实 2020-11-21 05:39

When planning out my programs, I often start with a chain of thought like so:

A football team is just a list of football players. Therefore, I should

27条回答
  •  面向向阳花
    2020-11-21 06:12

    Let me rewrite your question. so you might see the subject from a different perspective.

    When I need to represent a football team, I understand that it is basically a name. Like: "The Eagles"

    string team = new string();
    

    Then later I realized teams also have players.

    Why can't I just extend the string type so that it also holds a list of players?

    Your point of entry into the problem is arbitrary. Try to think what does a team have (properties), not what it is.

    After you do that, you could see if it shares properties with other classes. And think about inheritance.

提交回复
热议问题