Class in C,

Source: Internet
Author: User

Class in C,

1. What is a class?

The most basic object-oriented language is class. In C #, classes are defined as follows: classes represent a group of objects with common attributes and behaviors.

For example, in real life, a person is a "class", but this is just a general term, referring to all people. We need to find a specific person to play. For example, XiaoHong and I are two instances of the "person" class.

2. How to define a class in C?

Class is defined in C.

Class Name {}

You can also modify the class with an access modifier.

Access Modifier Description
Public Public access. Not limited.
Private Private access. Only access by members of this category is allowed.
Protected Protect access. The instance cannot be accessed only by the class or subclass.
Internal Internal access. It is restricted to access within the project, and others cannot be accessed.
Protected internal Access is protected internally. Only access to this project or subclass is allowed.

 

 

 

 

 

 

3. Class Members

Class Members include fields, attributes, methods, and constructors. Like classes, they all have their own access permissions. You can also use the static keyword to declare it as a static member of the class. Note that the static member belongs to the class level concept and does not belong to the class instance.

Field 3.1

A field consists of an access modifier, a field type, and a field name.

Public class Person

{

Private string name;

Public int age;

Protected bool sex;

}

Of course, you can also use readonly and const to modify it. readonly can be initialized in the constructor without definition, and the const modifier field must also be initialized when the field is defined.

Class can be modified with static, and the field can also use static. When a field is modified using static, only class name and field name can be used for access.

 

3.2 attributes

Attribute is an extension of the field

Public class Person

{

Private string name;

Public string Name

{

Get {return name ;}

Set {name = value ;}

}

}

Attribute definitions are mainly composed of get accessors and set accessors. As mentioned earlier, get and set in properties are compiled into two methods in the IL code. Attribute is used to wrap fields because it can better protect fields and add more logic control code as needed. Of course, attributes can also be declared using the static keyword, which is the same as the field and belongs to the class level. It cannot be accessed through the instance of the class or use non-static fields in static attributes.

3.3 Method

Method = method signature + code of a series of statements

Public class Person

{

Public void Print (string name)

{

Console. WriteLine ("aaaa ");

}

}

The static method can also be modified using the static keyword. The static method also belongs to the class level and cannot be accessed using a class instance.

Another important thing about methods is method overloading. Method overloading means that multiple methods with the same name but different method signatures can be defined in the class, different method signatures here refer to different parameter sequences, parameter types, and numbers of methods (Warning: the return value type of the method is not part of the method signature ).

Public class Person

{

Public void Print (string name ){}

Public void Print (int age ){}

}

 

3.4 Constructor

Constructor is used to create instance objects of a class. When a constructor is called to create an object, the constructor allocates memory space for the object and initializes the class members. Constructor can be divided into two types: instance constructor and static constructor.

(1) instance Constructor

Generally, the new keyword is used in programs to create objects. The new creation process is to call the instance constructor to initialize all instance members in the class.

For example, although the above Person class does not show that the instance constructor is written, the C # compiler will automatically generate a default lunch instance constructor with the function body empty.

Constructor has the following features:

  • Constructors can carry out method overloading.
  • If a constructor is not defined for the class display, the C # compiler will generate an instance constructor for the default lunch with the function body empty.
  • You can specify the access level for the instance constructor, that is, you can use the public, protected, and private modifiers to modify it.
  • The constructor must have the same name as the class and cannot have a return type.

(2) Static Constructor

Static constructor is used to initialize static members in the class. The CLR automatically calls the static constructor before creating the first instance or referencing any static members.

Static constructors have the following features:

  • Static constructors cannot use any access modifiers.
  • A static constructor cannot contain any parameters.
  • The static constructor is executed only once.
  • Static constructor cannot be called directly.
  • In programs, programmers cannot control the timing of static constructors.

3.5 destructor

The Destructor is used to release the managed and unmanaged resources used by the class instance before the class is destroyed. The Destructor actually implicitly calls the Finalize method of the base class Object.

Class Person

{

~ Person ()

{

Console. WriteLine ("destructor called ");

}

}

The above destructor code will be converted into the following code by the compiler:

Protected override void Finalize ()

{

Try {Console. WriteLine ("destructor called ");}

Finally {base. Finalize ();}

}

Pay attention to the following points when defining destructor:

  • The Destructor cannot be defined in the struct, but can only be used for classes.
  • A class can have only one destructor.
  • The Destructor cannot be inherited or overloaded.
  • You cannot call the Destructor explicitly. The Destructor is automatically called by the garbage collector.
  • The Destructor has no modifiers or parameters.

 

3.6 Indexer

The definition of the indexer is similar to an attribute and also has get accessors and set accessors.

[Modifier] data type this [index type index]

{

Get {// return an element of the array in the class ;}

Set {// assign values to array elements in the class ;}

}

This indicates that the operation is an array member of the class object.

4. Class instantiation

As mentioned above, to get an instance object of a class, you must declare a variable of the class type first, and then use the new operator followed by the instance constructor of the class to complete instantiation. Class instantiation objects are specific to classes.

Note that only classes that contain instance constructors can be instantiated, but instance constructors cannot be defined in static classes.

 

5. Differences between classes and struct

  • The syntax difference is that the key class should be used for the definition class, while the struct uses the key struct.
  • Fields declared in the struct cannot be initialized, but the class can.
  • If no constructor is defined for flavor classes, the C # compiler automatically generates an instance constructor without parameters. Struct, whether or not it explicitly defines the constructor, the implicit constructor always exists.
  • Struct cannot display and define constructors without parameters.
  • In the struct constructor, you must assign values to all fields in the struct.
  • Creating a struct object does not apply the new keyword, but the fields in the struct object do not have an initial value. Classes must use the new keyword to create objects.
  • Struct cannot inherit structures or classes, but can implement interfaces. classes can inherit classes but cannot inherit structures. They can also implement interfaces.
  • A class is a reference type, while a struct is a value type.
  • Struct cannot define destructor, but classes can have destructor.
  • The abstract and sealed keywords cannot be used to modify the struct, but the class can.

 

I feel that this article is very basic. I am also thinking about whether or not to write it. I decided to write it down later. Ten thousand miles from the ground up, coupled with rapid technological updates, but with the foundation, you can remain unchanged.

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.