C # single inheritance

Source: Internet
Author: User
Public class animal {private string _ name; // animal name public string name {get {return _ name;} set {_ name = value ;}} Public Virtual string burrow () // define animal behavior {return "animal behavior:" ;}} public class mouse: Animal {public mouse () {base. name = "rat";} public override string burrow () {return base. burrow () + "the son of the mouse will make a hole! ";}}

Look at this piece of code:

Public override string burrow () {return base. Burrow () + "the son of the mouse will make a hole! ";}
To use override to override the keyword, the process corresponding to the parent class should have a virtual keyword description, indicating that the process is rewritten (the corresponding parent class process will not be executed ), you can use the base keyword to call all methods, attributes, and fields exposed by the parent class.

Public class books {// The Field modifier is public Public String bookname; Public String ISBN; public books (string bname, string bisbn) {This. bookname = bname; this. ISBN = bisbn ;}} public class sale: Books // defines the derived class {public double booklists; Public Sale (string bname, string bisbn, double struct) // subclass constructor: base (bname, bisbn) {booknumbers = bytes ;}}

The constructor of this Code subclass first executes the constructor of the parent class to initialize the field values. This is completely correct, but sometimes we do not need to initialize these values, the constructor of the parent class must be executed. In this case, we can reload the constructor of the parent class and add public books () to the parent class () {} This constructor can meet our requirements!

Modify the subclass construction method as follows:

Public Sale (string bname, string bisbn, double struct) // subclass constructor //: Base () // You can also cancel the annotation and use the same {bookdetail = comment;
}
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.