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;
}