在构造函数中创建防御副本
Create a Defensive Copy in the Constructor
以下只是示例代码来解释我难以理解的问题:
假设我有以下教授 class,请注意 public 吸气剂和吸气剂:
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;
}
}
和课程:
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;
}
}
如何制作课程 class 中 Professor 对象的防御性副本?提供的示例 here 是这样处理日期对象的。
fDateOfDiscovery = new Date(aDateOfDiscovery.getTime());
课程中的教授对象也可以这样做吗class?
更新:
根据提供的答案,这就是我得出的答案,是否正确?
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)
}
您可以克隆您的教授实例。
克隆逻辑应在 Professor
class.
内
然后您可以在 Course
构造函数中接收一个已经克隆的教授实例
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
时,您将执行此操作
public Course AddCourse(string courseCode, string courseTitle, Professor original)
{
var clonedProfessor = Professor.Clone(original);
var course = new Course(courseCode, courseTitle, clonedProfessor);
return course;
}
以下只是示例代码来解释我难以理解的问题:
假设我有以下教授 class,请注意 public 吸气剂和吸气剂:
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;
}
}
和课程:
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;
}
}
如何制作课程 class 中 Professor 对象的防御性副本?提供的示例 here 是这样处理日期对象的。
fDateOfDiscovery = new Date(aDateOfDiscovery.getTime());
课程中的教授对象也可以这样做吗class?
更新:
根据提供的答案,这就是我得出的答案,是否正确?
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)
}
您可以克隆您的教授实例。
克隆逻辑应在 Professor
class.
然后您可以在 Course
构造函数中接收一个已经克隆的教授实例
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
时,您将执行此操作
public Course AddCourse(string courseCode, string courseTitle, Professor original)
{
var clonedProfessor = Professor.Clone(original);
var course = new Course(courseCode, courseTitle, clonedProfessor);
return course;
}