C # Object-Oriented Programming encapsulation inheritance polymorphism,

Source: Internet
Author: User

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 ();

 

 

}

}

}

 

 

 

Related Article

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.