"Learning notes" C # encapsulation and inheritance

Source: Internet
Author: User

  • Packaging
    • Encapsulation is the first step in implementing object-oriented programming
    • Encapsulation is the collection of data, methods, etc. in a unit, which we call class
    • The meaning of encapsulation is to protect code/data, shielding complexity
  • Inherited
    • Inheritance is an integral part of all object-oriented languages
    • Inheritance is to implement the reuse and extension of classes
    • The inherited class is called the base class, or the parent class. Inheriting from the base class is called a derived class, or subclass.
    • In C #, only single inheritance is supported, and subclasses can have only one parent class
    • The parent class has attributes that the child class has, and inheritance can pass
  • Example
  • 1 usingSystem;2 namespaceObjectfeaturedemo3 {4      Public classRefrigerator5     {6         //encapsulation is the first step in object-oriented programming7         //encapsulate the method, create a class8          Public voidOpen ()9         {TenConsole.WriteLine ("Open"); One         } A          Public voidClose () -         { -Console.WriteLine ("Close"); the         } -  -     } -  +     //If we create a class that does not specify a parent class, the default is inherited from Object -      Public class Person +     { A          Public stringname; at          Public intAge ; -  -          Public voidSay (stringstr) -         { - Console.WriteLine (str); -         } in     } -     //Inheritance-A class can inherit from another class to     //the inherited class is called the parent class, or the base class +     //the inherited class is called a subclass, or a derived class -  the     //inheritance can be extended for functionality and reuse *     //C # only supports single continuation, and a subclass can have only one parent class $      Public classStudent:personPanax Notoginseng     { -          Public intNum//School Number the          Public voidGotoclass () +         { AConsole.WriteLine ("go to class"); the         } +     } -     class Program $     { $         Static voidMain (string[] args) -         { -Refrigerator R =Newrefrigerator (); the             //encapsulation to protect code, data security - R.open ();Wuyi             //encapsulation can mask complexity the  -  Wu             //Inheritance -Person p =NewPerson (); AboutP.name ="Li"; $P.age = -; -P.say ("I am ok!"); -  -Student s =NewStudent (); AS.name ="LiL"; +S.age = A; theS.num =100001; - S.gotoclass (); $         } the     } the}

"Learning notes" C # encapsulation and inheritance

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.