实现这个的最佳解决方案是什么?

时间:2011-05-22 08:39:51

标签: c# .net

我正在使用的项目只有一个有朋友的个人资料

我已经通过

做到了
  • 静态课程资料

  • 班级朋友

  • 个人资料有静态的朋友聚会

但是个人资料和朋友有与名称,图片等相同的变量

所以我决定让一个人抽象类并继承它

然后我发现我不能从person继承静态类[profile],因为变量不具有属性

所以我把变量变成了静态的

然后每个朋友都没有变量,因为静态变量将属于朋友类

我是新手,我知道这是一个愚蠢的问题!!

但是实现这个

的最佳方法是什么

我首选使用静态配置文件以获取辅助功能

我首选使用静态物品进行辅助用途

2 个答案:

答案 0 :(得分:3)

避免使用静态类。如果您想要一个实例,只需创建一个实例。静态类使测试变得困难。

但回到设计,也许尝试介绍一个User类:

class User
 - name
 - picture
 - other properties

class Profile
 - User myAccountInfo
 - List<User> friends

答案 1 :(得分:1)

也许是这样的?:

class User
{
    public User(string name, object picture)
    {
        Name = name;
        Picture = picture;
    }

    public string Name { get; set; }
    public object Picture { get; set; } //Change object to a class that holds Picture information.
}

class Profile : User
{
    private static Profile _profile;

    public List<User> Friends = new List<User>(); //This List<T> can contain instances of (classes that derive from) User.

    public Profile(string name, object picture) : base(name, picture) { }
    public static Profile GetProfile()
    {
        return _profile ?? (_profile = new Profile("NameOfProfileHere", null));
    }
}