Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the characteristics of C # interface and abstract technology

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail what are the technical features of C# interface and abstract category for you. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

1. C# abstract class:

C # abstract classes are special classes that cannot be instantiated; in addition, they have other properties of the class; the important thing is that abstract classes can include abstract methods, which ordinary classes cannot. Abstract methods can only be declared in abstract classes and do not contain any implementations, and derived classes must override them. In addition, C# abstract classes can be derived from an abstract class, can override the abstract methods of the base class or not, and if not, their derived classes must override them.

Second, C# interface:

C # interfaces are reference types, similar to classes, and have three similarities with abstract classes:

1. Cannot be instantiated

2. Contains unimplemented method declarations

3. Derived classes must implement unimplemented methods, abstract classes are abstract methods, and interfaces are all members (not just methods, including other members)

In addition, the C# interface has the following features:

An interface can contain properties, indexers, and events in addition to methods, and these members are defined as public. In addition, you cannot contain any other members, such as constants, fields, constructors, destructors, and static members. A class can directly inherit multiple interfaces, but only one class (including abstract classes).

Third, the difference between C# abstract class and interface:

1. A class is an abstraction of an object. An abstract class can be understood as a class as an object, and an abstract class is called an abstract class. While the interface is just a specification or specification of behavior, Microsoft's custom interface is always followed by an able field, proving that it expresses a class of "I can do it." . Abstract classes are more defined in a series of closely related classes, while most of the interfaces are loosely related classes that implement a certain function.

two。 Basically, the interface does not have any specific features of inheritance, it only promises methods that can be called.

3. A class can implement several interfaces at a time, but can only extend one parent class

4. Interfaces can be used to support callbacks, but inheritance does not have this feature.

5. Abstract classes cannot be sealed.

6. The concrete method implemented by the abstract class is virtual by default, but the interface method in the class that implements the interface is not virtual by default, of course, you can also declare it virtual.

7. (interface) similar to a non-abstract class, an abstract class must provide its own implementation for all members of the interface listed in the base class list of the class. However, abstract classes are allowed to map interface methods to abstract methods.

8. Abstract classes implement a principle in oop that separates the mutable from the immutable. Abstract classes and interfaces are defined as immutable, while variable seat subclasses are implemented.

9. A good interface definition should be functional, not multi-functional, otherwise it will cause interface pollution. If a class only implements one of the functions of this interface and has to implement other methods in the interface, it is called interface pollution.

10. Try to avoid using inheritance to implement the build function, but instead use black box reuse, that is, object composition. Because of the increase in the level of inheritance, the most direct consequence is that when you call a class in this group, you must load them all into the stack! The consequences can be imagined. (understood in combination with stack principle). At the same time, interested friends will notice that Microsoft often uses the method of object composition when building a class. For example, in asp.net, the Page class has attributes such as Server Request, but in fact they are all objects of a certain class. It is a very basic design principle to use this object of the Page class to call the methods and properties of another class.

11. If the abstract class implements the interface, the method in the interface can be mapped to the abstract class as an abstract method without implementation, and the method in the interface can be implemented in the subclass of the abstract class.

Fourth, the use of abstract classes and interfaces:

1. If you expect to create multiple versions of the component, create an abstract class. Abstract classes provide simple ways to control the version of the component.

two。 If the functionality created will be used across a wide range of disparate objects, an interface is used. If you want to design small and concise function blocks, use interfaces.

3. If you want to design a large functional unit, use abstract classes. If you want to provide common implemented functionality across all implementations of a component, use abstract classes.

4. Abstract classes are mainly used for closely related objects; interfaces are suitable for providing general functionality for unrelated classes.

This is the end of this article on "what are the characteristics of C# interface and abstract technology". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report