C # basic class knowledge

Source: Internet
Author: User

Variable Scope

Variable name: the variable name is like the container number in the lab. {} Is equivalent to a lab.

Variable definition:

Only unique variables can be defined in the {} statement block with nested relationships.

The {} statement block without nested relationships can define variables with the same name.

Valid range of variables:

The variable is valid in its declared {} and its nested sub-block;

Tips for using variables:

Variables should be declared with the latest position as much as possible. Minimize the effective range.

Logically speaking, for (INT I = 0; I <5; I ++) {} If () {} else {} while () {} is a whole

Equivalent to {for (INT I = 0; I <5; I ++) {}}{ if () {} else {}}{ while (){}}

Basic Concepts

Class: abstraction with the same attributes and behavior objects

Attribute Value: In object-oriented programming, a field is used as a variable to save the attribute value. When a field is declared, the value can be left blank, with a default value.

Properties: Special "behaviors" are used to display the properties of objects. A pair of get/set behaviors are used to display the properties of objects. GET/set is called the "accessors" of the attribute ",

Method: The method is the action that the class can execute. Members defined in the function must be assigned a value before use.

Three object-oriented features: encapsulation, inheritance, and polymorphism.

Class.

This indicates the "current instance/current object", that is, the object that is operating on the property accessors. This indicates the object.

Access permission/access permission

Controls where class or class members can be accessed, so as to "encapsulate" the object.

The default class member is private.

Public public has no level limit on access members

Private can only be accessed within the class

Protected can only be accessed by the class and its derived classes, regardless of whether the derived class and the base class are in the same assembly

Internal internal access is limited to the concentration

The default class is internal.

Public public has no level limit on access members

Internal internal access is limited to the concentration

 

 

Person p1 = new person ();

P1.age = 20; // The age field value of the object pointed to by P1.

 

Person P2 = p1; // P2 points to the object Currently pointed to by P1

P2.age = 99;

 

Attribute usage: uppercase letters at the beginning of a property and lowercase letters at the beginning of a field

The reason why the field is set to private. Prevents unauthorized field reading or modification.

Private int age;

// When reading or modifying an operation, you can make sure that you do not need to restrict the operation.

Public int age {Get; set ;}

// When performing read or modify operations, you must restrict read or modify operations.

Public int age {get {return age;} set {age = value ;}}

Decompilation:

Public void set_age (INT value ){

This. Age = value;

}

Public int get_age (){

Return this. Age;

}

Note: In non-shorthand cases, you can only use set or get to define read-only or write-only attributes.

In short: set and get must both exist.

Constructor:

Initialize the object in the constructor. Is a special function used to create objects. The function name is the same as the class name, and there is no return value, even void is not required.

The constructor can have parameters. When a new object is created, the function parameters can be passed. It can be overloaded, that is, there are multiple constructors with different parameters.

If no constructor is specified, the class has a default no-argument constructor. If a constructor is specified, no default constructor is available. If no constructor is required, you need to write the constructor by yourself.

Calling sequence of Constructors (Parent and Child)

Constant

The constants defined in the class remain unchanged for any instance of the class. There is no need to call it through an instance of the class.

Class program {public const double Pi = 3.15 ;}

Reference: reference outside the class in the form of program. Pi; Class Name. Constant name

Const constant. All letters of a constant name are in uppercase. A value that never changes can be declared as a constant.

Static Member

It can also be understood as a global member. For example, Chinese humans have names and languages. Name has different values for different people. Language is only related to Chinese humans. Because everyone is Chinese.

External Reference in class: Class Name. variable name class name. Method Name ()

Int A = person. Age; person. setname ();

Class person {

// Static members are not associated with any instances of the class. This is the only part of the class. Global.

Public string name = "Faye Wong ";

Public static int age = 15;

// Non-static variables and methods cannot be called in static methods within the class.

Public static void setname (){

// String S = Name;

// Setage ();

}

// Static variables and methods can be called in non-static methods within the class.

Public void setage (){

Int I = age;

Setname ();

}

}

 

 

C # basic class knowledge

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.