2

我正在尝试为学生和教师创建一个社交网络应用程序。我目前正在设计用户帐户模块

我想到了这两种方法

方法一

Public class Account
{
    public string AccountId{get;set;}
    public string AccountName{get;set;}
       .
       .
       .


}

public class Student : Account
{
    public Course[] Courses {get; set;}
    //other student properties
        .
        .
        .

}

public class Teacher : Account
{
     //other teacher properties

}

方法二

Public class Account
{
    public string AccountId{get;set;}
    public string AccountName{get;set;}
    public AccountType AccountType {get;set;}  //AccontType is enum having student and teacher
       .
       .
       .


}


public class User
{
    public string UserId { get;set;}
    public string UserName {get;set;}
    public Account Account {get;set;}
}


public class Student : User
{
    public Course[] Courses {get; set;}
    //other student properties
        .
        .
        .

}


public class Teacher : User
{
     //other teacher properties

}

有没有比这些方法更好的方法?

4

2 回答 2

2

在我看来,方法 2 更清晰,更受欢迎。

归根结底是有User 帐户还是User帐户?对我来说是前者,在这种情况下你应该使用组合而不是继承,就像你在方法 2 中所做的那样。

于 2011-06-12T06:14:10.803 回答
1

我更喜欢方法 2,因为它更接近现实世界的场景。学生和教师都是用户,他们每个人都有一个具有所需属性集的帐户。

于 2011-06-12T06:15:22.083 回答