static void Main(string[] args)
{
Student student = new Student()
{
ID = 12,
Name = "Manu",
LastName = "Shekar"
};
Iregister x = student;
Student newstudent = x as Student;
//Console.WriteLine(x.LastName); //Uncommenting this shows compilation error
Console.WriteLine(newstudent.LastName); //This Show "Shekar"
Console.ReadKey();
}
class Student : Iregister
{
public int ID { get; set; }
public string Name { get; set; }
public String LastName { get; set; }
}
interface Iregister
{
int ID { get; set; }
String Name { get; set; }
}
我不知道newstudent.LastName
如何得到正確的值,因爲它是從Iregister
鑄造不具有LastName
財產? 值「Shekar」如何從student.LastName
傳遞到newstudent.LastName
。 x
是否存儲在它之間的某個地方?如果我想念一些基本知識,請原諒我。上傳如何保留派生類的屬性?
值「Shekar」如何從'student.LastName'傳遞給'newstudent.LastName'。 'x'是否存儲在中間的某處? –
'x'只是一個變量。整個過程只有一個對象在內存中。一個變量只指向那個對象。 – Kapol
@NithinChandran:想象一下,如果我把蒙娜麗莎放進一個安全儲物櫃,我給三個不同的人一個鑰匙給儲物櫃。三個人(變量)可以**訪問儲物櫃,並可以在他們想要的時候查看蒙娜麗莎(物體),但**只有一個蒙娜麗莎繪畫(物體)存在**。你的變量都有一個引用(locker key),但是它們的所有引用都指向內存中的同一個對象(它們的所有鍵都用於同一個locker) – Flater