加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何使用OOP进行设计

发布时间:2020-12-15 19:33:21 所属栏目:百科 来源:网络整理
导读:我有一个名为User的对象,其中包含Username,Age,Password email等属性. 我在ado.net代码中初始化此对象,如: private User LoadUser(SqlDataReader reader){ User user = new User(); user.ID = (int)reader["userID"]; // etc} 现在说我创建一个继承自User的
我有一个名为User的对象,其中包含Username,Age,Password email等属性.

我在ado.net代码中初始化此对象,如:

private User LoadUser(SqlDataReader reader)
{
      User user = new User();

      user.ID = (int)reader["userID"];
      // etc
}

现在说我创建一个继承自User的新对象,如:

public class UserProfile : User
{
     public string Url {get;set;}
}

现在我需要创建一个加载userprofile的方法,所以目前我在做:

public UserProfile LoadUserProfile(SqlDataReader reader)
{
         UserProfile profile = new UserProfile();

         profile.ID = (int)reader["userID"];
         // etc. copying the same code from LoadUser(..)
         profile.Url = (string) reader["url"];
}

是否有更多的OOP方法,所以我不必从LoadUser()在LoadUserProfile()镜像我的代码?

我希望我能做到这一点:

UserProfile profile = new UserProfile();

profile = LoadUser(reader);

// and then init my profile related properties

可以这样做吗?

解决方法

我真的不明白为什么用户配置文件继承自用户,从逻辑上讲,它没有多大意义.

对我来说有意义的是

public class UserProfile
{
     User user;
     public string Url {get;set;}
}

然后你可以有类似的东西

public UserProfile LoadUserProfile(SqlDataReader reader)
{
         User user = LoadUser(reader);
         UserProfile profile = new UserProfile(user);

          //load profile stuff from reader...
         return profile;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读