您的位置:首页 > 编程语言 > C#

C#类使用base关键字,this关键字

2013-05-15 00:24 274 查看
class A
{
public int a;
public A(int i)
{
a=i;

}
public A(int i,int j)
{
a=i*j;

}
}

class B:A
{
public int m;
public B():this(5,6)            //指定类B使用的默认构造函数为B(int,int)
{

}
public B(int w,int q):base(i,j) //指定类B的父类使用的构造函数是A(int,int)
{
m=w*q;
}
}


总之,在子类使用base用来指定创建子类对象的时候,父类使用的构造函数,this修改当前类的默认构造函数!

base 也有当前类的父类的意思。this还有代表当前对象的含义!代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
class A
{
public void fly()
{
Console.WriteLine("I can fly");
}
}

internal class B : A
{

new public void fly()
{
Console.WriteLine("sory,I can not fly");
}
public void Action()
{
base.fly();
this.fly();
}
}
class Program
{
static void Main(string[] args)
{
B b1 = new B();
b1.Action();
Console.Read();
}
}
}

运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: