[. Net Object-Oriented Programming Basics] (15) abstract class,. net Object-Oriented Programming

Source: Internet
Author: User
Tags protected constructor

[. Net Object-Oriented Programming Basics] (15) abstract class,. net Object-Oriented Programming

[. NetObject-Oriented Programming Basics] (15)Abstract class

We have used the virtual method (UseVirtualModifier) And abstract classes and abstract methods (using abstract modifiers) we mentioned in the polymorphism section that to override class members, we must define them as a virtual method or abstract method. This section introduces the abstract class because the abstract is. net implements an important idea of object-oriented programming. Defining an abstract class is like an outline of a template. It can be understood as a central guiding ideology. we can implement it through a derived class. It can be seen that the abstract class has no function and cannot be instantiated, because it does not have specific things. For example, in the animal class example in the previous section, it doesn't make sense to instantiate an animal class. The classes we need to instance are specific animals, such as Dog, Cat, and Sheep.

When we use object-oriented programming, we need to learn to abstract the commonalities of things and form a base class. Other classes inherit from them and implement them. It is helpful to improve programming and learning design patterns for us.

1.What is an abstract class?

If a class is not associated with a specific thing, but only expresses an abstract concept, it is only used as a base class of its derived class. Such a class is an abstract class.When declaring a method in an abstract class, adding abstract is an abstract method.

1.Abstract class declaration

Use of abstract classesAbstractKeyword.

2.Abstract class features:

A. abstract classes cannot be instantiated.

B. An abstract class is a special class that has the characteristics of a class. Its members can be abstract or abstract.

C. abstract methods must be defined in abstract classes rather than non-abstract classes.

D. An abstract class can inherit an abstract class. Its derived class is still an abstract class.

E. abstract classes cannot be sealed (sealed cannot be inherited ))

3. Features of abstract methods:

A. the abstract method is an implicit virtual method.

B. abstract methods can only be declared in abstract classes.

C. Abstract METHODS cannot provide actual implementations, so there is no method body. The implementation of abstract methods is implemented by override in non-Abstract Derived classes.

D. static or virtual modifiers cannot be used in abstract method declarations.

E. abstract keywords cannot modify static methods or static attributes

6.Constructor of abstract classes

A. do not define constructors with public or protected internal access permissions in abstract classes.

B. the constructor with the protected or private access permission should be defined in the abstract class.

C. If a protected constructor is defined in the abstract class, the base class can execute the initialization task when instantiating the derived class.

7.Differences between abstract and Virtual Methods

Virtual method (UseVirtualModifier)There is an implementation part, and the derived class override is optional; the abstract method does not implement the part, and the non-Abstract derived class is forced to override it.

8.Differences between abstract classes and interfaces

In fact, this difference is intended to be explained in the next section and in the interface. I 'd like to explain it in advance.

A. Their Derived classes can only inherit one base class, that is, only one abstract class can be inherited, but multiple interfaces can be inherited.

B. abstract classes can define Member implementations, but interfaces cannot.

C. abstract classes include fields, constructor, destructor, static members, or constants. They are not allowed in interfaces.

D. The members in the abstract class can be private (as long as they are not abstract), protected, internal, or protected internal members, but the members in the interface must be public.

PS: abstract classes and interfaces are used for completely different purposes.Abstract classes are mainly used as the base classes of object series and share some main features, such as the common purpose and structure. Interfaces are mainly used for classes. These classes are different at the basic level, but some identical tasks can still be completed.

7.Example

Let's take an example to illustrate how to use an abstract class. In our previous example, Animal is defined as an abstract class. There are several Derived classes, such as dogs, cats, and sheep, if we have cats and Persian cats, dogs and sheepdogs, we can also let them inherit again, because cats and dogs are also animals, and we can not define them as abstract classes to support instantiation. Take a look at the UML diagram below:

 

I will write the UML diagram at a later time. The member + in the diagram indicates public and-Indicates private. The fields, attributes, and methods use the split line.

The specific implementation code is as follows:

