As long as the public interface is protected, private implementation can be safely implemented, which is the target. The way to ensure this goal is made below. Inheritance and Interface In addition to encapsulation in the object's method and attribute, the complex system is easy to create and modify, OOP also supports class inheritance and interface implementation. In this way, new categories can be safely added to existing systems. You don't have to modify existing code. These two methods are usually more than preparing, and the study is for every way to be appropriate and discussing the third method - subject portfolio, which is often the best choice. Inheritance hierarchy in all OOP technologies existing in Visual Basic .NET, inherits the most unfamiliar for programmers from Visual Basic 6.0 because there is no inheritance in Visual Basic 6.0. Even the non-Visual Basic object model that Visual Basic 6.0 is easy to use, such as the ADO or Office library, rarely needs to be understood. By inheritance can create a new class, it is an existing (basis) variant, and replaced with a new inheritance at any initial call base class. For example, there is a class named SalesOrder that derives a class named SalesOrder from this class: Public Class WebsaSorder Inherits SalesOrder is now remained unchanged, and it will remain unchanged and can also process WebsalesOrder objects. If SalesOrder has a CONFIRM method, you can ensure that the WebSalesOrder class also has a CONFIRM method. However, if the confirm method is marked in the SalesORDER class, you can create a new CONFIRM method in the WebsaSaDer class to overload the original method: Public overrides sub confirm may send a fax to the customer, and WebsalesORDER The CONFIRM method in the class uses emails. Importantly, a method originally set to accept SalesOrder type objects, now if the WebsalesORDER object is passed, this method can also run normally, and after the method calls the object's confirm method, the customer will receive an email. Not a fax. The old code can call the new code without any modifications. On the other hand, it is assumed that there is a Total that is identical to the two classes. Detective Class WebsalesOrder does not need to make any modifications to this method - its implementation will automatically inherit from the base class, and you can call any WebsalesOrder object's Total method, so that this object is like a SalesOrder object. Polymorphism, or alternative, inheritance is a significant advantage: Whenever a derived object is created, this derived class object can be used in places where the base class object is used. WebsalesOrder object "IS A" SalesOrder object, WebsalesOrder objects must be able to implement all features of the SalesOrder object, even in your own unique way. The multi-state of many OOP has a key factor, and will be seen later, it is not only in the inheritance, but also in the interface. Using polymorphism, different types of objects can be used to handle a set of universal messages used in interactive, and is carried out in their respective modes. The order confirmation code does not need to know how to perform confirmation, nor does it need to know the type of the confirmation order.
It only cares about whether it can call the confirm method of the order object being processed, it depends on this object processing confirmation details: Public Sub ProcessOrder ORDER.Confirm When calling the ProcessOrder process, you need to deliver SalesOrder or WebsaSaSORDER objects. , Pass any object programs to run. The virtual method and attributes are only used when they are re-served, and virtual can be the most mysterious overview of inheritance. Its mystery is to automatically find and run the most special implementation of the called method or attribute when .NET runtime. For example, calling the Order.confirm method in the above example will call the CONFIRM method for the WebsalesORDER class, where the salesORDER.CONFIRM method is overloaded. However, calling the Order.Total method calls the Total method for the SalesOrder class because there is no existing Total method in the WebsaSaSorder class. Abstractions and methods are abstraction that includes at least one class or attribute that must be overloaded. For example, create a SalesOrder class that does not have an order confirmation. Because different types of orders must be confirmed in a different way, it is to be derived from the SalesOrder class and overload the CONFIRM method to provide its own implementation. This means that you can never create a SalesOrder. Instead, only the derived class can create an object, populate the details of how to perform the order. Since this reason SalesORDER class will be marked as Mustinherit: Public Mustinherit Class SalesOrder Public Mustover () Public Sub Total () 'Calculation Total Calculation End Sub If you have to overload all class members, then this class is called pure abstraction class. For example, the SalesORDER class can only contain methods and properties that must be derived. But as long as one member must be overloaded, this class must be marked as an abstract class with the Mustinherit property. Type Check and Down Modulation If some orders need to confirm that some orders do not need to be confirmed, what is the result? You can create a SalesORDER base class that does not contain the confirm method, but adds a CONFIRM method only in the derived class that needs to be confirmed. But there will be a problem: I now want to create a program to handle all types of orders and confirm when needed. How do you know which order needs to be confirmed? Provides a SalesOrder type for this program to allow all types of derived from SalesOrder to be incoming. But this needs to be tested for all confirmation types before calling the CONFIRM method. Moreover, once the order type is confirmed, then it is necessary to look down from the SalesORDER class (there in the CONFIRM method in this class) to the derived class: Public Sub ProcessOrder (Order As SalesOrder) â. â âof (Order) IS WebsalesORDER THEN CTYPE (ORDER, WebsalesOrder) .confirm elseif typeof (Order) IS EmailsalesORDER THEN CTYPE (ORDER, EmailsalesOrder) .confirm 'and so on is difficult to maintain. This method must be modified each time a class is derived from SalesOrder in the system. This coupling of old code and new code is exactly what is avoided.
This is the same as the confirmation code of different categories of sales orders in this process. IF TypeOf (Order) IS WebsalesOrder Then 'is written here. ISEF TYPEOF (Order) IS EmailsaSorder The ELSEF TYPEOF (ORDER) IS EmailsalesOrder Then' This is written here to confirm the code of Emailsales. 'Waiting in this way is worse: each time you create a new type, you must modify this program, and each sales object is no longer confirmed, the result makes the programmer difficult to add new types of orders. How can programmers know that when processing new type orders, what is the code needs to add code? Another method is to create an intermediate abstract type named ConfirMables Order (derived from the SalesOrder class, with must be overloaded. Confirm). Then derive the type of confirmation from the ConfirMablesalesOrder, and other types are derived from SalesOrder. The program must check if the incoming SalesOrder object is a CONFIRMABLESALESORDER type. If so, the CONFIRM method will be called using this type. If TypeOf (Order) IS ConfirMables Order Then CType (Order, ConfirMablesalesOrder) .confirm To use the CONFIRM method, CTYPE modeling is still required. However, by virtuality, calls will be automatically passed to the class that creates the order object, and the Confirm method defined in this class is run. The problem seems to solve, but this is just a temporary solution. Guess why? It is assumed that the next step is: some types of orders require a credit card. There is a confirmation of the order that requires a credit card, while others do not. There is a problem. There is no multiple inheritance in .NET, you can derive the CreditcheckablesaSorder type from SalesOrder. Some order types are derived from CONFIRMABLESALESORDER, and others are derived from Creditcheckables, but what will I do if I need to confirm that the order is required? One limit on inheritance in .NET Framework is a type that can only be derived from a base type. The order type cannot be derived from the CONFIRMABLEORDER and derived from CreditcheckableOrder. This may be considered to be exclusive or misleading, but there are many reasons for this. Supports multiple inheritance in C . However, all other popular object-oriented languages, including Java, do not allow multiple inheritance. (Some advanced languages, such as Eiffel, attempted to design different types of multiple inheritations, used for .NET's Eiffel even provided multiple inheritances on Microsoft .NET platform.) Multiple inheritance is the problem is that when compiler needs When you find the correct implementation of the virtual method, uncertainty. For example, it is envisaged that hound and puppy are derived from the DOG, and BabyBasset inherits from hound and puppy: Figure 1. Multiple inheritance issues assume that DOG has an overloaded Bark method. HOUND was overloaded to make it sound like an angry, and Puppy also overloaded it made it sounded like a scream, but Babybasset did not overload Bark. If you have created a BabyBasset object, then call it, what is the result, anger is still screaming? .NET Framework requires that derived classes can only have a base class to prevent this problem from happening. This limitation also means that each class is finally derived from a single grandfather system.Object class.
Single path type inherit means processing the .NET object in a method of being used as a parameter by the System.Object type. Single path type inheritance is critical in debris collection, because the debris collector is to release the memory that is not accessible to objects, and if it is, it can process all types of objects. Allows all objects to shape (UPCAST) is a common type, showing a more familiar example of its advantages is Event handler: public sub myeventhandler (by valle sender as _ system.object, by val val e as system.eventargs) The program can be connected to an event from any combination of any object or object because the parameter sender is the System.Object type, and any .NET object can replace it. If necessary, you can use System.Reflection.gettype () to identify what type of Sender object. Creating and usteling Interfaces Create and implement this kind of subtlety inherited, but what should I do for sales orders that need to be confirmed and / or credit cards? The answer is to use the interface. The problem caused by multiple inheritance of the class is caused by potential conflicts between general methods in the inheritance chain. However, what is the pure abstract class without a specific implementation? In this case, multiple inherits do not cause any problems, because there is no specific implementation, it will not cause conflicts. This is the functionality provided by the interface: inherits a group of methods and attribute descriptions, but does not care about its specific implementation, so inheritance from multiple interfaces will not cause problems. Although the phrase "interface inherit" is often used, the correct term is an interface implementation. An interface is inherited from another interface, so that the interface manipulation method can extend to a method containing the interface that it inherits it. However, to use the Visual Basic .NET class interface must implement these interfaces instead of inheriting them: Public Interface IConfirmable Sub Confirm () End InterfacePublic Class WebSalesOrder () Inherits SalesOrder Implements IConfirmable Public Sub Confirm () Implements IConfirmable.Confirm ' Confirm the code End Sub 'of the Web Order End Sub' End Class (in C #, the colon is used to express class inheritance and interface implementation. This may be the reason it is usually prefixed before the interface is prefixed. This way C # programmers can easily distinguish between base classes and interfaces.) You can create some sales order types, some implementation IconFirMable, some of which implements iCreditcheckable, and some are all implemented. To check if the order needs to be confirmed, the code and check the order inherited from a specific type, and the model is the same as the type of code: Public Sub ProcessOrder (Order As SalesOrder) IF Typeof (Order) IS IConfirMable the ctype (Order, IconfirMable) .confirm interface polymorphism interface also provides the advantages of polymorphisms with derivation classes.
For example, any method that implements iConFirMable is passed to the method of IconFirMable parameters: public sub confirmorder (Order as iconfirmable) Order.confirmend Sub If WebsalesOrder and EmailsalesORDER are implemented, you can pass any object to the Confirmorder method. . When the ORDER.CONFIRM is called, the confirmation code implemented in the appropriate class will run. Even if the class is not named CONFIRM, the program can run normally when it is marked as a Confirm method that implements the iconfirMable interface. Public Class WebsalesOrder () Inherits SalesOrder Implements IconFirMable Public Sub ConfirmWeborder ()_Mplements iconfirMable.confirm 'confirms that the code End Sub free naming is a useful feature. If the class implements two different interfaces, this characteristic is to take advantage of the method of the same name in the interface. Comparison class inheritance and interface implementation of the most important technological difference between the creation of the school, the implementation of the interface is that the derived class can only inherit from one base class, but a class can implement multiple interfaces. From a design perspective, inheritance is a special type of relationship. If WebSalesOrder is a special type of SalesOrder, then consider using derived classes. , However, should be careful, when distinguish between derived classes and base classes is also a feature that requires support, do not use inheritance. Interface implementation provides greater flexibility when adding this type of characteristics or capabilities to classes. Inheriting the inheritance hierarchy for building a frame design requires detailed planning, and how to use inheritance. To treat it as an experienced software designer (and he is creating a framework, programmers should use this framework system to build a number of applications) need to complete the task, not as the strategy used in simply building a specific application . The .NET Framework itself contains many inheritance instances being used, and needs to create a fault class to perform a lot of usual programming activities. For example, you can derive your own proprietary anomaly class from the ApplicationException class to store custom error messages. When the custom event is released, the information is sent to the event handler by deriving a custom class from Eventargs. To create a particular type of collection, you can derive it from the CollectionBase class. Each time you create a Windows Form in the Visual Studio .NET, a class is derived from the Windows.Forms.Form base class. You should be used to send students from the base class provided by the frame designer, but be careful when creating your own base class. To ensure a clear level is being expressed, and the behavior of the client programmer to be overloaded is analyzed. Be careful when you create your own interface, but use the interface than the inheritance is not easy to go to the dead angle, so you can take care of them if you can choose. Object portfolio When considering creating its own inheritance, do not affect the voices of the reusable code. Reuse itself is not a sufficient reason for the creation of a part. With its inheritance allows new objects to use existing objects, it is better to use techniques called combinations, inclusion, aggregation, or packaging. This technology you might use in Visual Basic 6.0, but there is no inheritance in Visual Basic 6.0. For example, to create a WebsalesORDER class, this class reuses all the code in the SalesOrder class, and adds some new code, declare and create an instance of the SalesOrder object in the WebsaSaDER class. It can be publicly exposed to the interior SalesOrder object, or it can be saved as private.
Agent If there is a Total method in the SalesORDER class, the WebsalesOrder class has a Total method by simply calling the Total method of the private SalesORDER instance. The technique of passing method calls (or attribute calls) to internal objects is often referred to as agents, but do not use it to create a backup function or event handler to use the proxy object in .NET. As in Visual Basic 6.0, through the objects included with the WitHevents keyword declaration, the events containing objects can be exposed to the package class. The main disadvantage of using the interface implementation and combination using the object portfolio and the agent is that polymorphisms like derived class cannot be automatically obtained. If the WebsalesORDER object simply contains the SalesOrder object, not from another object, then the WebsaSaSorder object cannot be passed to a method that requires the SaleOrder type as a parameter. By creating an IsalesORDER interface, you can overcome this shortcomings by creating this interface. It is also possible to provide a proxy for methods and attributes that are included in SalesOrder objects. Or, if you need, WebsalesOrder can also independently implement the methods and properties in the interface, not the agent to the SalesOrder object. This is similar to the overload of the derived class. Combining the object portfolio with the agent, you can reuse the code, realize polymorphism, without designing headache inheritance. For this reason, this method is preferred when it is necessary to expand or provide a proprietary function on the class. What is the difference between Visual Basic 6.0? In Visual Basic 6.0, each time you create a class module, you automatically create a class interface. Consider the following Visual Basic 6.0 code: 'Visual Basic 6.0 Code DIM MyObject as myclassset myObject = new myclass In this code, when using MyClass for the first time, point to hidden interfaces that contain null methods and properties, the second Myclass is The specific classes of these methods and properties are implemented. Visual Basic 6.0 is very important to shield your interface, and the use of the interface is very important for the underlying COM pipeline. The Implements key in Visual Basic 6.0 allows you to express interfaces and utilize interface-based polymorphisms existing in Visual Basic .NET. However, class implementation must use naming conversion of interface names: 'Visual Basic 6.0 iconfirMable class module public sub confirm () End Sub