The c derived class calls the base class constructor.doc

上传人:rrsccc 文档编号:9008953 上传时间:2021-01-29 格式:DOC 页数:13 大小:30.50KB
返回 下载 相关 举报
The c derived class calls the base class constructor.doc_第1页
第1页 / 共13页
The c derived class calls the base class constructor.doc_第2页
第2页 / 共13页
The c derived class calls the base class constructor.doc_第3页
第3页 / 共13页
The c derived class calls the base class constructor.doc_第4页
第4页 / 共13页
The c derived class calls the base class constructor.doc_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《The c derived class calls the base class constructor.doc》由会员分享,可在线阅读,更多相关《The c derived class calls the base class constructor.doc(13页珍藏版)》请在三一文库上搜索。

1、The c # derived class calls the base class constructorIts very, very thin.The default constructor in this article refers to the systems default non-parametric constructor without writing a constructorWhen you dont write your own constructor in the base class, the default constructor of the call base

2、 class is derived by the derived classEx:Public class MyBaseClassPublic class MyDerivedClass: MyBaseClassPublic MyDerivedClass ()The Console. WriteLine ( Im a subclass non-parametric constructor );Public MyDerivedClass (int I)The Console. WriteLine ( Im a constructor for a parameter );Public MyDeriv

3、edClass (int I, int j)Console. WriteLine ( Im a constructor for two parameters );When a derived class is instantiated, the base class default constructor is called2, write constructor as base class, the derived class does not specify which constructor is invoked, will be looking for a no-parameter c

4、onstructor, if there is no error, the other by any call which in a derived class constructor is looking for the base class constructor, no arguments rather than parameter matching.Ex:Public class MyBaseClassPublic MyBaseClass (int I)The Console. WriteLine ( I am a constructor with a parameter );Publ

5、ic class MyDerivedClass: MyBaseClassPublic MyDerivedClass ()The Console. WriteLine ( Im a subclass non-parametric constructor );Public MyDerivedClass (int I)The Console. WriteLine ( Im a constructor for a parameter );Public MyDerivedClass (int I, int j)Console. WriteLine ( Im a constructor for two p

6、arameters );When the derived class is instantiated, the error is reportedThe constructor is written in the base class, and the derived class can specify a constructor that invokes the base class, using the base keyword.The ExPublic class MyBaseClassPublic MyBaseClass (int I)The Console. WriteLine (

7、I am a constructor with a parameter );Public class MyDerivedClass: MyBaseClassPublic MyDerivedClass () : base (I)The Console.WriteLine ( Im a subclass non-parametric constructor );Public MyDerivedClass (int I) : base (I)The Console. WriteLine ( Im a constructor for a parameter );Public MyDerivedClas

8、s (int I, int j) : base (I)Console. WriteLine ( Im a constructor for two parameters );When you instantiate a derived class with a constructor that takes a parameter, it is not wrong because he specifies the constructor of the base class.4, if the base class constructor does not contain a no-paramete

9、r constructor, then the derived class constructor must specify all call the base class constructor, otherwise the errorThe ExPublic class MyBaseClassPublic MyBaseClass (int I)The Console. WriteLine ( I am a constructor with a parameter );Public class MyDerivedClass: MyBaseClassPublic MyDerivedClass

10、()The Console. WriteLine ( Im a subclass non-parametric constructor );Public MyDerivedClass (int I) : base (I)The Console. WriteLine ( Im a constructor for a parameter );Public MyDerivedClass (int I, int j)Console. WriteLine ( Im a constructor for two parameters );Compile will not pass at this timeA

11、ttached: a derived class can write a derived class constructor without invoking the base class constructor, such as below:The base classUsing System;Public class QuadPublic double width;Public double length;Public Quad (double w, double l)Width = w;Length = l;Public Quad ()Width = 1;Length = 22;Publ

12、ic double GetArea ()Return the width * length;Derived classesUsing System;The class NameQuad: QuadString name;/ / public NameQuad (double w, double l, string s)/ / : base (w, l)/ / / / name = s;/ /Public NameQuad (double w, double l, string s)Width = w;Name = s;Length = l; / / derived class its own

13、constructorPublic string GetName ()Return the name;Theres nothing wrong with that, but the Internet says no, please explain it.The constructor also invokes the constructor inside the class, using this keyword:Using System;The class QuadDouble width;Double length;Public Quad (double w, double l)Width

14、 = w;Length = l;Public Quad ()This (8.0, 1.0)Public Quad (double d): this (d, d)Public double GetArea ()Return the width * length;Using System;Class ProgramStatic void Main (string args)Quad Q1 = new Quad ();Quad Q2 = new Quad (2.0);Quad Q3 = new Quad (2.0, 3.0);WriteLine ( 0 , Q1. GetArea ();WriteLine ( 0 , Q2. GetArea ();Console. WriteLine ( 0 , q3.getarea ();The Console. ReadKey ();Structure of 8 minus

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1