如何在系统中设计两种类型的账户

发布于 2024-11-15 01:35:11 字数 1068 浏览 3 评论 0原文

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

我想到了这两种方法

方法1

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

}

方法2

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

}

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

I am trying to create a social networking application for students and teachers.
I am currently designing the user accounts module

I have thought of these two approaches

Approach 1

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

}

Approach 2

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

}

Is there any better approach than these approaches?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

×眷恋的温暖 2024-11-22 01:35:11

我认为方法 2 更加清晰,也是首选。

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

Approach 2 is much clearer and preferred in my opinion.

What it comes down to is does a User have an account or is a User an account? For me its the former and you should use composition instead of inheritance in this case as you do in approach 2.

眼眸 2024-11-22 01:35:11

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

I like approach 2 better because it's more closer to the real world scenario. Students and Teachers are users and each of them have an account with the desired properties set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文