首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在构造函数中创建防御副本

在构造函数中创建防御副本
EN

Stack Overflow用户
提问于 2016-02-18 01:01:39
回答 1查看 1.4K关注 0票数 2

以下只是解释我难以理解的问题的示例代码:

假设我有以下的教授类,请注意公共getter和setter:

代码语言:javascript
复制
    public class Professor
    {
        public string id {get; set; }
        public string firstName{get; set;}
        public string lastName {get; set;}

        public Professor(string ID, string firstName, string lastname)
        {
            this.id = ID;
            this.firstName = firstName;
            this.lastName = lastname;
        }


    }

当然:

代码语言:javascript
复制
public class Course
{
    string courseCode {get; private set;}
    string courseTitle {get; private set;}
    Professor teacher {get; private set;}

    public Course(string courseCode, string courseTitle, Professor teacher)
    {
        this.courseCode = courseCode;
        this.courseTitle = courseTitle;

    }
}

我如何在课程中复制教授对象的防御性副本?提供的示例这里在date对象中这样做。

代码语言:javascript
复制
fDateOfDiscovery = new Date(aDateOfDiscovery.getTime());

在课程中教授的对象也可以这样做吗?

更新:

如果这是我想出的答案,这是正确的吗?

代码语言:javascript
复制
public class Professor
{
 public string id {get; set; }
 public string firstName{get; set;}
 public string lastName {get; set;}

 Professor(string ID, string firstName, string lastname)
  {
       this.id = ID;
       this.firstName = firstName;
       this.lastName = lastname;
  }

 //This method can be either static or not
 //Please note that i do not implement the ICloneable interface. There is discussion in the community it should be marked as obsolete because one can't tell if it's doing a shallow-copy or deep-copy
 public static Professor Clone(Professor original)
 {
   var clone = new Professor(original.id, original.firstName, original.lastName);
   return clone;
 }
}

//not a method, but a constructor for Course
public Course (string courseCode, string courseTitle, Professor teacher)
{
    this.courseCode = courseCode;
    this.courseTitle = courseTitle;
    this.teacher = Professor.Clone(teacher)

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-18 01:20:52

你可以克隆你的教授。

克隆逻辑应该在Professor类中。

然后,您可以在Course构造函数中接收已经克隆的教授实例。

代码语言:javascript
复制
public class Professor
{
 public string id {get; set; }
 public string firstName{get; set;}
 public string lastName {get; set;}

 Professor(string ID, string firstName, string lastname)
  {
       this.id = ID;
       this.firstName = firstName;
       this.lastName = lastname;
  }

 //This method can be either static or not
 //Please note that i do not implement the ICloneable interface. There is discussion in the community it should be marked as obsolete because one can't tell if it's doing a shallow-copy or deep-copy
 public static Professor Clone(Professor original)
 {
   var clone = new Professor(original.id, original.firstName, original.lastName);
   return clone;
 }
}

然后,当您调用一个新的Course时,您将执行以下操作

代码语言:javascript
复制
public Course AddCourse(string courseCode, string courseTitle, Professor original)
{
  var clonedProfessor = Professor.Clone(original);
  var course = new Course(courseCode, courseTitle, clonedProfessor);
  return course;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35470863

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档