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(); } } }
运行结果:
相关文章推荐
- 【面向对象】访问this关键字和base关键字使用
- .Net (C#)Base关键字的使用
- C#继承中的静态构造函数和base关键字使用
- 黑马程序员——C#枚举 结构体 this关键字 base关键字 值类型 引用类型 装箱和拆箱
- c#访问this关键字和base关键字示例
- 使用C# yield关键字来提高性能和可读性
- 使用C# yield关键字来提高性能和可读性
- c#:使用using关键字自动释放资源未必一定就会有明显好处
- 【C#函数式编程】C#中的函数式编程(三) —— yield关键字的使用
- C#学习笔记之六 类定义中static关键字的用法暨C#中静态变量的意义与使用
- C#中base和this关键字
- 如何使用C#关键字const,readonly,static
- C#中的var关键字的使用
- 使用 Override 和 New 关键字进行版本控制(C# 编程指南)
- C#中SQL Server数据库连接池使用及连接字符串部分关键字使用说明
- C# this关键字在静态方法中的使用。
- 18个不常见的C#关键字,您使用过几个?
- C#里面ref和out关键字使用与区别