override 子类继承父类 方法的实现 变量 构造方法的编译顺序

 下载链接地址:https://pan.baidu.com/s/1kVHaL19

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

namespace testOverride
{

//在这段代码中有单例模式的影子 子类 和父类的继承关系 在初始化的过程 顺序
//子类变量--父类变量--父类构造函数--子类构造函数
//new 表达式 如果父类有一个方法 consoleWrite 如果想些这个方法 而不被调用 就用new

class A
{
int x = 1, y;
public A()
{
consoleWrite();
}
public virtual void consoleWrite()
{
Console.WriteLine("write by A -->'{0}','{1}'", x.ToString(), y.ToString());
}
}

class B : A
{
int x = 1, y = 2;
static int m;
public B()
{
m = getM;
}
public static int getM
{
get
{
if (m != 3)
{
m = 3;
Console.WriteLine("init the para......");
}
return m;
}
}

public override void consoleWrite()
{
Console.WriteLine("write by B -->'{0}','{1}','{2}'", x.ToString(), y.ToString(), m.ToString());
}

//public new void consoleWrite()
//{

// Console.WriteLine("write by B -->'{0}','{1}','{2}'", x.ToString(), y.ToString(), m.ToString());
//}
}

class Program
{
static void Main(string[] args)
{
for (int l = 0; l < 10; l++)
{
B b2 = new B();
}
Console.ReadLine();
}
}
}

posted @ 2016-09-05 15:38  再见大圣--无毛猴子  阅读(167)  评论(0)    收藏  举报