1 /// <summary> 2 // animal class (parent class abstract class) 3 /// </summary> 4 abstract class Animal 5 {6 /// <summary> 7 /// name 8 /// description: class and subclass can access 9 /// </summary> 10 protected string name; 11 12 13 // <summary> 14 // constructor 15 /// </summary> 16 // <param name = "name"> </param> 17 public Animal (string name) 18 {19 this. name = name; 20} 21 22 private int shoutNum = 3; 23 public int ShoutNum 24 {25 get {return shoutNum ;} 26 set {shoutNum = value;} 27} 28 29 // <summary> 30 // name (Virtual attribute) 31 /// </summary> 32 public virtual string MyName 33 {34 get {return this. name;} 35 36} 37 38 // <summary> 39 // call, this method removes the virtual method, write the loop here 40 // </summary> 41 public void Shout () 42 {43 string result = ""; 44 for (int I = 0; I <ShoutNum; I ++) 45 result + = getShoutSound () + "! "; 46 47 Console. writeLine (MyName); 48 Console. writeLine (result); 49} 50 51 // <summary> 52 // create a call imaginary method, subclass rewrite 53 // </summary> 54 // <returns> </returns> 55 public virtual string getShoutSound () 56 {57 return ""; 58} 59 60 61} 62 63 // <summary> 64 // Dog (subclass) 65 /// </summary> 66 class Dog: animal 67 {68 string myName; 69 public Dog (string name) 70: base (name) 71 {72 myName = name; 73} 74 75 // <summary> 76 // name (override the parent class attribute) 77 // </summary> 78 public override string MyName 79 {80 get {return "I am: dog, my name is:" + this. name;} 81} 82 83 // <summary> 84 // call (override parent class method) 85 // </summary> 86 public override string getShoutSound () 87 {88 return "Wang! "; 89} 90} 91 92 // <summary> 93 // dog (subclass) 94 // </summary> 95 class ShepherdDog: dog 96 {97 string myName; 98 public ShepherdDog (string name): base (name) 99 {100 myName = name; 101} 102 103 // <summary> 104 // name (override the parent class attribute) 105 /// </summary> 106 public override string MyName107 {108 get {return "I am: sheepdog, my name is:" + this. name;} 109} 110 111 // <summary> 112 // call (override parent class method) 113 // </summary> 114 public ov Erride string getShoutSound () 115 {116 return "Wang ~ Wow! "; 117} 118} 119 120 // <summary> 121 // Cat (subclass) 122 // </summary> 123 class Cat: Animal124 {125 string myName; 126 public Cat (string name) 127: base (name) 128 {129 myName = name; 130} 131 // <summary> 132 // name (override parent class attributes) 133 /// </summary> 134 public override string MyName135 {136 get {return "I am: cat, my name is:" + this. name;} 137 138} 139 140 // <summary> 141 // call (override parent class method) 142 // </summary> 143 public override st Ring getShoutSound () 144 {145 return "Meow! "; 146} 147} 148 149 // <summary> 150 // CAT (subclass) 151 // </summary> 152 class PersianCat: Cat153 {154 string myName; 155 public PersianCat (string name): base (name) 156 {157 myName = name; 158} 159 // <summary> 160 // name (override parent class attributes) 161 /// </summary> 162 public override string MyName163 {164 get {return "I am: Persian cat, my name is:" + this. name;} 165 166} 167 168 // <summary> 169 // call (override parent class method) 170 // </summary> 171 public ove Rride string getShoutSound () 172 {173 return "meow ~ Wow! "; 174} 175} 176 177 178 // <summary> 179 // goat (subclass) 180 // </summary> 181 class Sheep: animal182 {183 string myName; 184 public Sheep (string name) 185: base (name) 186 {187 myName = name; 188} 189 // <summary> 190 // name (override the parent class attribute) 191 /// </summary> 192 public override string MyName193 {194 get {return "I am: Goat, my name is:" + this. name;} 195 196} 197 // <summary> 198 // call (override parent class method) 199 // </summary> 200 public overrid E string getShoutSound () 201 {202 return! "; 203} 204}

 

The call result is as follows:

// Call Animal persianCat = new PersianCat ("Good Cat"); persianCat. Shout (); Console. ReadLine ();
// The result is as follows: // I am a persian cat. My name is haomu. // meow ~ Wow !! Meow ~ Wow !! Meow ~ Wow !!

 

10Key points:

Abstract class refers to a class that is not associated with a specific thing, but only expressed as an abstract concept. It is only used as a base class of its derived class.

An abstract class is a special class.

Abstract classes cannot be instantiated.

An abstract class can inherit an abstract class.

Abstract METHODS can only exist in abstract classes. They can be rewritten in Derived classes like virtual methods.

The existence of abstract classes is inherited, so they cannot be sealed (that is, they cannot be modified using sealed)

Finally, abstraction is an important idea of object-oriented programming ..

 

========================================================== ========================================================== ====================

Returned directory

<If it is helpful to you, remember to click on the recommendations. If you do not understand the content or do not write it correctly, please contact us more>
 

========================================================== ========================================================== ====================

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.