In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "what is the use of the .NET interface", the content is simple and clear, and I hope it can help you solve your doubts. Let the editor lead you to study and learn the article "what is the use of the .NET interface?"
What is the .NET interface?
An interface is an abstract type that contains a set of virtual methods, each with its name, parameters, and return value. Interface methods cannot contain any implementations, and CLR allows interfaces to contain events, properties, indexers, static methods, static fields, static constructors, and constants. Note, however, that C# cannot contain any static members. A class can implement multiple interfaces, and when a class inherits an interface, it implements not only all the methods defined by that interface, but also all the methods that the interface inherits from other interfaces.
The definition method is:
Public interface System.IComparable {int CompareTo (object o);} public class TestCls: IComparable {public TestCls () {} private int _ value; public int Value {get {return _ value;} set {_ value = value;}} public int CompareTo (object o) {/ / use as mode to determine the transition TestCls aCls = o as TestCls If (aCls! = null) {/ / implement the abstract method return _ value.CompareTo (aCls._value);}
What is a .NET abstract class?
Abstract classes provide a common definition of a base class shared by multiple derived classes, which can provide both abstract and non-abstract methods. Abstract classes cannot be instantiated and their abstract methods must be implemented by inheriting derived classes, so the new keyword cannot be used on abstract classes and cannot be sealed. If a derived class does not implement all abstract methods, it must also be declared as an abstract class. In addition, the implementation of abstract methods is implemented by overriding methods.
The definition method is:
/ define abstract class / abstract public class Animal {/ / define static field static protected int _ id; / / define attribute public abstract static int Id {get; set;} / / define method public abstract void Eat (); / / define indexer public string this [int I] {get; set } / public class Dog: Animal {public static override int Id {get {return _ id;} set {_ id = value;}} public override void Eat () {Console.Write ("Dog Eats.")}}
Similarities and differences between .NET Interface and .NET Abstract Class
Now that you know what the .NET interface and the .NET abstract class are, let's take a look at the similarities and differences between them.
Identical point
All of them cannot be instantiated directly, and their abstract methods can be implemented through inheritance.
They are the technical basis of abstract programming, and many design patterns are realized.
Differences
Interfaces support multiple inheritance; abstract classes cannot implement multiple inheritance.
Interfaces can only define abstract rules; abstract classes can define rules and may also provide implemented members.
An interface is a set of behavioral specifications; an abstract class is an incomplete class, focusing on the concept of a family.
Interfaces can be used to support callbacks; abstract classes cannot implement callbacks because inheritance does not support them.
Interfaces contain only methods, properties, indexers, and event signatures, but cannot define fields and methods that contain implementations; abstract classes can define fields, properties, and methods that contain implementations.
Interfaces can act on value types and reference types; abstract classes can only act on reference types. For example, Struct can inherit an interface, not a class.
Through the same and different comparisons, we can only say that interfaces and abstract classes have their own advantages, but no advantages. In the actual programming practice, we have to be discretionary according to the specific situation, but the following experience and accumulation may give you some inspiration. In addition to some of my accumulation, many of them come from classics, which I believe can stand the test. So in the rules and occasions, we learn these classics, the most important thing is to apply what we have learned. Of course, I will laugh at you with one word. Please continue.
Rules and situations of .NET Interface and .NET Abstract Class
Remember, one of the most important principles of object-oriented thinking is interface-oriented programming.
With the help of interfaces and abstract classes, many of the ideas in the 23 design patterns have been cleverly implemented, and I think the essence is simply: abstract-oriented programming.
Abstract classes should be used primarily for closely related objects, while interfaces are best suited to provide common functionality for unrelated classes.
Interfaces focus on CAN-DO relational types, while abstract classes focus on IS-A-style relationships
Interfaces define the behavior of objects; abstract classes define the properties of objects.
Interface definitions can use public, protected, internal, and private modifiers, but almost all interfaces are defined as public, needless to say.
"the interface remains unchanged" is an important factor that should be considered. Therefore, when you add an extension from an interface, you should add a new interface instead of changing the existing interface.
Try to design the interface as a single function block. Take A. NET Framework as an example, IDisposable, IDisposable, IComparable, IEquatable, IEnumerable and so on all contain only one common method.
The uppercase "I" in front of the interface name is a convention, just as the field name begins with an underscore, adhere to these principles.
In the interface, all methods default to public.
If you expect version problems, you can create an abstract class. For example, if you have created a dog (Dog), a chicken (Chicken), and a duck (Duck), you should consider abstracting the Animal to deal with the possible occurrence of wind horses and cattle in the future. Adding new members to the interface forces all derived classes to be modified and recompiled, so the version problem is implemented by abstract classes.
A non-abstract class derived from an abstract class must include the implementation of all inherited abstract methods and abstract accessors.
The new keyword cannot be used for abstract classes and cannot be sealed because abstract classes cannot be instantiated.
You cannot use static or virtual modifiers in abstract method declarations.
These are all the contents of the article "what's the use of the .NET Interface?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.