第三章知识点归纳

1.对象初始化器

   在调用构造函数时直接初始化对象。

 

public class Student
{
    public string Name;
    public int Age;
    public bool Gender;
}



Student stu=new Student{Name="zhangsan",Age=10,Gender=false};

    等同于:

Student stu=new Student();
stu.Name="zhangsan";
stu.Age=10;
stu.Gender=false;


2.this 和 base

   this 是引用类实例自身。

 

public class Student
{
  string name;
  public Test(string name){this.name=name;}
}


base作用 : 1. 从子类访问重载的基类方法成员 2.调用基类的构造方法

  引用书上的例子:

public class Asset
{
   public string name;
   public virtual decimal Liability{ get { return 0; } }
}

public class Home : Asset
{
   public decimal Mortgage;
    public override decimal Liability
    {
        get { return base.Liability + Mortgage; }
    }
}


3.装箱和拆箱

   装箱是将值类型转换为引用类型。

   int num=10;

   object obj = x; //把int类型装箱

  拆箱是讲引用类型转换为值类型。

  object obj = 10;

  int num= (int)obj;

  装箱和拆箱的实质是复制: 装箱是把值类型的实例复制到新对象中,拆箱是把对象的内容复制回数值类型的实例中。

 

 4. 协变和逆变

    协变:假定A是B的子类,如果C<A>可以引用转化成C<B>,那么称C为协变类。

    IAbc<string> a = ... ;

    IAbc<object> b = a;

    IAbc<T> 是协变类。

    逆变:假定A是B的子类,如果C<B>可以引用转化成C<A>,那么称C为逆变类。

posted @ 2015-06-12 15:11  豆腐爱鱼儿  阅读(204)  评论(0编辑  收藏  举报