Object-oriented fundamentals (inheritance classes, abstract methods, interfaces, delegates, and events)

Source: Internet
Author: User

1. The page implements the cat barks, the dog barks, the tiger barks

<asp:button id= "Cat" runat= "server" text= "cat called" onclick= "Cat_click"/><br/>
<asp:button id= "Dog" runat= "server" text= "dogs called" onclick= "Dog_click"/><br/>
<asp:button id= "Tiger" runat= "server" text= "Tiger" onclick= "Tiger_click"/><br/>
<asp:button id= "Shout" runat= "Server" text= "Alternate call" onclick= "Shout_click"/><br/>
<asp:button id= "Change" runat= "Server" text= "Implements Interface" onclick= "Change_click"/>

2. Implement cat Barking first, without any design pattern, define a cat class (CCAT)

No object-oriented design

public class Ccat    {public        string Name = string. empty;//Common        Private int _numb=1;//Private public        ccat (string name)//constructor        {this            . name = name;        }        Public Ccat ()//2 a function overload, the number of arguments, or a different parameter type can overload        {this            . Name = "Nameless";        }        public int Numb//called several times        {            get {return _numb;} Readable            set {_numb = value<=3? Value:3;}//Assignable        } public        string Shout ()       {           string result = ""; 
   for (int i = 0; i < _numb; i++)           {               result + = "Meow! ";           }           Return "My Name" + name +result;        }    }

Use this method on the page

protected void Cat_click (object sender, EventArgs e)        {            ccat cat=new ccat ("Kitten");            Cat. Numb = 5;            Response.Write (Cat. Shout ());        }

3. The use of inheritance, cats and dogs and tigers are all animals, so define an animal, Animal, cat and dog Tigers have a constructor and call this method, so the definition of animal

Abstract class Inheritance

Abstract class Animal//abstract is a method of abstraction, Anmial is abstract class, abstract class can not be instantiated, to quilt class rewrite, otherwise meaningless    {        protected string Name = ""; Protected can be used for quilts, other classes cannot use        protected Animal ()        {this            . Name = "Nameless";        }        Protected Animal (string name)        {this            . name = name;        }        protected int _numb = 1;        public int Numb         {            get {return _numb;}            set {_numb = value;}        }        Public virtual string Shout ()//Build virtual method virtual        //{        //    return "";        }        #region Refactoring puts the shout method into the parent class, Getshout () The virtual write method, just for getting who the public        string Shout ()//Added the override to represent method overrides        {            string result = "";            for (int i = 0; i < _numb; i++)            {                result + = Getshout () + ",";            }            Return "My Name" + name + result;        }        Public abstract string Getshout ();//abstract method        #endregion    }

  

Class Cdog:animal//Inherit the parent cat and the dog are all animals the    {public        Cdog ()//base () keyword represents the parent class        {                    } public        Cdog (string name): Base (name)        {        } public        override string Getshout ()//Added override means override        {            return ' Wang! ";        }
}

  

  protected void Dog_click (object sender, EventArgs e)        {            Cdog dog = new Cdog ("Puppy");            Dog. Numb = 5;            Response.Write (dog. Shout ());        }

  

3. Implement interface Cat and dog Tigers are all running on the ground (a class can have multiple interfaces but cannot inherit multiple base classes)

Interface

Interface ichange//interface naming begins with I, cannot have constructors, cannot have modifiers, cannot virtual static        //implemented interface classes must inherit methods and properties of all interfaces    {        string changegthing ( string thing);//interface method returns a string type    }

  

Class Cdog:animal,ichange//Inherit the parent cat and the dog are all animals Implement interface can run    {public        Cdog ()//base () keyword represents the parent class        {                    } public        Cdog ( String name): Base (name)        {        } public        override string Getshout ()//Added override means override        {            return "Wang!" ";        }        public string changegthing (String thing)        {            return base. Shout () +thing;}        }

The advantages of generics, the process of packing the unboxing less than the collection, the possibility that the collection has failed to convert

protected void Change_click (object sender, EventArgs e)        {                      ilist<ichange> listanimal=new list< Ichange> ();//Generic            Listanimal.add (new Cdog ("Puppy"));            Listanimal.add (New Ctiger ("Xiao Hu"));            Response.Write (Listanimal[0]. Changegthing ("I am Running");//Use polymorphism to achieve different changegthing            Response.Write (listanimal[1]. Changegthing ("I am also running");        }

4. When the cat cries, I come, the mouse 1 and the mouse 2 respectively called the Cat to run, and to achieve a running method

Delegates and events

Class mouse//defines a mouse class    {        private string name;        Public Mouse (string name)        {            this.name = name;        }        public void Run ()        {            Console.WriteLine ("The old cat has come run {0}", name);}    }
Class cat//defines a cat class    {        private string Name;        Public Cat (string name)        {this            . name = name;        }        public delegate void Catshouteventhandler ();//Declaration Delegate (delegate is the encapsulation of the function, delegate) public        event Catshouteventhandler catshout;//Declaration Event event is a special delegate, event public        void Shout ()        {            Console.WriteLine ("Meow, I am {0}", Name);             if (catshout! = null)            {                catshout ();//If Catshout has an object registration event then executes catshout, registers the mouse run method in the event, the cat calls, executes the mouse's Run Method            }          }    }

 

static void Main (string[] args)        {            cat cat=new cat ("cat");            Mouse mouse1=new Mouse ("Jukery");            Mouse mouser2=new Mouse ("Kshds");            Cat. Catshout + = new Cat.catshouteventhandler (mouse1. Run);            Cat. Catshout + = new Cat.catshouteventhandler (mouser2. Run),//Register the method in the event (event is the Special Delegate delegate)            Cat. Shout ();//When the Shout method is the cat teaches, notify two mice, if the event is not empty, execute the method in the delegate            Console.read ();        }

  

 

  

 

 

 

  

Object-oriented fundamentals (inheritance classes, abstract methods, interfaces, delegates, and events)

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.