Polymorphic: means that different objects can perform the same actions, but are executed by their own implementation code. The parent class is required to declare the member as virtual, and the method override (override) is used.
Abstract class: Declare abstract classes with abstract, note that abstract classes cannot be instantiated, and abstract methods must be overridden by subclasses.
Interface: is a collection of implicit public methods and properties that encapsulate a particular function, and once the class implements an interface, it can support all the properties that are specified by the interface. A class can support multiple interfaces, and multiple classes can also support the same interface.
Collection: Is better than an array, and the. NET Framework provides a dedicated class for data storage and retrieval. The collection automatically increases as we add elements to the inside, automatically adjusting the size. Provides support for stacks, queues, and hash tables.
Generics: Classes, structs, interfaces, and methods that have placeholders (that is, parameter types). In close contact with collections, generic collections can gain direct benefits of type safety. This means that the type is fixed.
Define the code for the class and interface:
<span style="font-size:18px;">Abstract classAnimal//an abstract class is defined using the keyword abstract { protected stringName =""; PublicAnimal (stringname) { This. Name =name; } PublicAnimal () { This. Name ="Nameless"; } protected intShoutnum =6;//number of calls Public intShoutnum {Get { returnShoutnum; } Set{shoutnum=value; } } Public stringShout ()//the code that calls out { stringresult =""; for(inti =0; i < Shoutnum; i++) Result+ = Getshoutsound () +", ";//The virtual method of Getshoutsound was called return "My name is ""+ name +""+result; } protected Virtual stringGetshoutsound ()//modifier virtual means that this method is a virtual method and can be overridden by a quilt class { return ""; } } //defining different subclasses to implement the parent class is actually a very good representation of polymorphism. classCat:animal//Sub-category cat { PublicCat ():Base() {} PublicCat (stringName):Base(name) {}protected Override stringGetshoutsound () {return"Meow"; } } classDog:animal//sub-class dog { PublicDog ():Base() { } PublicDog (stringname):Base(name) {}protected Override stringGetshoutsound () {return "Meow"; } } classCattle:animal//sub-type cattle { Publiccattle ():Base() { } PublicCattle (stringname):Base(name) {}protected Override stringGetshoutsound () {return "Moo"; } } classSheep:animal//sub-class sheep { PublicSheep ():Base() {} PublicSheepstringname):Base(name) {}protected Override stringGetshoutsound () {return "Baa"; } } InterfaceIchange//defines an interface { stringChangething (stringthing); } classMachinecat:cat, Ichange//defines a machine cat, inherits the parent cat, and uses the interface Ichange { PublicMachinecat ():Base() { } PublicMachinecat (stringname):Base(name) {} Public stringChangething (stringthing) { return Base. Shout () +"I have a universal pocket, I can make out:"+thing; } }</span>
Below is the code for the client:
<span style="font-size:18px;">//private animal[] arrayanimal;//declare an array of animals//Use of collectionsIlist<animal> Arrayanimal;//declares a generic collection variable, using the interface IList, noting that ilist<animal> indicates that this set variable can only accept Animal types//Cat-called button events Private voidButton1_Click (Objectsender, EventArgs e) {Cat Cat=NewCat ("Mimi");//instantiation of a classCat. Shoutnum =7; MessageBox.Show (Cat.shout ()); } //Dog-called button events Private voidButton2_Click (Objectsender, EventArgs e) {Dog Dog=NewDog ("Wong Choy");//This is just a simple call method that can be used to achieve the purpose, embodying the characteristics of encapsulationDog. Shoutnum =6; MessageBox.Show (Dog.shout ()); } //button for animal registration Private voidButton3_Click (Objectsender, EventArgs e)//Animal Registration button, { //arrayanimal = new Animal[5];//Instancing can enroll up to 5 animal array objectsArrayanimal =NewList<animal> ();//instantiating a list objectArrayanimal.add (NewCat ("Small Flower"));//Sign up for cats and dogs, respectively .Arrayanimal.add (NewDog ("Mao")); Arrayanimal.add (NewDog ("Little Black")); Arrayanimal.add (NewCat ("Jiao Jiao")); Arrayanimal.add (NewCat ("Mimi")); MessageBox.Show (ArrayAnimal.Count.ToString ());//Print out the number we addedArrayanimal.removeat (1);//Here you can also delete the more we added. } //let all the animals in the application call the game Private voidButton4_Click (Objectsender, EventArgs e) { foreach(Animal IteminchArrayanimal)//traverse the entire array, and let them implement the Shout method at a time{MessageBox.Show (Item.shout ()); } } //jingle Cat gets something out of the event code Private voidButton5_click (Objectsender, EventArgs e) {Machinecat MCAT=NewMachinecat ("Jingle");//instantiation of the Machine catMachinecat MCAT1 =NewMachinecat ("Jingle 2"); Ichange[] Array=Newichange[2];//declares an array of interfaces and assigns two instances to the arrayarray[0] =MCAT; Array [1]=MCAT1; MessageBox.Show (array[0].changething ("all kinds of things! "));//use polymorphism to achieve different changethingMessageBox. Show (array[1].changething ("So many things, huh! ")); }</span>
About C # Object-oriented 2