Implement the OOP concept in C #

Source: Internet
Author: User

1. Objects and classes in C #

Class is the description of a group of objects with the same attributes and behaviors.

Coding Convention:

Use the Pascal naming method to Name public member variables, protected member variables, or internal member variables, such as Score, Name, and Staus.

To name private member variables, use the camel naming method and start with an underscore, such as _ age, _ length, and _ radius.

class Employee{  private string _name;  private char _gender;  private string _qualification;  private uint _salary;}

2. Access Modifier

Public can be accessed by members of the class or non-members of the class.

Internal can be accessed by the current Assembly

Protected can be accessed by the class or the type of the derived class

Private is accessible only to members of the class.

If no access modifier is specified for the class, the default access modifier of the class is internal, but the default access modifier of the class members is private.

3. constructor and destructor

C # provides a structure named constructor to automatically initialize member variables, but constructor is a special method in the class. This method is called every time you create an instance of the class. The constructor has the same name as the class and does not return any value.

Using System; namespace BaseConsole {class Employee {private string _ name; private char _ gender; private string _ qualification; private uint _ salary = 0; // default constructor private Employee () {_ qualification = "";} // parameterized constructor private Employee (string strQualification, string strName, char gender, uint empSalary) {_ qualification = strQualification; _ name = strName; _ gender = gender; _ salary = empSalary;} [STAThread] static void Main (string [] args) {Employee objEmployee = new Employee (); // call the parameterized constructor: Employee objMBA = new Employee ("MBA", "tom", 'M', 4000); Console. writeLine ("qualification =" + objEmployee. _ qualification); Console. writeLine ("salary =" + objEmployee. _ salary); Console. writeLine ("qualification =" + objMBA. _ qualification); Console. readKey ();}}}

Destructor

~ Employee () {// implements the Destructor}

The Destructor does not accept any parameters or any access modifier. The main body of the Destructor includes some code, which is usually used to close the database, file, or network connection opened by the instance.
A class can have only one destructor.

The Destructor cannot be overloaded.

The Destructor cannot be explicitly or manually called and can only be automatically called by the garbage collector.

4. Method Overloading

Multiple methods share one name but perform similar functions on different data. This concept is called method overload.

Using System; namespace BaseConsole {class OverloadParameters {int greatest (int num1, int num2) {Console. the larger numbers in WriteLine ("{0} and {1} are:", num1, num2); if (num1> num2) {return num1 ;} else {return num2 ;}} int greatest (int num1, int num2, int num3) {Console. the largest numbers in WriteLine ("{0}, {1}, and {2} are:", num1, num2, num3); if (num1> num2 & num1> num3) {return num1;} else if (num2> num1 & num2> num3) {return num2 ;}else {return num3 ;}} [STAThread] static void Main (string [] args) {OverloadParameters obj = new OverloadParameters (); Console. writeLine (obj. greatest (22, 88); Console. writeLine (obj. greatest (300,200,100); Console. readKey ();}}}

 

 

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.