C # Getting started-class definition in C #9.1

Source: Internet
Author: User

C # Use the class keyword to define the class. Its basic structure is as follows:

Class myclass

{

// Class member

}

After defining a class, you can access other places in the project to instantiate the class. By default, the class is declared as internal (internal), that is, onlyCodeTo access him. The internal access modifier can be explicitly specified, but this is unnecessary. In addition, you can specify that the class is public and can be accessed by code in other projects. To this end, you must use the public keyword.

Pbulic class myclass

{

// Class members

}

In addition to the two access modifiers, you can also specify that the class is abstract (not instantiated, can only inherit, can have abstract members) or sealed (not inherited ). Therefore, you can use two mutually exclusive keywords abstract or sealed. Therefore, the abstract class must be declared in the following way:

Public abstract class myclass

{

// Class members, may be abstract.

}

Myclass is a public abstract class or an internal abstract class. The Declaration of the seal class is as follows:

Public sealed class myclass

{

// Class members.

}

Like an abstract class, the sealing class can also be public or internal.

You can also specify inheritance in the class definition. Therefore, add a colon to the end of the class name, followed by the base class name, for example:

Public class myclass: mybase

{

// Class members.

}

Note: In the definition of C #, only one base class is allowed. If an abstract class is inherited, all abstract members inherited must be implemented (unless the derived class is also an abstract class ).

The compiler does not allow a derived class to have higher accessibility than its base class. That is to say, the internal class can inherit from a public class, but the public class cannot inherit from an internal class. Therefore, the following code is legal:

Public class mybase

{

// Class members.

}

 

Internal class myclass: mybase

{

// Class members.

}

However, the following code cannot be compiled:

Internal class mybase

{

// Class members.

}

 

Public class myclass: mybase

{

// Class members.

}

If the base class is not used, the defined class only inherits from the base class system. Object (its alias in C # is object ). After all, in the inheritance hierarchy, the root of all classes is system. object.

In addition to specifying the base class in this way, you can also specify the supported interfaces after the colon. If a base class is specified, it must be followed by the colon, followed by the specified interface. If no base class is specified, the interface follows the colon. You must use commas to separate the base class names (if any) and interface names.

For example, add an interface to myclass as follows:

Public class myclass: imyinterface

{

// Class members.

}

All interface members must be implemented in classes that support this interface. However, if you do not want to use a given interface member, you can provide an "empty" execution method (without function code ). You can also implement an interface member as an abstract member in an abstract class.

The following statement is invalid because the base class mybase is not the first item in the inheritance list:

Public class myclass: imyinterface, mybase

{

// Class members.

}

The correct method for specifying the base class and interface is as follows:

Public class myclass: mybase, imyinterface

{

// Class members.

}

You can also specify multiple interfaces, so the following code is valid:

Public class myclass: mybase, imyinterface, imysecondinterface

{

// Class members.

}

Table 9-1 is a combination of access modifiers that can be used in the class definition.

Modifier

Description

none or internal

the class can only be accessed in the current project.

Public

class can be accessed anywhere.

abstract or internal abstract

A class can only be accessed in the current project. It cannot be instantiated and can only be inherited.

public abstract

A class can be accessed anywhere. It cannot be instantiated and can only be inherited.

sealed or internal sealed

A class can only be accessed in the current project and cannot be derived.

Public sealed

Class can be accessed anywhere and cannot be derived.

 

The method for declaring an interface is similar to that for declaring a class, but the keyword used is interface rather than class. For example:

Interface imyinterface

{

// Interface members.

}

The access modifier keywords public and internal are used in the same way. To make the interface publicly accessible, you must use the keyword public:

Public interface imyinterface

{

// Interface members.

}

Abstract and sealed keywords cannot be used in the interface, because these two modifiers are meaningless in the interface definition.

Interface inheritance can also be specified in a similar way as class inheritance. The main difference is that you can use multiple basic interfaces, such:

Public interface imyinterface: imybaseinterface, imybaseinterface2

{

// Interface members.

}

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.