Chapter 9th Defining Classes----Learning Notes

Source: Internet
Author: User

a . definition of Classes in C #

1. Methods for defining classes:

<accessType> class<classname>:<baseclass>,<interface>

{

//c Lass member;

}

Note:

1). by default, the access rights of a class are internal ( internal), only the current project can access it

2). classes also have abstract classes ( AB stract modifiers, which cannot be instantiated, can only be inherited, may have abstract members), and the opposite Seal class ( Sealed modified, not inherited), both types have Public and the Internal type of

3). A class can have only one base class

4). If you inherit an abstract class, you must implement all of the inherited abstract members (unless their base class is also an abstract class)

5). the accessibility of a subclass cannot be higher than the parent class, such as a subclass is Public , the base class cannot be I nternal of the

6). A class that supports interfaces must implement all interface members, but if you do not want to use a given interface, you can provide an "empty" implementation (no function Code)

7). defines the interface in the same way as the definition class, using the keyword inch Terface

8). defining an interface is not available AB stract and the Sealed Retouching

2.system.object

Because all classes inherit the System. Object, all classes have access to sytem.object 's Protect and Public Members. You can also override its methods

3. Constructors and destructors

C # when defining a class, you often do not need to define constructors and destructors, and the compiler adds them automatically, but it can be added when necessary.

1). constructor definition methods:

<ACC esstype> <className> (paramater1,...)

{

Constructor Code;

}

1>. constructors have the same name as a class

2>. the default constructor has no parameters and access control permissions are Public

3>. constructors are used to create instances of classes, but can also have Internal constructor, this constructor cannot create an instance of the class

4>. constructors can have multiple , parameter set cannot be duplicated

2). definition of destructors

~ <className> ()

{

destructor body;

}

3). the execution sequence of the constructor function

1>. instantiating an inherited subclass, you must first instantiate its base class, instantiate its base class, and instantiate its base class until it instantiates Sytem.object So far

2>. Use the period default constructor unless explicitly instructed. The base class must construct the Fir tree by default, regardless of whether the default constructor is used by the subclass.

3>. instantiating subclasses are available if you want to use a non-default constructor of the base class Base (<paramater>) keyword Specifies a constructor that uses fixed arguments

5. Class Library project

If a project contains only classes (and other definitions of related classes, but no entry points), the project is called a class library. Class Library project is compiled as a . dll file

The Class library project is added to the reference and written to the main program with a using statement can be referenced. ..

6. interfaces and abstract classes

1). the similarities between the two

1>. contain members that can be inherited by derived classes

2>. cannot be instantiated directly, but these types of variables can be declared. The variables that inherit both types of objects are then assigned to them, and the same arguments are used for their members

2). the difference between the two

1>. a class can inherit directly from an abstract class (the inheritance chain inherits multiple classes), but a class may have multiple interfaces

2>. Abstract classes can have abstract members ( There is no code body and must be implemented in a derived class, unless the derived class is also abstract ) and non-abstract members; The interface member must be implemented using its class

3>. The definition of an interface member tends to be public, but the members of an abstract class can be private, protected, internal

4>. interfaces cannot contain fields, constructors, destructors, static members, or constants

7. structure and class

struct: A value type that assigns structure a to struct B, which refers to copying the contents of structure a to the structure B in

class: a reference type that assigns the object of class a to the object of class B , which refers to the class a the address of the object in memory is copied to the class B the Object

Chapter 9th Defining Classes----Learning Notes

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.