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