8.2 OOP Technology
Some other features of the object are discussed below, including:
Interface
Inherited
Polymorphism
Relationships between objects
Operator overloading
Event
Reference types and value types
8.2.1 interface
An interface is the combination of public instance (non-static) methods and properties to encapsulate a collection of specific functionality.
Once you have defined an interface, you can implement it in the class. In this way, the class can support all the properties and members specified by the interface.
Note that the interface cannot exist alone. You cannot instantiate an interface as you instantiate a class. In addition, an interface cannot contain any code that implements its members, but can only define the member itself. The implementation process must be done in the class that implements the interface.
In the previous coffee example, you can combine common properties and methods such as Addsugar (), Milk, Sugar, and Instant into an interface called Ihotdrink (the name of the interface usually begins with the capital letter i). The interface can then be used on other objects, such as objects of the Cupoftea class. So these objects can be handled in a similar way, and the object still retains its own properties (for example, Cupofcoffee still has properties Beantype,cupoftea still have properties Leaftype).
In UML, an interface implemented on an object is represented by a "lollipop" syntax. In Figure 8-6, the Ihotdrink members are placed in a separate box with similar syntax as the class.
A class can support multiple interfaces, and multiple classes can also support the same interface. So the concept of interfaces makes it easier for users and other developers to understand other people's code.
For example, there is some code that uses an object with an interface. Assuming that you do not use other properties and methods of this object, you can replace the object with another object (for example, code that uses the Ihotdrink interface above can handle Cupofcoffee and Cupoftea instances). In addition, the developer of the object can provide an updated version of the object, as long as
It supports interfaces that are already in use and can be used in code with this new version.
After the interface is published, that is, the interface can be used by other developers or end users, it is best not to modify it. One way to understand this is to think of the interface as a contract between the creator and the user of the class. "Each class that supports interface X supports these methods and properties" is valid. If you modify the interface later, perhaps upgrading the underlying code, the user of the interface will not run the interface correctly, or even fail. We should create a new interface to extend the old interface, for example, to include a version number, such as X2. This is the standard way to create an interface, and we will often encounter a numbered interface.
Objects that can be deleted
The IDisposable interface is particularly interesting. Objects that support the IDisposable interface must implement their Dispose () methods, that is, they must provide code for this method. When an object is no longer needed (for example, before the object goes out of scope), this method is called to release the important resource, or the resource waits until the destructor method is called on garbage collection. This gives you more control over the resources used by the object.
C # allows the use of a structure that optimizes the use of this method. The Using keyword can initialize an object in a code block that uses an important resource, and automatically calls the Dispose () method at the end of this block of code, using the following:
New <ClassName>(); ... using (<VariableName>) { ... }
Alternatively, initialize as <VariableName> as part of the using statement:
using 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 has finished executing)
(original) C # learning note 08--Object-oriented Programming introduction 02--oop technology 01--interface