C # Object-Oriented Programming encapsulation inheritance polymorphism,
C #Object-Oriented Programming
What Is Object-Oriented?
Object-Oriented programming is a new programming idea proposed after Object-Oriented Programming in 1960s.
Encapsulation, inheritance, and Polymorphism
Encapsulation, inheritance, and polymorphism are the core of object-oriented programming:
EncapsulationIt is the first step to implement object-oriented programming. encapsulation is to aggregate data or functions in units (called classes ). Encapsulated objects are usually called abstract data types.
Meaning: protect data from destruction (for example, a TV can be used but cannot change the internal circuit at Will)
Keywords: private, public, protected, internal
Public string name;
Inheritance:Inheritance mainly implements code reuse, saving development time. In inheritance, the constructor runs in the order from top to bottom, while the Destructor goes down to the top. The Inheritance statements are as follows:
Public class name: class name
Bytes --------------------------------------------------------------------------------------------------------------------
I have to mention the virtual method when learning polymorphism.
Virtual method: the virtual method declared is the virtual method defined in the base class. You can use override to override the virtual method in the subclass.
The process of rewriting is polymorphism.
Virtual public void show() // Define Virtual Methods
Override public void show() // Method Rewriting
BaseKeywords
BaseKeywords:The base keyword is used to access base class members from a derived class. The main form is as follows:
Call methods that have been overwritten by other methods on the base class
Specify the base class constructor to be called when creating a derived class instance
(The base keyword cannot be used in static methods)
Here I wrote a demonstration of a case.
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Inheritance of namespace code
{
Class Goods // defines a base class
{
Public string TradeCode {get; set;} // defines the product ID.
Public string FullName {get; set;} // define the product name
}
Class JHinfo: Goods
{
// Define the purchase number
Public string JHID {get; set ;}
Public void showinfo () // output the purchase number
{
Console. WriteLine ("purchase no.: {0} \ n product No.: {1} \ n Product Name: {2} \ n", JHID, TradeCode, FullName );
}
}
}
Class Program
{
Static void Main (string [] args)
{
JHinfo metadata = new JHinfo (); // instantiate an object
Forbidden. TradeCode = "Ares 1000 ";
Notebook. FullName = "laptop ";
Forbidden. JHID = "ZS10000 ";
Response. showinfo ();
Console. WriteLine ();
Console. ReadKey ();
}
}
}