Object-oriented basics of design patterns

Source: Internet
Author: User

I recently read the design model. I like this book very much, and the content is also very understandable. I am a little addicted to reading it. Haha, I first looked at the object-oriented basics. I have been familiar with object-oriented basics before, I have a further understanding of the three features of object-oriented, and the following is a summary:

1. Encapsulation

Concept: each object contains all the information required for operations. This feature is called encapsulation.

Understanding: Like a big box, it has the common characteristics of some things. What I need to do is instantiate these commonalities as an example. For example, the Cat class is instantiated as a Cat named Tom.

Advantages: 1. Reduce coupling, that is, it can reduce the dependence and connection on the outside world and be as independent as possible; 2. Internal Implementation of the class can be freely modified, but it will reduce the impact on the outside world; 3. The class has clear external interfaces;

Example: The two classes do not affect each other and they do not depend on each other.

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. threading. tasks; using System. windows. forms; namespace encapsulation {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private void button#click (object sender, EventArgs e) {Cat cat = new Cat ("dandan"); cat. shoutNum = 5; MessageBox. show (cat. shout ();} class Cat {private string name = ""; public Cat (string name) {this. name = name;} public Cat () {this. name = "Dan";} public string Shout () {string result = ""; for (int I = 0; I <shoutNum; I ++) {result + = "meow";} return "my name is" + name + "meow";} public int shoutNum = 3; public int ShoutNum {get {return shoutNum ;} set {if (value <= 10) shoutNum = value; else shoutNum = 10 ;}} private void button2_Click_1 (object sender, EventArgs e) {Dog dog = new Dog ("dandan"); dog. shoutNum = 5; MessageBox. show (dog. shout ();} class Dog {private string name = ""; public Dog (string name) {this. name = name;} public Dog () {this. name = "Dan";} public string Shout () {string result = ""; for (int I = 0; I <shoutNum; I ++) {result + = "Wang";} return "my name is" + name + "Wang";} public int shoutNum = 3; public int ShoutNum {get {return shoutNum ;} set {if (value <= 10) shoutNum = value; else shoutNum = 10 ;}}}}}


2. Inheritance

Concept: Object Inheritance represents a relationship of 'is-a'. If two objects A and B can be described as 'B is A', B can inherit.

Understanding: for example, if a pen is a stationery, the pen inherits the stationery. However, in addition to its characteristics, the pen also has its own characteristics. For example, the pen needs to be written in steel, and the pen tip is made of steel. For example, a son and a father have some characteristics of his father's appearance, but his height must be high and big eyes. These are new features, the father is also called a parent class or a base class in the object-oriented model, while the son is called a subclass or a derived class.

Advantage: the common parts of all sub-classes are placed in the parent class, so that the code is shared, which avoids duplication, inheritance makes modifications or extensions easier.

Note: 1. subclass has non-private attributes and functions (public and protected) of the parent class; 2. subclass has its own attributes and functions, that is, subclass can extend the attributes and functions that the parent class does not have. 3. Subclass can implement the features of the parent class in its own way (method rewriting ).

Example: I think you can see that the above example has a lot of repeated code, which not only increases the amount of code, but also is prone to errors, so we use inheritance, extract the common parts of them as the parent class, so that Cat and Dog can inherit from each other. The following is the improved code. After comparison, we can see the role of inheritance.

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. threading. tasks; using System. windows. forms; namespace inherits {public partial class Form1: Form {public Form1 () {InitializeComponent ();} class Animal {protected string name = ""; public Animal (string name) {this. name = name;} public Animal () {this. name = "";} protected int shoutNum = 3; public int ShoutNum {get {return shoutNum;} set {shoutNum = value ;}} private void button#click (object sender, eventArgs e) {Cat cat = new Cat ("dandan"); cat. shoutNum = 5; MessageBox. show (cat. shout ();} class Cat: Animal {public Cat (): base () {} public Cat (string name): base (name) {} public string Shout () {string result = ""; for (int I = 0; I <shoutNum; I ++) {result + = "meow ";} return "my name is" + name + "" + result;} private void button2_Click (object sender, EventArgs e) {Dog dog = new Dog ("dandan "); dog. shoutNum = 5; MessageBox. show (dog. shout ();} class Dog: Animal {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 ;}}}}
3. Polymorphism

Concept: different objects can execute the same action, but they must be implemented through their own code.

Understanding: for example, in our code, all cats and dogs are called but their voices are different. Then we can write a method called parent class, however, the parent class cannot directly implement the call of a cat or dog. Therefore, you need to virtualize the parent class with virtual, and then rewrite the method with the Child class with override.

Example: In this example, because polymorphism is applied, the running results will show up after the animal registration is complete, and each animal in the call competition will automatically combine with the corresponding call, rewrite the Shout () method to get the correct call.

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. threading. tasks; using System. windows. forms; namespace polymorphism {public partial class Form1: Form {public Form1 () {InitializeComponent ();} class Animal {protected string name = ""; public Animal (string name) {this. name = name;} public Animal () {this. name = "";} protected int shoutNum = 3; public int ShoutNum {get {return shoutNum;} set {shoutNum = value ;}} public virtual string Shout () {return "" ;}} private void button#click (object sender, EventArgs e) {Cat cat = new Cat ("dandan"); cat. shoutNum = 5; MessageBox. show (cat. shout ();} class Cat: Animal {public Cat (): base () {} public Cat (string name): base (name) {} public override string Shout () {string result = ""; for (int I = 0; I <shoutNum; I ++) result + = "meow "; return "my name is" + name + "" + result;} private void button2_Click (object sender, EventArgs e) {Dog dog = new Dog ("dandan "); dog. shoutNum = 5; MessageBox. show (dog. shout ();} class Dog: Animal {public Dog (): base () {} public Dog (string name): base (name) {} public override string Shout () {string result = ""; for (int I = 0; I <shoutNum; I ++) result + = "Wang "; return "my name is" + name + "" + result;} private void Form1_Load (object sender, EventArgs e) {} private Animal [] arrayAnimal; // Animal array // Animal Registration private void button3_Click (object sender, EventArgs e) {arrayAnimal = new Animal [2]; arrayAnimal [0] = new Cat ("Xiaohua "); arrayAnimal [1] = new Dog ("Dan")} private void button4_Click (object sender, EventArgs e) {foreach (Animal item in arrayAnimal) // traverses the array, shout () {MessageBox. show (item. shout ());}}}}
Our understanding of these three features remains to be improved. I hope you can provide guidance!


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.