Next we will go on to the previous blog and continue to learn the concept of implementing object-oriented with C. Here are some examples of animal competitions.
Polymorphism: It indicates that different objects can execute the same action, but they can execute the same action through their own implementation code. The parent class must be declared as virtual and override is used ).
Abstract class: the abstract class is declared with abstract. Note that the abstract class cannot be instantiated. The abstract method must be overwritten by the quilt class.
Interface: It combines implicit public methods and attributes to encapsulate a set of specific functions. Once the class implements the interface, it can support all the attributes specified by the interface. A class can support multiple interfaces, and multiple classes can also support the same interface.
Set: it is better than an array.. NET Framework provides a dedicated class for data storage and retrieval. The set will automatically increase as we add elements to it and adjust the size automatically. Provides support for stacks, queues, and hash tables.
Generic: A class, structure, interface, and method with a placeholder (that is, a parameter type. It is closely related to the set, and generic sets can obtain the direct advantages of type security. That is to say, the type is fixed.
Below we will use this example to understand the above concepts:
Code for defining classes and interfaces:
Abstract class animal // define an abstract class {protected string name = ""; Public animal (string name) {This. name = Name;} public animal () {This. name = "";} protected int shoutnum = 6; // call Count public int shoutnum {get {return shoutnum;} set {shoutnum = value ;}} public String shout () // code for calling {string result = ""; for (INT I = 0; I <shoutnum; I ++) Result ++ = getshoutsound () + ","; // call the getshoutsound virtual method return "my name is" + name + "" + result;} protected virtual string getshoutsound () // modifier virtual indicates that this method is a virtual method. You can override {return "" ;}/// define different subclass to implement the parent class, in fact, it is a good embodiment of polymorphism. Class Cat: Animal // sub-cat {public CAT (): Base () {} public CAT (string name): Base (name) {} protected override string getshoutsound () {return "" ;}} class Dog: Animal // subclass dog {public dog (): Base () {} public dog (string name): Base (name) {} protected override string getshoutsound () {return "" ;}} class cattle: animal // subclass {public cattle (): Base () {} public cattle (string name): Base (name) {} protected override string getshoutsound () {return "failed" ;}} class sheep: Animal // sub-sheep {public sheep (): Base () {} public sheep (string name): Base (name) {} protected override string getshoutsound () {return "" ;}} interface ichange // defines an interface {string changething (string thing);} class machinecat: cat, ichange // defines a robot cat, inherits the parent cat, and uses the interface ichange {public machinecat (): Base () {} public machinecat (string name): Base (name) {} Public String changething (string thing) {return base. shout () + "I have a versatile pocket, and I can change it out:" + thing ;}}
The following is the client code:
// Private animal [] arrayanimal; // declare an animal array // use ilist for a set <animal> arrayanimal; // declare a generic set variable using the interface ilist, note: ilist <animal> indicates that the Set variable can only accept the private void button1_click (Object sender, eventargs E) of the animal type // The Cat call button event) {Cat cat = new CAT ("Mimi"); // class instantiation cat. shoutnum = 7; MessageBox. show (cat. shout ();} // Private void button2_click (Object sender, eventargs e) {dog = new dog ("wangcai "); // here is just a simple call method to achieve the goal, reflecting the encapsulated Feature dog. shoutnum = 6; MessageBox. show (dog. shout ();} // The Animal Registration button private void button3_click (Object sender, eventargs e) // The Animal Registration button, {// arrayanimal = new animal [5]; // you can register up to five animal array objects for instantiation: arrayanimal = new list <animal> (); // instantiate the list object arrayanimal. add (new CAT ("Xiaohua"); // register for the cat and dog arrayanimal. add (new dog ("Tom"); arrayanimal. add (new dog (""); arrayanimal. add (new CAT ("Jiaojiao"); arrayanimal. add (new CAT ("Mimi ")); MessageBox. Show (arrayanimal. Count. tostring (); // print the number of added arrayanimal. removeat (1); // You can also delete the added ones here.} // Let all registered animals perform the call competition private void button4_click (Object sender, eventargs e) {foreach (animal item in arrayanimal) // traverse the entire array, let them implement shout method {MessageBox. show (item. shout () ;}}// Event code private void button5_click (Object sender, eventargs e) {machinecat MCAT = new machinecat (" "); // instantiate the machine cat machinecat mcat1 = new machinecat (" 2"); ichange [] array = new ichange [2]; // declare an interface array, assign two instances to array [0] = MCAT; array [1] = mcat1; MessageBox. Show (array [0]. changething ("various things! "); // Use polymorphism to implement different changething MessageBox. Show (array [1]. changething (" a lot of things! "));}
This learning is also very basic, learning the basis of the design model!