- 装箱是指将一个对象进行再次封装,越封装越大—值类型转成引用类型
- 拆箱是指将一个对象进行解除封装,越拆箱越小—引用类型转成值类型,前提是先装箱
class Person
{
public void Say()
{
Console.WriteLine("父类中的方法");
}
}
class Student:Person
{
public void SonSay()
{
Console.WriteLine("子类中的方法");
}
}
static void Main(string[] args)
{
Student student2 = new Student();
//系统会自动将子类student对象隐式转换为Person类型(隐式实现了一次装箱)
Person p = student2;
Console.ReadLine();
}
注意:子类的对象则不能直接使用父类的实例
static void Main(string[] args)
{
Person person = new Student();
Student student = new Person();
Console.ReadLine();
}
报错误