一、Base关键词的几种用法base重要用于OOP的多态上,base 关键字用于在派生类中实现对基类公有或者受保护成员的访问,但是只局限在构造函数、实例方法和实例属性访问器中 1、base调用基类构造函数
using System;
namespace BaseDemo { class Program { static void Main(string[] args) { B b = new B(); Console.ReadLine(); } } public class A { public A() { Console.WriteLine("Build A"); } } public class B : A { public B() : base() { Console.WriteLine("Build B"); } } } 执行结果: 2、base在派生类中调用基类的方法
using System;
namespace BaseDemo { class Program { static void Main(string[] args) { B b = new B(); b.Hello(); Console.ReadLine(); } } public class A { public virtual void Hello() { Console.WriteLine("Hello"); } } public class B : A { public override void Hello() { base.Hello(); } } } 执行结果: 二、this关键字1、 this代表当前类的实例对象
using System;
namespace thisDemo { class Program { static void Main(string[] args) { A a = new A(); a.GetName(); Console.ReadKey(); } } public class A { private string name = "qxh"; public void GetName() { string name = "欢迎你"; // this代表A的实例对象 // 所以this.name对应的是"qxh" // name对应的是GetName方法内的局部变量 Console.WriteLine($" {this.name} - {name}"); } } } 执行结果: 2、 用this串联构造函数
using System;
namespace thisDemo { class Program { static void Main(string[] args) { A a = new A("qxh"); Console.ReadKey(); } } public class A { public A() { Console.WriteLine("无参构造函数"); } // this()对应无参构造方法A() // 先执行A(),后执行A(string text) public A(string text) : this() { Console.WriteLine(text); Console.WriteLine("有参构造函数"); } } }
执行结果: 3、扩展方法
using System;
namespace thisDemo { class Program { static void Main(string[] args) { string name = "qxh"; Console.WriteLine(name.ShowInfo("欢迎你"));
Console.ReadKey(); } } /// <summary> /// 必须是静态类 /// </summary> public static class A { public static string ShowInfo(this string str, string info) { return $"{str}-{info}"; } } }
执行结果: 4、索引器:参数可以是int,也可以是string等其他类型
using System;
namespace thisDemo { class Program { static void Main(string[] args) {
A a = new A(); Console.WriteLine(a["qxh"]); Console.ReadKey(); } } public class A { public string Name { get; set; } //索引器 []括号里面可以是string,int,array等 public string this[string index] { get { if (index == "qxh") { return index; } else { return "gg"; } } set { Name = value; } } } }
执行结果: 三、New关键字1、new 运算符:用于创建对象和调用构造函数(1)特点
(2)demostatic void Main(string[] args) { A a = new A();//new操作符 创建对象和调用构造函数 Console.ReadKey(); } 2、new 修饰符:new 关键字可以显式隐藏从基类继承的成员new 关键字可以显式隐藏从基类继承的成员。 隐藏继承的成员时,该成员的派生版本将替换基类版本。 虽然可以在不使用 new 修饰符的情况下隐藏成员,但会生成警告。 如果使用 new 显式隐藏成员,则会取消此警告,并记录要替换为派生版本这一事实。下面是隐藏 A中的Name属性
public class A { public string Name { get; set; } = "qxh"; }
public class B : A { new public string Name { get; set; } = "dachong"; }
3、new 约束:用于在泛型声明中约束可能用作类型参数的参数的类型new 约束指定泛型类声明中的任何类型参数都必须有公共的无参数构造函数。如果要使用 new 约束,则该类型不能为抽象类型。当与其他约束一起使用时,new() 约束必须最后指定: public class A<T>where T : IComparable, new() { //// } 四、override关键字要扩展或修改继承的方法、属性、索引器或事件的抽象实现或虚实现,必须使用 override 修饰符。 由 override 声明重写的方法称为重写基方法。重写的基方法必须与 override 方法具有相同的签名。不能重写非虚方法或静态方法。 重写的基方法必须是 virtual、abstract 或 override 的。用关键字 virtual 修饰的方法,叫虚方法。可以在子类中用override 声明同名的方法,这叫“重写”。相应的没有用virtual修饰的方法,我们叫它实方法。
using System;
namespace thisDemo { class Program { static void Main(string[] args) { B b = new B(); b.Show(); Console.ReadKey(); } } public class A { public virtual void Show() { Console.WriteLine("A"); } }
public class B : A { public override void Show() { Console.WriteLine("B"); } } }
执行结果: 五、abstract关键字
|
|
来自: ontheroad96j47 > 《待分类》