?? 7-10.cs
字號:
//程序7-10
using System;
class Student
{
public int studID;
public string studName;
}
public class Department
{
public int DepartmentID;
public Department()
{ }
public Department(int i)
{
DepartmentID=i;
}
}
class CollegeStudent: Student,ICloneable
{
public Department Dept; // 引用類型
public CollegeStudent(string nm, int sID, int dID)
{
studID=sID;
studName=nm;
Dept= new Department( dID);
}
// 實現(xiàn)ICloneable接口中的Clone方法,完成深復制
public object Clone()
{
CollegeStudent CS=new CollegeStudent(this.studName, this.studID,this.Dept.DepartmentID);
return CS;
}
}
public class Test
{
public static void Main()
{
CollegeStudent S1 = new CollegeStudent( "Sam",2003179,001);
// 深復制,S2中的值從S1中的所有值復制,但是S1和S2分別占用不同的存儲空間
CollegeStudent S2 = (CollegeStudent)S1.Clone();
// 改變S1中字段的值不影響S2中的值
S1.studName = "Bush";
//S1.studName = "Sam"; // string類型是一個特殊的引用類型,只要字符串內(nèi)容相同,則引用和值均相等
S1.Dept.DepartmentID=002;
Console.WriteLine(S1.studID+" "+S1.studName+" "+S1.Dept.DepartmentID );
Console.WriteLine(S2.studID+" "+S2.studName+" "+S2.Dept.DepartmentID );
Console.WriteLine(Object.ReferenceEquals(S1,S2)); // false
Console.WriteLine(Object.ReferenceEquals(S1.studName,S2.studName)); // false
Console.WriteLine(Object.ReferenceEquals(S1.Dept.DepartmentID,S2.Dept.DepartmentID)); // false
Console.WriteLine(Object.Equals(S1.Dept.DepartmentID,S2.Dept.DepartmentID)); // false
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -