C # Study Notes 4,

Source: Internet
Author: User

C # Study Notes 4,

1.C # only supports single inheritance. If you want to implement multiple inheritance. There are two methods:

The first is to pass inheritance. Class A is the base class. Class B inherits from Class A and Class C inherits from Class B, and the elements to be included are added through transmission. However, this inheritance Design, it has obvious subordination in terms of business.

The second is aggregation. If a core component of an inclusive object is defined in the association, aggregation will occur. For multi-inheritance, this involves selecting a class as the main base class (PdaItem), then generating a new class (Contact) from it, and the second expected base class (Person) add as a field in the derived class (Contact. Then, all non-private members in the field (Person) are redefined on the derived class (Contact. Then, the derived class (Contact) delegates the call to the field (Person ). Because methods need to be re-declared, some code will be repeated, but the repeated code will not have many roots, because the actual method subject is only implemented in the aggregate class (Person.

2.Do not call any virtual methods of the constructed object in the constructor, because if this virtual method is overwritten in the derived type of the type to be instantiated, the rewrite implementation is called. However, in the inheritance hierarchy, the base class is being constructed and the derived class is not yet constructed. Therefore, calling virtual methods will lead to unpredictable behavior.

3.New modifier: The new Keyword can explicitly hide members inherited from the base class. When the inherited member is hidden, the derived version of the member replaces the base class version. Although you can hide a member without using the new modifier, a warning is generated. If new is used to explicitly hide a member, the warning is canceled and the fact that the member is replaced with a derived version is recorded.

4.Sealed modifier: In addition to modifier classes (Seal classes, cannot be derived), virtual members can also be sealed. For example, Class A has A virtual method Say (), class B is inherited and then rewritten to Say (). Class B can be modified using sealed to prevent subsequent classes from rewriting to Say.

5.Is OPERATOR: not only checks whether data can be converted to the target type, but also checks whether the object type is actually the target type. Therefore, objects that can be forcibly converted are not necessarily of the target type. Such as Thing code.

Public class Thing {public static explicit operator string (Thing thing) {return thing. toString () ;}public override string ToString () {return "Thing" ;}public static void Test () {string s = "text"; Thing t = new Thing (); object o = t; Trace. assert (! (T is string); Trace. assert (string) t = "Thing"); o = s; Trace. assert (string) o = "text"); Trace. assert (o is string); Console. writeLine ("OK") ;}} public class PdaItem {public DateTime CreateTime {get; set;} public DateTime ModifyTime {get; set ;}public string Name {get; set ;} protected string ObjectKey {get; set ;}} public class Contact: PdaItem {private Person internalPerson; // detailed Code of the Person class, which is listed in the previous chapter public string Address {get; set;} public string Phone {get; set;} public string FirstName {get {return internalPerson. firstName;} set {internalPerson. firstName = value ;}} public string LastName {get {return internalPerson. lastName;} set {internalPerson. lastName = value ;}}}

--------------------- The above content is organized according to "C # the third edition of this topic ".

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.