现在的位置: 首页 > 综合 > 正文

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

2014年02月17日 ⁄ 综合 ⁄ 共 635字 ⁄ 字号 评论关闭
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();
        }
    }
}

运行结果:

 

抱歉!评论已关闭.