About C # Object-oriented 1

Source: Internet
Author: User

Class: Is a collection of objects that have the same properties and functionality.

Constructor Method: Initializes the class. If you do not encode, the system will generate an empty construction method by default.

Method Overloading: Provides the ability to create multiple methods with the same name, but these methods require different parameter types. He can add new features without changing the original method.

Properties: Suitable for use in fields where method calls are used, where the field is the data that the storage class needs to meet its design.

Encapsulation: Each object contains all the information he can do. Encapsulation can reduce coupling, the interior of the class can be modified, and the class can have a clear external interface.

Inheritance: If the subclass inherits the parent class, the first child class has properties and functions that are not private to the parent class, and second, the subclass has its own properties and capabilities, that is, subclasses can extend the properties and functionality that the parent class does not have, and can implement the functionality of the parent class in its own way (method overrides).

<span style="font-size:18px;">namespaceWindowsFormsApplication3 { Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }            Private voidButton1_Click (Objectsender, EventArgs e)//Click the Cat Called Button event method{cat Cat=NewCat ("Mimi");//instantiation of a classCat. SHOUTNUM1 =7;          MessageBox.Show (Cat.shout ()); }            Private voidButton2_Click (Objectsender, EventArgs e)//Click the dog-called Button's event method{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. SHOUTNUM1 =6;          MessageBox.Show (Dog.shout ()); }                         }      classAnimal//declaring a parent class           {          protected stringName ="";//the private string variable that declares the class name                  PublicAnimal (stringName//defines the construction method for the Cat class, with the argument entering a string        {               This. name = name;//Assigning a parameter to a private variable        }             PublicAnimal ()//to construct a method overload        {                 This. Name ="Nameless"; }            protected intSHOUTNUM1 =3;//declares an internal character segment, the default value of 3,         Public intShoutNum1//setting of the property        {              Get//get represents the value of an outside call property            {                  returnShoutNum1; }              Set//set indicates that the outside world can assign values to a property{shoutNum1=value; }          }        }            classCat:animal//The subclass cat, using the inheritance mechanism, inherits the animal parent class        {               PublicCat ():Base()              { }               PublicCat (stringname):Base(name) {} Public stringshout () {stringresult ="";  for(inti =0; i < SHOUTNUM1; i++) {result+="Meow"; }                  return "My name is ""+ name +" "+result; }                    }          classDog:animal//Subclass Dog, inheriting parent class animal        {               PublicDog ():Base()              { }               PublicDog (stringname):Base(name) {} Public stringshout () {stringresult ="";  for(inti =0; i < SHOUTNUM1; i++) {result+="Wang"; }                  return "My name is ""+ name +" "+result; }            }               }</span>

About C # Object-oriented 1

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.