C # Summary (III)-three major features (encapsulation, inheritance, and polymorphism)

Source: Internet
Author: User

1. encapsulation 1. Define that each object contains all the information required for operations. This feature is called encapsulation. Therefore, the object does not have to have other objects to complete its own operations. In this way, methods and classes are encapsulated in classes and implemented through class instances. 2. Good encapsulation can reduce coupling; internal implementation of the class can be freely modified; the class has clear interfaces; 3. instance [csharp] <span style = "font-size: 18px; "> class Cat {private string name =" "; public Cat (string name) {this name = name;} public string Shout () {return "my name is" + name + "kitten" ;}}</span> 2. Inheritance 1. Definition inheritance is to obtain some public members from the parent class, such as methods and attributes. In C #, only one parent class can be inherited, but multiple interfaces can be inherited. If the subclass inherits the interface, all public members defined in the interface must be implemented. A public member is a member defined as public in the parent class (the public scope can take effect in the subclass, but the private scope cannot). The subclass inherits the parent class: subclass has non-Private attributes and functions of the parent class; subclass has its own attributes and functions, that is, subclass can expand the attributes and functions that the parent class does not have; subclass can also implement the function of the parent class (method rewriting) in its own way. 2. Advantages inheritance puts all the public parts of the subclass in the parent class to share the code, this avoids duplication. In addition, inheritance makes it easier to modify or extend the inherited implementations. 3. instance [csharp] <span style = "font-size: 18px;"> Example 1: // parent class ParentClass classParentClass {privatestring_name; // attribute public stringParentName // parent class name {get {return _ name ;}set {_ name = value ;}}} </span> <span style = "font-size: 18px;"> // subclass // parent class SonClass: ParentClass {privatestring_name; // attribute public stringSonName // subclass name {get {return _ name;} set {_ name = value ;}}// client SonClass myson = new SonClass (); // create Subclass object myson. parentName = "parent"; // automatically inherits the attributes of the parent class myson. sonName = "sub-name"; // attributes of the sub-class: private void button#click (object sender, EventArgs e) {Cat cat = new Cat ("Mimi "); // declare a Cat object for class instantiation. The object name is cat; newCat () instantiate the Cat object. shoutNum = 5; // assign MessageBox to the attribute. show (cat. shout ();} class Animal // defines the parent class {protected string name = ""; // note that the Protected subclass can inherit public Animal (string name) {this. name = name;} public Animal () {This. name = "";} protected int shoutNum = 3; // Changes to protected public int ShoutNum {get {return shoutNum ;}set {shoutNum = value ;}} class Cat: animal // note the changes {public Cat (): base () // subclass constructor needs to call the constructor of the same parameter type of the parent class, use the base keyword to represent the parent class {} public Cat (string name): base (name) // note the changes {} public string Shout () {string result = ""; for (int I = 0; I <shoutNum; I ++) result ++ = "Meow! "; Return" My name is: "+ name +" "+ result;} class Dog: Animal // note changes {public Dog (): base () {} public Dog (string name): base (name) {} public string shout () {string result = ""; for (int I = 0; I <shoutNum; I ++) result + = "Wang! "; Return" My name is: "+ name +" "+ result;} private void button2_Click (objectsender, EventArgs e) {Dog dog = new Dog ("dog"); // class instantiation Cat cat Cat declares a cat object named cat; newCat () instantiate this Cat object Dog. shoutNum = 5; // assign MessageBox to the attribute. show (dog. shout () ;}</span> 4. Note that when Protected is inherited, The subclass can have full access to the base class. That is to say, the class member modified by protected is public to the subclass, and the segment is not public to other classes. Iii. polymorphism 1. Defining polymorphism means that classes can have multiple forms and multiple implementation methods can be formed through modifications. When a subclass inherits from the parent class, it obtains all methods, fields, attributes, and events of the parent class. Polymorphism indicates that different objects can perform the same operation, but they must be executed through their own implementation code. To change the data and behavior of the parent class, you can create a new member to replace the parent member, or override the virtual parent member. (1) to replace a parent Member, use the new keyword. The following two forms of the GetName method are described: return the parent name and return the child name. [Csharp] <span style = "font-size: 18px;"> first: return the parent class name class PerentClass // parent class {public string GetName () {return "parent name" ;}} type 2: return child name class SonClass: PerentClass // subclass inherits parent class {public new string GetName () {return "Sub-Level name" ;}} (2) rewrite two conditions for a virtual parent-level member: the parent-level member uses the keyword "virtual", and the Child-level member uses the "override ". Code class PerentClass // parent class {public virtual string GetName () {return "parent class name" ;}} class SonClass: perentClass // subclass inherits the parent class {public override string GetName () {return "sublevel name ";}} </span> 2. Note that when a subclass appears as a parent class in the parent class province, the ID of the subclass B parent class is displayed in its own way in the work, when the attributes and methods specific to a subclass cannot use polymorphism at the same time, you need to understand that virtual methods and method rewriting are used to completely replace the subclass instance with members from the parent class, the parent class must declare the member as virtual. This is achieved by adding virtual before the returned type of the member. The subclass selects the override keyword and replaces the parent class implementation with its own implementation. This is the method rewriting. 3. instance (1) Creation class "Employee" indicates employees in the company. It contains the virtual method GetDepart. Code: [csharp] class Employee // parent class {public virtual string GetDepart () {return "" ;}} (2) create class "MarketEmployee ", it overrides the GetDepart method in the parent class. Code [csharp] class MarketEmployee: Employee // subclass inherits the parent class {public override string GetDepart () {return "Marketing Department" ;}} (3) the client calls two methods. [Csharp] www.2cto. comEmployee myee = new Employee (); // create the parent class string str1 = myee. getDepart (); // The result is the parent company MarketEmployee yee = newMarketEmployee (); // create a subclass string str2 = yee. getDepart (); // The result is a subsidiary.

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.