I am confused with this code below,
Developer devCopy = (Developer)dev.Clone();
Clone method of Developer class just creating a Employee clone, then how developer get another clone of developer.
public abstract class Employee
{
public abstract Employee Clone();
public string Name { get; set; }
public string Role { get; set; }
}
public class Typist : Employee
{
public int WordsPerMinute { get; set; }
public override Employee Clone()
{
return (Employee)MemberwiseClone();
}
public override string ToString()
{
return string.Format("{0} - {1} - {2}wpm", Name, Role, WordsPerMinute);
}
}
public class Developer : Employee
{
public string PreferredLanguage { get; set; }
public override Employee Clone()
{
return (Employee)MemberwiseClone();
}
public override string ToString()
{
return string.Format("{0} - {1} - {2}", Name, Role, PreferredLanguage);
}
}
Developer dev = new Developer();
dev.Name = "Bob";
dev.Role = "Team Leader";
dev.PreferredLanguage = "C#";
Developer devCopy = (Developer)dev.Clone();
devCopy.Name = "Sue";
Console.WriteLine(dev);
Console.WriteLine(devCopy);
/* OUTPUT
Bob - Team Leader - C#
Sue - Team Leader - C#
*/
Typist typist = new Typist();
typist.Name = "Kay";
typist.Role = "Typist";
typist.WordsPerMinute = 120;
Typist typistCopy = (Typist)typist.Clone();
typistCopy.Name = "Tim";
typistCopy.WordsPerMinute = 115;
Console.WriteLine(typist);
Console.WriteLine(typistCopy);
/* OUTPUT
Kay - Typist - 120wpm
Tim - Typist - 115wpm
*/
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…