This article introduces in detail the usage of ASP. NET Abstract classes and some summary and instructions on abstract classes.
To put it simply:
1. abstract classes and interfaces are similar. The difference is that abstract classes can partially implement their members.
2. There must be at least one unimplemented member in the abstract class.
3. the abstract class cannot be instantiated. Its derived class can be instantiated.
Definition method:
The Code is as follows: |
Copy code |
Reference content is as follows: /// <Summary> /// Define an abstract class /// </Summary> Abstract public class Animal { // Define static Fields Static protected int _ id; // Define attributes Public abstract static int Id { Get; Set; } // Define the Method Public abstract void Eat ();
// Define the Indexer Public string this [int I] { Get; Set; } } /// <Summary> /// Implement an abstract class /// </Summary> Public class Dog: Animal { Public static override int Id { Get {return _ id ;} Set {_ id = value ;} } Public override void Eat () { Console. Write ("Dog Eats .") } }
|
Exercise questions:
To implement three types of mobile storage devices: USB flash drive, MP3 player, and mobile hard disk, the computer must be able to exchange data with these three devices. In the future, there may be new third-party mobile storage devices, therefore, the computer must be scalable and can exchange data with storage devices that may occur so far. The reading and writing methods vary between storage devices. There are only two methods for USB flash drives and mobile hard drives. MP3Player also has a playMusic method;
The Code is as follows: |
Copy code |
Using System; Using System. Collections. Generic; Using System. Linq; Using System. Text; Namespace Practices03 { Class Program { Static void Main (string [] args) { Computer myComputer = new Computer (); RSD myRSD = new USB (); MyRSD. ReadDate (); MyRSD. WriteDate (); Console. ReadKey (); } } /// <Summary> // RSD: Mobile storage device /// </Summary> Abstract class RSD { Public abstract void ReadDate (); Public abstract void WriteDate (); } Class USB: RSD { Public override void ReadDate () { Console. WriteLine ("ReadDate method for USB! "); } Public override void WriteDate () { Console. WriteLine ("WtiteDate method for USB! "); } } Class MP3Player: RSD { Public override void ReadDate () { Console. WriteLine ("ReadDate method for MP3Player! "); } Public override void WriteDate () { Console. WriteLine ("WtiteDate method for MP3Player! "); } Public virtual void PlayMusic () { Console. WriteLine ("PlayMusic method for MP3Player! "); } } Class MobileHDD: RSD { Public override void ReadDate () { Console. WriteLine ("ReadDate method for MobileHDD! "); } Public override void WriteDate () { Console. WriteLine ("WtiteDate method for MobileHDD! "); } } Class Computer { Public RSD {get; set ;} Public void ReadDate () { RSD. ReadDate (); } Public void WriteDate () { RSD. WriteDate (); } } } |
When to use abstract classes
An abstract class can be used when a group of related components are required to contain a group of methods with the same functions but require flexibility in other method implementations. Abstract classes are also valuable when version issues are expected, because the base classes are flexible and easy to modify. For more information, see abstract classes and interface suggestions.
For more information about abstract classes and interfaces, see
Abstract class | polymorphism in components | MustInherit (Visual Basic) | abstract (C #)
Designing a function as an interface or an abstract class (MustInherit class in Visual Basic) is sometimes difficult. An abstract class is a class that cannot be instantiated but must be inherited from it. Abstract classes can be fully implemented, but more commonly, they are partially implemented or are not implemented at all, so as to encapsulate the general functions of the inherited classes. For more information, see abstract classes.
On the contrary, an "interface" is a fully abstract set of members and can be seen as defining contracts for operations. The implementation of interfaces is left to developers.
Interfaces and abstract classes are useful for component interaction. If a method requires an interface in the form of a parameter, any object implementing this interface can be used in this parameter. For example:
Visual Basic
Public Sub Spin (ByVal widget As IWidget)
End Sub
C #
Public void Spin (IWidget)
{}
This method can accept any object that implements IWidget as a widget parameter, even if the implementation of IWidget may be very different. Abstract classes also allow this polymorphism, but note the following:
Classes may only inherit from a base class, so if you want to use abstract classes to provide polymorphism for a group of classes, these classes must all inherit from that class.
Abstract classes may also provide implemented members. Therefore, you can use abstract classes to ensure that a specified number of functions are the same, but you cannot use interfaces to do so.
Here are some suggestions to help you decide whether to use interfaces or abstract classes to provide polymorphism for components.
If you want to create multiple versions of a component, create an abstract class. Abstract classes provide a simple way to control component versions. By updating the base class, all inheritance classes are automatically updated with the change. On the other hand, the interface cannot be changed once it is created. If you need a new version of the interface, you must create a new interface.
If the created function is used across a wide range of different objects, the interface is used. Abstract classes are mainly used for closely related objects, and interfaces are most suitable for providing general functions for irrelevant classes.
If you want to design small and concise functional blocks, use interfaces. If you want to design a large functional unit, use an abstract class.
If you want to provide common implemented functions among all the implementations of a component, use an abstract class. Abstract classes allow partial implementation classes, while interfaces do not include the implementation of any member.