C # entry-classic-8.2 OOP Technology

Source: Internet
Author: User

8.2.1 Interface

An interface combines implicit public methods and attributes to encapsulate a set of specific functions. After defining an interface, you can implement it in the class to support all attributes and members specified by the interface. You can combine attributes and methods of a general purpose into an interface, and then use this interface in the class.

Note: The interface cannot exist independently. You cannot instantiate an interface as if you had instantiated a class. In addition, an interface cannot contain any code that implements its members, but can only define the members themselves. The implementation process must be implemented in the class that implements the interface.


Objects that support the idisposable interface must implement their dispose () method, that is, they must provide code for this method. When you no longer need an object, call this method to release important resources. Otherwise, the resource will not be released until the garbage collection calls the destructor.

C # allows you to use a structure that can optimize the use of the dispose () method. The Using Keyword can initialize objects that use important resources in a code block. The dispose () method is automatically called at the end of the code block. The usage is as follows:

<Classnme> <variablename> = new <classname> ()

....

Using (<variablename>)

{

....

}

You can also use the initialization object <variablename> as part of the using statement:

Using (<classname> <variablename> = new <classname> ())

{

....

}

In both cases, the variable <variablename> can be used in the using code block and automatically deleted at the end of the code block (call dispose () after the code block is executed ()).

 

8.2.2 inheritance

Inheritance is one of the most important features of OOP. Any class can be inherited from another class, that is, this class has all the members of the class it inherits. In Oop, inherited (also called derived) classes are called parent classes (also called base classes ). Note that the C # object can only be directly derived from a base class. Of course, the base class can also have its own base class.

Inheritance can be expanded from a general base class or more specific classes can be created. For example, consider a category that represents poultry. This class is called animal and has methods such as eatfood () or breed (). We can create a derived class cow that supports all these methods. It also has its own methods, such as MoO () and supplymilk (). You can also create another derived class chicken, which has the cluck () and layegg () methods.

When inheriting a base class, the accessibility of members becomes an important issue. A derived class cannot access private members of a base class, but can access its public members and protected members.

In addition to defining the protection level of a member, you can also define its inheritance behavior for the member. The base class member can be virtual, that is, the member can be rewritten by the derived class. A derived class can provide other code for executing a member based on the base class. This type of Execution Code does not delete the original code, but can still access the original code in the class, but the external code can only access the Execution Code provided by the derived class. If no other execution method is provided, the external code accesses the Execution Code of the members in the base class.

The base class can also be defined as an abstract class. Abstract classes cannot be directly instantiated. To use an abstract class, you must inherit this class. abstract classes can have abstract Members who do not execute code in the base class. These Execution Code must be provided in the derived class. Abstract base classes can provide member implementation code, which is very common. The abstract class cannot be instantiated, which does not mean that the function cannot be encapsulated in the abstract class.

The sealing class cannot be used as the base class.

8.2.3 Polymorphism

A result of inheritance is that the (several) classes derived from the base class overlap methods and attributes. Therefore, you can use the same syntax to process objects of different classes inherited from the same base class. For example, if the base class animal has a method eatfood (), the method is called from its cow and Chicken classes. Its syntax is similar:

Cow mycow = New Cow ();

Chicken mychicken = new chicken ();

Mycow. eatfood ();

Mychicken. eatfood ();

Polymorphism is a step forward. You can assign the variables of the derived class to the variables of the base class, for example:

Animal myanimal = mycow;

You can use this variable to call the method of the base class without forcing type conversion:

Myanimal. eatfood ();

The result is to call eatfood () in the derived class to execute the code. Note: The methods defined on the derived class cannot be called in the same way, and the following code cannot be run:

Myanimal. Moo ();

However, you can convert the base class variable to the derived class variable and call the method of the derived class, as shown below:

Cow mynewcow = (COW) myanimal;

Mynewcow. Moo ();

If the type of the original variable is not the base class inherited by cow, this forced type conversion will produce an exception.

Note that not only classes that share the same parent class can exploit polymorphism. As long as subclasses and grandchildren have the same class in the inheritance hierarchy, they can exploit polymorphism in the same way. Note that in C #, all classes are derived from the same class object, and the object is the root of the hierarchy.

Interface Polymorphism

