看题,然后写出输出结果:(2.0的framework没法自动化属性)
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication13 { public abstract class A { private string _name; public string Name { get { return _name; } set { value = this._name; } } private int a = 12; public A() { Console.WriteLine("A"); } public virtual void Fun() { Console.WriteLine("A.Fun()"); } } public class B : A { public int _age; public int age { get { return _age; } set { value = this._age; } } public int c = 12; public B() { Console.WriteLine("B"); } public new void Fun() { Console.WriteLine("B.Fun()"); } } class Program { static void Main(string[] args) { //当有继承再的时候,先实例调用父类中的 //1.先初始化本类中的字段 //2.接着初始化父类中的字段 //3.调用父类中的构造函数进行属性的初始化 //4.最后调用本类中的构造函数进行属性的初始化 //所以结果是: //A //B //A.Fun() A a = new B();//子类实例化父类; a.Fun(); Console.ReadLine(); } } }
变形一:
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication13 { public abstract class A { private string _name; public string Name { get { return _name; } set { value = this._name; } } private int a = 12; public A() { Console.WriteLine("A"); } public virtual void Fun() { Console.WriteLine("A.Fun()"); } } public class B : A { public int _age; public int age { get { return _age; } set { value = this._age; } } public int c = 12; public B() { Console.WriteLine("B"); } public override void Fun() { Console.WriteLine("B.Fun()"); } } class Program { static void Main(string[] args) { //结果是: //A //B //B.Fun() A a = new B();//子类实例化父类; a.Fun(); Console.ReadLine(); } } }
变形二:
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication13 { public abstract class A { private string _name; public string Name { get { return _name; } set { value = this._name; } } private int a = 12; public A() { Console.WriteLine("A"); } public virtual void Fun() { Console.WriteLine("A.Fun()"); } } public class B : A { public int _age; public int age { get { return _age; } set { value = this._age; } } public int c = 12; public B() { Console.WriteLine("B"); } public new void Fun() { Console.WriteLine("B.Fun()"); } } class Program { static void Main(string[] args) { //结果是: //A //B //B.Fun() B b = new B();//实例化子类; b.Fun(); Console.ReadLine(); } } }
总结:
public class father { public void normalFun() { Console.WriteLine("f-normal"); } public virtual void virFun() { Console.WriteLine("f-virtual"); } } public class son : father { public new void normalFun() { Console.WriteLine("s-normal"); } public override void virFun() { Console.WriteLine("s-virtual"); } } class Program { static void Main(string[] args) { father f = new son(); f.normalFun(); //f-normal f.virFun(); // s-virtual //记住: //new 的本质是:覆盖.隐藏 父类中的方法。 //ovrride 的本质是:对父类中方法的扩展; Console.ReadLine(); } }
然后接着:我们再来看一道题:
public class A { public virtual void Fun1(int i) { Console.WriteLine(i); } public void Fun2(A a) { a.Fun1(1); Fun1(5); } } public class B : A { public override void Fun1(int i) { base.Fun1(i + 1); } public static void Main() { B b = new B(); A a = new A(); a.Fun2(b); //2 5 b.Fun2(a); //1 6 Console.ReadLine(); } }
时间: 2024-09-30 06:51:08