Chapter 3 C # classes and objects 2. C # class definition (class)

This article is transferred from: http://m.biancheng.net/view/2806.html

 Any project created in the  C # language has the existence of classes, which can well reflect the characteristics of encapsulation, inheritance, and polymorphism in the object-oriented language.

This section will explain the classes in C # and how to define classes.

The class has been used many times in the previous study. The syntax of the class definition is not complicated. Remember the class keyword, which is the keyword that defines the class.

The specific syntax of the class definition is as follows.

Class access modifier Modifier Class name
{
    Member of class
}

among them:

  • Class access modifier: used to set access restrictions on the class, including public, internal or not written, when internal or not written means that the class can only be accessed in the current project; public means that the class can be accessed in any project .
  • Modifiers: Modifiers describe the characteristics of the class itself, including abstract, sealed, and static. Abstract is abstract, and classes that use its modifiers cannot be instantiated; sealed modified classes are sealed classes and cannot be inherited; static modified classes are static classes and cannot be instantiated.
  • Class name: The class name is used to describe the function of the class, so it is best to have practical meaning when defining the class name, so that the user can understand the content described in the class. Class names must be unique under the same namespace.
  • Members of the class: elements that can be defined in the class, mainly including fields, attributes, methods.


[Example] Add the class file in the Visual Studio 2015 project.

First create a console application code_1 in Visual Studio 2015, the effect after creation is shown in the figure below.

Console application code_1


Right-click the project name in the above figure, select "Add"-"New Item"-"Class" command in the pop-up menu, and define the class name as Test, as shown in the following figure.

Add class


Click the "Add" button, the content of the added class is as follows.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace code_1
{
    class Test
    {
    }
}

As can be seen from the created Test class, the class created by default does not have any modifier in front of the class keyword, so the class created by default can be accessed in the same project.

In addition, multiple classes can be defined in the same namespace. For example, a class named Test1 is defined in the file shown in the above code, and the code is as follows.

namespace code_1
{
    class Test
    {
    }
    class Test1
    {
    }
}

Although you can define multiple classes in a namespace, it is not recommended to use this method. It is best to define a class for each file, which is convenient for reading and searching.

Not only can users add classes to the console application, but we can also add classes to other types of applications that we will introduce in later learning. The methods for adding are similar.

Guess you like

Origin www.cnblogs.com/hanguoshun/p/12729233.html