The interface concept is to combine related attributes and methods. Although the interface cannot be instantiated like an object, you can create a variable of the interface type, and then use this variable to access the methods and properties provided by the interface on the object that supports this interface.

Assume that the eatfood () method provided by the base class animal is not used, but the method is put on the iconsume interface. The cow and Chicken classes also support this interface. The only difference is that they must provide the Execution Code of the eatfood () method (because the interface does not contain the Execution Code ), then you can use the following code to access this method:

Cow mycow = New Cow ();

Chicken mychicken = new chicken ();

Iconsume consumeinterface;

Consumeinterface = mycow;

Consumeinterface. eatfood ();

Consumeinterface = mychicken;

Consumeinterface. eatfood;

This provides access to multiple objects in the same way and does not depend on a public base class.

Note that the derived class inherits the interfaces supported by its base class. Classes with common base classes do not necessarily have common interfaces, and vice versa.

8.2.4 relationship between objects

Include link: one class contains another class. This is similar to an inheritance relationship, but the contained class can control the access to the members of the contained class, and even use the members of the contained class for other processing.

Set relationship: A class is used as a container for multiple instances of another class. This is similar to an object array, but the set has other functions, including indexing, sorting, and resetting the size.

1. Inclusion relationship

You can declare a class as a member field to implement the inclusion relationship. This member field can be a public field. In this case, just like the inheritance relationship, the container object user can access its methods and attributes, but it cannot use the internal code of the category class of the derived class like the inheritance relationship. That is to say, you can only access the public attributes and methods of the contained classes.

In addition, the contained Member objects can be converted into private members. In this way, users cannot directly access their members, even if they are public members. However, you can use the members that contain the class (indirectly) to access these private members. You can perform other operations on the members that contain the class before accessing the members of the contained class.

For example, the cow class contains a udder class, which has a common method milk. The cow object can call this method as required as part of its suplymilk () method, but users of the cow object cannot see these details.

In UML, the contained classes can be represented by correlated lines. For simple include relationships, you can use a line with 1 to describe the one-to-one relationship.

2. Set relationships

Use arrays to store multiple variables of the same type, which also applies to objects. For example:

Animal [] animals = new animal [5];

The set is basically an array, and the set is implemented as a class in the same way as other objects. They are usually named in the plural form of the stored object name. For example, animals class contains a set of animal objects.

The main difference between an array and a set is that a set usually performs additional functions. For example, the add () and remove () methods can be used to add or delete items in the set. In addition, a set usually has an item attribute, which returns the object based on the object index. In addition, this attribute allows more complex access methods. For example, if an animals object is designed, the animal object can be accessed according to its name.

8.2.5 Operator Overloading

You can use operators to instantiate objects from classes because classes can contain instructions on how operators are operated.

For example, add a new attribute weight to animal. Use the following code to compare the poultry weight:

If (cowa. weight> cowb. Weight)

{

...

}

Using Operator overloading, You can implicitly use the weight logic in the Code, for example, the following code:

If (cowa> cowb)

{

...

}

You can also overload operators to process different classes in the same way. One (or two) class defines the code to achieve this purpose.

Note that you can only use this method to reload existing C # operators, and cannot create new operators.

8.2.6 events

Objects can activate events as part of their processing. Events are very important and can work in other parts of the Code, similar to exceptions (but more powerful ). For example, you can execute specific code when adding an animal object to an animals set. This part of code is not part of the animals class or part of the code that calls the add () method. Therefore, you need to add an event handler to the Code. This is a special type of function that is called when an event occurs. You also need to configure this handler to listen to events we are interested in.

8.2.7 reference type and Value Type

In C #, data is stored in a variable in either of the two ways based on the variable type. There are two types of variables: reference type and value type. The differences are as follows:

  • Value types store themselves and their content in a place in memory (called stacks.
  • The reference type stores a reference in one place (called heap) of the memory, while the content is stored in another place.

A major difference between the value type and the reference type is that the value type always contains a value, and the reference type can be null, indicating that they do not contain values. However, you can create a value type with an empty type (this is a form of generics). The behavior of the value type in this respect is similar to the reference type (which can be null ).

Class is the reference type.

The key difference between the structure type and the class is that the structure type is the value type.

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.