c#——继承[模拟server类]初始化过程顺序dmeo -凯发k8官方网
凯发k8官方网
收集整理的这篇文章主要介绍了
c#——继承[模拟server类]初始化过程顺序dmeo
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
模拟一个服务类,server类实现了服务器的创建逻辑,子类只要在生成实例对象时传递一个端口号即可创建一个监听该端口的服务,该代码意图如下:
(1)通过simpleserver的构造函数接收端口参数。
(2)子类的构造函数默认调用父类的构造函数。
(3)父类的构造函数调用子类的getport方法获得端口号。
(4)父类构造函数建立端口监听机制(以console.writeline替代)。
(5)对象创建完毕,服务监听启动,正常运行。
运行该程序,多运行几次,查看输出结果,写出你认为的code 1 – 5的执行顺序,并解释原因。
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks;namespace demo2 {/*** 以下代码模拟一个服务类,server类实现了服务器的创建逻辑,子类只要在生成实例对象时传递一个端口号即可创建一个监听该端口的服务,该代码意图如下:*(1)通过simpleserver的构造函数接收端口参数。*(2)子类的构造函数默认调用父类的构造函数。*(3)父类的构造函数调用子类的getport方法获得端口号。*(4)父类构造函数建立端口监听机制(以console.writeline替代)。*(5)对象创建完毕,服务监听启动,正常运行。* 运行该程序,多运行几次,查看输出结果,写出你认为的code 1 – 5的执行顺序,并解释原因。*/abstract class server{protected const int default_port = 40000;//code1public server(){console.writeline("2");int port = getport();//code2console.writeline("port: " port);}protected abstract int getport();}class simpleserver : server{private int port = 100;//code3public simpleserver(int _port){console.writeline("4");port = _port;//code4}protected override int getport(){console.writeline("5");return (new random()).nextdouble() > 0.5 ? port : default_port;//code5}}class program{public int i = 0;static void main(string[] args){server s = new simpleserver(1000);/*** code3 => code1 => code2 => code5 => code4* 1)继承类静态成员变量初始化 * 2)继承类实例变量初始化 * 3)基类静态静态成员变量初始化 * 4)基类实例变量初始化 * 5)基类构造方法调用 * 6)继承类构造方法调用*/}} }https://www.iteye.com/blog/fhuan123-829394
总结
以上是凯发k8官方网为你收集整理的c#——继承[模拟server类]初始化过程顺序dmeo的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: c#——ellipse(椭圆)类[继承e
- 下一篇: