(original) C # learning note 09--defining class definitions in class 01--c#

Source: Internet
Author: User

The Nineth chapter defines the class

The contents of this chapter:

How to define classes and interfaces in C #

How to use keywords that control accessibility and inheritance

The System.Object class and its role in the class definition

How to use some of the help tools provided by VS and VCE

How to define a class library

Similarities and differences of interfaces and abstract classes

More about structure types

Some important information for copying objects

9.1 Class definitions in C #

C # uses the class keyword to define classes:

      class MyClass       {           //      }

  by default, a class is declared internal, which means that only code in the current project can access it . You can use the internal access modifier keyword to explicitly specify as follows (but this is not necessary):

      Internal class MyClass       {           //      }

In addition, you can specify that the class is public and should be accessible by code in other projects. To do this, use the keywordpublic.

       Public class MyClass       {           //      }

Note: Classes declared in this manner cannot be private or protected. You can use the modifiers of these declaration classes for declaring class members, as described in chapter 10th.

In addition to these two access modifier keywords, You can also specify that the class is abstract (cannot be instantiated, can inherit only, may have abstract members), or sealed (sealed, cannot inherit). To do this, 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       {           //      

Where MyClass is a public abstract class, or it can be an internal abstract class.

The declaration of the sealed class is as follows:

       Public Sealed class MyClass       {           //      }

As with abstract classes, sealed classes can also be public or internal.

  You can also specify inheritance in the class definition. To do this, add a colon after the class name followed by the base class name , for example:

       Public class Myclass:mybase       {           //      }

  Note that in C # class definitions There can be only one base class, and if you inherit an abstract class, you must implement all of the inherited abstract members (unless the derived class is also abstract).

  The compiler does not allow derived classes to be more accessible than the base class . In other words, an inner class can inherit from a common base class, but a public class cannot inherit from an inner class. Therefore, the code below is legal:

       Public class MyBase       {           //      }      internalclass  myclass:mybase       {           //      }

However, the following code cannot be compiled:

      Internal class MyBase       {           //      }              publicclass  myclass:mybase       {           //      }

  If you do not use a base class, the class that is defined inherits only the System.Object (its alias in C # is Object). After all, in an inheritance hierarchy, the root of all classes is System.Object.

In addition to specifying a base class in this manner, you can also specify a supported interface after the colon . if a base class is specified , it must be immediately after the colon, followed by the specified interface . If you do not specify a base class , the interface is followed by a colon. you must use commas to separate the base class name (if there is a base class) and the interface name .

For example, add an interface to MyClass as follows:

       Public class Myclass:imyinterface       {           //      }

  all interface members must be implemented in a class that supports this interface, but if you do not want to use a given interface member, you can provide an "empty" implementation (with no function code). You can also implement an interface member as an abstract member in an abstract class.

The following declaration is not valid because the base class MyBase is not the first item in the Inheritance list:

       Public class Myclass:imyinterface, MyBase       {           //      }

The correct way to specify base classes and interfaces is as follows:

       Public class Myclass:mybase, IMyInterface       {           //      }

Multiple interfaces can be specified, so the following code is valid:

       Public class myclass:mybase, IMyInterface, Imysecondinterface       {            //      

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

  

  

Definition of the interface

The interface is declared in a manner similar to the way it is declared, but the keyword used is interface, not class, for example:

      Interface IMyInterface       {           //      }

Access modifier keywords public and internal are used the same way, and as with classes, interfaces are defined as internal interfaces by default. So in order for the interface to be publicly accessible, you must use the Public keyword:

       Public Interface IMyInterface       {           //      }

You cannot use the keyword abstract and sealed in an interface, because these two modifiers are meaningless in the interface definition (they do not contain implementation code and cannot be instantiated directly and must be inheritable).

The inheritance of an interface can also be specified in a similar way to class inheritance. The main difference is that you can use multiple base interfaces , such as:

       Public Interface Imyinterface:imybaseinterface, IMyBaseInterface2       {           //      }

  the interface is not a class, so there is no inheritance System.Object. For convenience, however, members of the System.Object can be accessed through variables of the interface type . As mentioned above, you cannot instantiate an interface in a way that instantiates a class. The following example provides some code for class definitions and code to use them.

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacech09ex01{ Public Abstract classMyBase {}Internal classmyclass:mybase {} Public InterfaceImybaseinterface {}Internal InterfaceIMyBaseInterface2 {}Internal InterfaceImyinterface:imybaseinterface, IMyBaseInterface2 {}Internal Sealed classMycomplexclass:myclass, IMyInterface {}classProgram {Static voidMain (string[] args) {Mycomplexclass MYOBJ=NewMycomplexclass ();            Console.WriteLine (Myobj.tostring ());        Console.readkey (); }    }}

Execute project, result.

  

description of the example

This project defines the classes and interfaces in the following inheritance hierarchy, as shown in 9-2.

  

This includes program because the class is defined in the same way as other classes, and it is not part of the main class hierarchy. The main () method that this class handles is the entry point for the application.

The code in Main () calls the ToString () method of an instance of Mycomplexclass MyObj:

      New Mycomplexclass ();       Console.WriteLine (Myobj.tostring ());

  This is a way of inheriting from System.Object. (not shown, the figure omits members of this class, makes the diagram clearer), and returns the class name of the object as a string, which is qualified with any associated namespace (the namespace in this example is: ch09ex01).

  

(original) C # learning note 09--defining class definitions in class 01--c#

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.