zoukankan      html  css  js  c++  java
  • 巧用 构造函数

    你真的会合理的使用构造函数吗?
    真的会用构造函数,来有优化代码量么?
    巧用构造函数
    比如来个实例,有一段代码如下:

        var userCourseSchedule=new UserCourseSchedule
                     {
                        CourseId = viewModel.CourseId,
                        UserId = viewModel.UserId,
                        StartTime = DateTime.Now,
                        CreateTime = DateTime.Now,
                        UpdateTime = DateTime.Now,
                        HaveLearnThemeNumble = Convert.ToInt16(havelearnNumber),
                        CourseStatus = LearningState.Study
                    };

    看完这段代码,会觉得有什么不妥的地方吗? 或者说,有什么可以优化的地方吗?

    如果,你可以看出可以优化的地方,请先不要往下看,先把怎么优化写下来,发表评论一下,好让我看到,谢谢。

    如果,你觉得这代码很正常,没有要优化的,那么请接着往下看,(最好自己先思考一下,这就是一个产生对象的代码,想一下能不能优化呢?)。

      --实体类如下

    public class UserCourseSchedule : EntityBase<Int32>
        {
    
            /// <summary>
            /// 用户编号
            /// </summary>
            [Display(Name = "用户编号")]
            [Required]
            public Int32 UserId { get; set; }
    
            /// <summary>
            /// 课程编号
            /// </summary>
            [Display(Name = "课程编号")]
            [Required]
            public Int32 CourseId { get; set; }
    
            /// <summary>a
            /// 已学主题数
            /// </summary>
            [Display(Name = "已学主题数")]
            [Required]
            public Int16 HaveLearnThemeNumble { get; set; }
    
            /// <summary>
            /// 开始时间
            /// </summary>
            [Display(Name = "开始时间")]
            [Required]
            public DateTime StartTime { get; set; }
    
            /// <summary>
            /// 更新时间
            /// </summary>
            [Display(Name = "更新时间")]
            [Required]
            public DateTime UpdateTime { get; set; }
    
            /// <summary>
            /// 课程状态
            /// </summary>
            [Display(Name = "课程状态")]
            [Required]
            public LearningState CourseStatus { get; set; }
    
            /// <summary>
            /// 课程
            /// </summary>
            [ForeignKey("CourseId")]
            public CourseInfo CourseInfo { get; set; }
    
        }


    你思考完了吗? 那我就说一下我怎么做的,说的不好,请直接指出,千万不要含蓄着不说啊,咱软件工程师就是直接了当。

    1. 首先像 时间字段 (CreateTime,StartTime ,UpdateTime ) 都是取当前时间,这是不会变的,所以就可以把初始化值放到构造函数中。
    那么现在产生的代码是:

      

     var userCourseSchedule=new UserCourseSchedule
                     {
                        CourseId = viewModel.CourseId,
                        UserId = viewModel.UserId,                    
                        HaveLearnThemeNumble = Convert.ToInt16(havelearnNumber),
                        CourseStatus = LearningState.Study
                    };

    一看,有什么差别,少了三行代码,如果这个对象要产生2次,那么就节省了6行,产生N 个对象,那么就是 (n*3)行代码.
    实体类如下:

    public class UserCourseSchedule : EntityBase<Int32>
        {
    
            /// <summary>
            /// 默认构造函数
            /// <para>会对一些默认值进行初始化</para>
            /// </summary>
            public UserCourseSchedule()
            {
                StartTime = DateTime.Now;
                CreateTime = DateTime.Now;
                UpdateTime = DateTime.Now;
            }
    
    
            /// <summary>
            /// 用户编号
            /// </summary>
            [Display(Name = "用户编号")]
            [Required]
            public Int32 UserId { get; set; }
    
            /// <summary>
            /// 课程编号
            /// </summary>
            [Display(Name = "课程编号")]
            [Required]
            public Int32 CourseId { get; set; }
    
            /// <summary>a
            /// 已学主题数
            /// </summary>
            [Display(Name = "已学主题数")]
            [Required]
            public Int16 HaveLearnThemeNumble { get; set; }
    
            /// <summary>
            /// 开始时间
            /// </summary>
            [Display(Name = "开始时间")]
            [Required]
            public DateTime StartTime { get; set; }
    
            /// <summary>
            /// 更新时间
            /// </summary>
            [Display(Name = "更新时间")]
            [Required]
            public DateTime UpdateTime { get; set; }
    
            /// <summary>
            /// 课程状态
            /// </summary>
            [Display(Name = "课程状态")]
            [Required]
            public LearningState CourseStatus { get; set; }
    
            /// <summary>
            /// 课程
            /// </summary>
            [ForeignKey("CourseId")]
            public CourseInfo CourseInfo { get; set; }
    
        }

     2. 发现 CourseStatus(枚举) 字段,初始化时候,也一直都用一个初始值,所以又少一个字段。 

     var userCourseSchedule=new UserCourseSchedule
                     {
                        CourseId = viewModel.CourseId,
                        UserId = viewModel.UserId,                    
                        HaveLearnThemeNumble = Convert.ToInt16(havelearnNumber)
                        
                    };

       实体类:

    public class UserCourseSchedule : EntityBase<Int32>
        {
    
            /// <summary>
            /// 默认构造函数
            /// <para>会对一些默认值进行初始化</para>
            /// </summary>
            public UserCourseSchedule()
            {
                StartTime = DateTime.Now;
                CreateTime = DateTime.Now;
                UpdateTime = DateTime.Now;
                      CourseStatus = LearningState.Study;
            }
    
    
            /// <summary>
            /// 用户编号
            /// </summary>
            [Display(Name = "用户编号")]
            [Required]
            public Int32 UserId { get; set; }
    
            /// <summary>
            /// 课程编号
            /// </summary>
            [Display(Name = "课程编号")]
            [Required]
            public Int32 CourseId { get; set; }
    
            /// <summary>a
            /// 已学主题数
            /// </summary>
            [Display(Name = "已学主题数")]
            [Required]
            public Int16 HaveLearnThemeNumble { get; set; }
    
            /// <summary>
            /// 开始时间
            /// </summary>
            [Display(Name = "开始时间")]
            [Required]
            public DateTime StartTime { get; set; }
    
            /// <summary>
            /// 更新时间
            /// </summary>
            [Display(Name = "更新时间")]
            [Required]
            public DateTime UpdateTime { get; set; }
    
            /// <summary>
            /// 课程状态
            /// </summary>
            [Display(Name = "课程状态")]
            [Required]
            public LearningState CourseStatus { get; set; }
    
            /// <summary>
            /// 课程
            /// </summary>
            [ForeignKey("CourseId")]
            public CourseInfo CourseInfo { get; set; }
    
        }

    3. 既然优化到这里,为了节省代码量与美观度,那就再产生一个有参的构造函数,把需要传值的字段的值作为参数。
    最后代码是这样的:

     var userCourseSchedule=new UserCourseSchedule(viewModel.CourseId,viewModel.UserId,Convert.ToInt16(havelearnNumber));

    实体类:

    public class UserCourseSchedule : EntityBase<Int32>
        {
    
            /// <summary>
            /// 默认构造函数
            /// <para>会对一些默认值进行初始化</para>
            /// </summary>
            public UserCourseSchedule()
            {
                StartTime = DateTime.Now;
                CreateTime = DateTime.Now;
                UpdateTime = DateTime.Now;
                CourseStatus = LearningState.Study;
            }
    
            /// <summary>
            /// 初始化函数
            /// </summary>
            /// <param name="courseId">课程编号</param>
            /// <param name="userId">用户编号</param>
            /// <param name="havelearnNumber">已学主题数</param>
            public UserCourseSchedule(int courseId, int userId, short havelearnNumber)
            {
                StartTime = DateTime.Now;
                CreateTime = DateTime.Now;
                UpdateTime = DateTime.Now;
                CourseStatus = LearningState.Study;
                CourseId = courseId;
                UserId = userId;
            }
    
            /// <summary>
            /// 用户编号
            /// </summary>
            [Display(Name = "用户编号")]
            [Required]
            public Int32 UserId { get; set; }
    
            /// <summary>
            /// 课程编号
            /// </summary>
            [Display(Name = "课程编号")]
            [Required]
            public Int32 CourseId { get; set; }
    
            /// <summary>a
            /// 已学主题数
            /// </summary>
            [Display(Name = "已学主题数")]
            [Required]
            public Int16 HaveLearnThemeNumble { get; set; }
    
            /// <summary>
            /// 开始时间
            /// </summary>
            [Display(Name = "开始时间")]
            [Required]
            public DateTime StartTime { get; set; }
    
            /// <summary>
            /// 更新时间
            /// </summary>
            [Display(Name = "更新时间")]
            [Required]
            public DateTime UpdateTime { get; set; }
    
            /// <summary>
            /// 课程状态
            /// </summary>
            [Display(Name = "课程状态")]
            [Required]
            public LearningState CourseStatus { get; set; }
    
            /// <summary>
            /// 课程
            /// </summary>
            [ForeignKey("CourseId")]
            public CourseInfo CourseInfo { get; set; }
    
        }

    这样下来,虽然实体类代码增加了,但生产一个对象,就省下9行对象,那么生产对象就是(n*9)行,最主要是代码视觉上有提高。

    我觉得用实体类产生对象,会更符合单一职责些。
    这样还有一个好处就是,某一天,要新加一个字段,只需在构造函数中,加就好,不用找哪里有 New 这个对象,不小心漏掉了。

    希望这内容,可以更多的人看到,也给我一些指导,谢谢。

    真实的代码如下:

  • 相关阅读:
    Winsock 2 入门指南
    Winsock 2 入门指南
    [手游新项目历程]-40-linux环境实现C/C++程序崩溃退出时打印栈信息
    1月下旬解题
    poj1226,poj3080
    poj3666
    poj3067
    poj12月其他题解(未完)
    poj1823,3667
    poj2352
  • 原文地址:https://www.cnblogs.com/Hangle/p/5319908.html
Copyright © 2011-2022 走看看