C # Object-oriented basics-classes and objects

Source: Internet
Author: User

1. Classes and Objects

A class is a basic unit of object-oriented programming; A variable called an object.

A class contains two kinds of members: fields and Methods .

A field is a variable , and a method is a function .

Object-oriented thinking: teaches us how to use the rules of the class to write code rationally.

2. Fields of the class

The field represents the data in the class. You can add public, private, and protected to represent access to a field before the variable.

3. Methods of Class

(1), the concept of function

An expression:

Return value type method name (parameter list)

{

Statement 1;

Statement 2;

.....

return expression;

}

The function needs to return a value to the outside world, implemented by the return statement.

If a function has no return value or does not care about its return value, the return value is defined as void.

(2) definition and use of methods

Functions placed in a class (typically attaching an access modifier such as public and private) are called methods.

The most basic way to access a method is through the object created by the class , and you can access This method by creating an object of the class by using the new keyword .

(3), method overloading

Two methods with the same range (in the same class, or in a parent-child class) with the same name but different parameter types or different number of parameters form an overloaded relationship with each other.

The two functions that make up an overloaded relationship must satisfy:
The function name is the same.
The parameter types are different, or the number of parameters is different . (* The difference of the function return value type is not the judging condition of the function overload )
These two functions of the same name make up an "overloaded relationship" with each other.

Output:

Output (with parameters):

4. Static members of a class

A function in a class is called an instance method of a class if it is declared without the "static" keyword .

A method that adds a "static" keyword is called a " static method " of a class.

Fields with the "static" keyword are called " static fields " of the field.

Static members are not initialized with the new object and go directly with the class name.


Static members: Without initialization of objects, all object shares are called directly through the class name

(1), the basic method of accessing static members of a class

class name. static method Name (parameter list)

(2), class static member characteristics

A static member of a class is shared by all objects of the class.

Functions can also be used with static members

(3) access rules for class instance members and static members


instance methods in the same class can be called directly to each other.
The fields of the class (including instance fields and static fields) can be accessed directly by all instance methods in the same class,
A static method in a class can only access the class static field directly; To access an instance method, create an object in a static method.

5. Properties of the class

property is a special "field"

A property consists of two special read accessors and write accessors.
when the property is read , the read accessor is called, simply returning the value of the private field to the outside world.
when the property is set , the write accessor does not invoke, first check that the external value passed in is not an empty string, and then save the incoming value in the private field.

There is a special variable in the read accessor that value must be paid special attention to, which represents the value passed in by the outside world.

Ways to write properties:
(1), Design a private field to hold the data for the property.
(2), design get read accessor and set write accessor to access private field data.

6. In-depth understanding of classes and objects

(1) differences between classes and objects

Objects are created with a class template. A one-to-many relationship between a class and an object.

In C #, create an object using the New keyword.

"Active" In a program is an object, not a class.

---The two concepts of "object" and "instance of class" are identical

(2), class constructor (typically used to initialize a private data field of a class)

When an object is created with the New keyword, a special function is called automatically, which is the constructor of the class.

In C #, the constructor of a class is the same as the class name, and there is no return value.

all of the bands () are functions

Constructors each class has at least one constructor, with the same name as the class name, and can be overloaded.

All objects are constructed by calling the constructor first.

Typically used to assign an initial value to a member

For data transfer across classes

Constructors with parameters

(3), reference type and value type

Difference:

A variable of a value type is immediately available after it is defined.

After a variable definition of a reference type, you must also create the object with the New keyword before you can use it.

All value Types in C # are implicitly derived from System.ValueType:

    • struct: struct (directly derived from System.ValueType);
      • Numeric type:
        • Integer: sbyte (alias of System.SByte), short (system.int16), int (System.Int32), long (System.Int64), Byte (System.Byte), ushort (system.uint16), uint (System.UInt32), ULONG (System.UInt64), char (System.Char);
        • Float type: float (system.single), double (system.double);
        • High-precision decimal type for financial calculations: decimal (System.Decimal).
      • BOOL Type: bool (alias of System.Boolean);
      • User-defined struct (derived from System.ValueType).
    • Enum: Enum (derived from System.Enum);
    • Nullable type (derived from system.nullable<t> generic struct, T? is actually an alias of system.nullable<t>).

Value type, the value type instance is typically assigned on the thread's stack (stack) and does not contain any pointers to the instance data, because the variable itself contains its instance data

reference Types in C #:

    • Array (derived from System.Array)
    • The following types are defined by the user:
      • Classes: Class (derived from System.Object);
      • Interface: Interface (interface is not a "thing", so there is no question of where to derive it.) Anders in the C # programming Language, the interface simply represents a convention [contract]);
      • Delegate: Delegate (derived from System.Delegate).
    • Object (alias of System.Object);
    • String: String (alias of System.String).

C # Object-oriented basics-classes and objects

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.