- 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