The problem arises:
When we use the abstract classes and interfaces of C #, we often encounter the following similar problems, broadly summarized as follows:
(1) What are the essential differences and connections between abstract classes and interfaces?
(2) When to choose the use of abstract classes, then when to use the interface is most appropriate?
(3) How to use the project in order to make the project more maintainable, extensibility? How to combine it with the struct, the class tightly, achieves the final double-edged sword action?
Solution:
This is also the problem I encountered when learning abstract classes and interfaces, from the three questions I summed up, it is not difficult to see that this is perhaps the three stages that most of our programmers are experiencing problems,
The first stage (basic concept): Just like question 1, this part of the people need first to clear the basic concept of the barrier, first of all to understand what is called abstract class, what is called interface?
Then what is the difference between an abstract class and an interface and what is the connection? Of course, this may take a while to understand and practice, after all, these concepts are more abstract, belong to the kind of things that can not be seen, of course, the most important or more practice, nothing to do a demo instance, they are used again, in the process of using more think about why to use this? What is the benefit of this? Can you use interfaces, and if not, where are the benefits of using abstract classes? This can deepen the understanding of them, this is my little experience it, hehe! Said so much, I would like to summarize the question 1, one is to facilitate their own mind, the second is to deepen understanding it.
Abstract class and interface concepts: In fact, these concepts in textbooks and blogs basically a large pile, the predecessors summed up is also very good, but may be in the popular, easy to understand a bit obscure, I will translate, add Shaanxi version of the vernacular, hey.
(1) Abstract class: Provides a set of derived classes to access shared base class public methods;
Abstract classes are characterized by: (1) Abstract classes include both abstract and method implementations, and (2) abstract classes cannot be instantiated or sealed; (3) abstract methods in abstract classes are either implemented in derived classes or inherited by derived abstract classes (abstract derived classes can inherit the base class abstract method). If you want to implement an abstract method of a base class in a derived class, you must use the override modifier; (4) The abstract class belongs to the single inheritance (this is the same-sex of all Classes) (5) Abstract class is an abstraction of an ethnic group, similar to is-a;
The above I if not very clear, give you an official website about abstract class address: Https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/abstract
(2) Interface: An abstract type containing a set of virtual methods;
The characteristics of the interface are: (1) Only the definition of the virtual method is included in the interface, only the declaration definition, no function implementation, (2) The interface class can include attributes, events, indexers, etc., but cannot include fields, (3) The interface class is multi-inheritance, (4) inherits the interface of the class must all implement the interface method
If you want to know the official website about the interface description, give you an address: Https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/interface
Abstract classes and interfaces differ and relate to:
The same point: (1) can not be directly instantiated, can only be achieved by means of inheritance;
(2) is the abstract of the behavior and objects of things, forming a certain pattern of design;
Different points:
(1) The interface supports multiple inheritance, and abstract classes cannot implement multiple inheritance;
(2) Interfaces include methods, properties, events, indexers, cannot include fields, abstract classes can include fields, or they can include the implementation of methods;
(3) Interfaces can support callbacks, abstract classes do not support callbacks
(4) An interface can be a base class for value types and reference types, whereas an abstract class can only be used as a base class for reference types;
Phase II (Use phase): Just like question 2, this part of the people have a certain understanding of the foundation, but is a lack of certain practice, perhaps is to do a simple demo, then when to use abstract class, when to use the interface?
Analysing the second question, I propose 3 points:
The first suggestion, the basic concept is not just the concept of memory, to practice more, more thinking, and then more practice, then think, cycle several times, until cooked rotten in the heart;
The second suggestion, as far as possible in their own projects to use this knowledge, to use it, you can find problems, solve problems, will think;
The third proposal summarizes and sums up the knowledge points of the abstract classes and interfaces that you have used in the project.
When using abstract classes and interfaces, I summarize the experience of predecessors, give the following points, for reference only:
(1) When a component is designed to have multiple versions in the future, generally use abstract classes, such as designing DB DB in C #, you may be using SQL Server, MySQL, and later large projects may use ORACLE,DB, a large database system, So when we design the class, we design an abstract base class db, and let it have some common properties and methods of data, properties: Database connection name, version, database type, database common method: Open (), Close () method, etc.;
(2) When the design of the components to support common behavior action, you can consider the interface, for example, birds, humans, cars can have sound, this time you can design the interface, including the function of the called behavior, and then in each specific class implementation;
(3) in the derived class or interface that inherits the interface, once the interface needs to increase the behavior method is a more troublesome thing, must all the inheritance must implement its method, this time can be implemented in the derived class A new interface, to implement the unique action of the derived class, for example:
<summary>
Implements a reptile's action interface
</summary>
Interface Ianimalclimb
{
void Climb ();
}
<summary>
Implement an action interface for an animal that can be called
</summary>
Interface Icry
{
void Cry ();
}
<summary>
Implement an animal abstract class
</summary>
Public abstract class Animal
{
The name of the animal
public string Name {get; set;}
The color of the animal
public string Color {get; set;}
Common methods of animal abstract classes
public abstract void Sleep ();
public abstract void Breathe ();
}
<summary>
Define birds, the general method is to fly
</summary>
public class Bird:animal,icry
{
public override void Sleep ()
{
Console.WriteLine ("Bird derived class inherits the sleep () method of the base class");
}
public override void Breathe ()
{
Console.WriteLine ("Bird derived class inherits the Breathe () method of the base class");
}
Birds can inherit a unified interface action, for example: Call
public void Cry ()
{
Console.WriteLine ("Bird derived class inherits the method called by the interface Icry");
}
}
<summary>
Defining Reptile Classes
</summary>
public class Snake:animal, ianimalclimb
{
public override void Breathe ()
{
Console.WriteLine ("Snake derived class inherits the sleep () method of the base class");
}
public override void Sleep ()
{
Console.WriteLine ("Snake derived class inherits the sleep () method of the base class");
}
Reptiles can inherit a unified interface animal, for example: crawling
public void Climb ()
{
Console.WriteLine ("Snake derived class inherits the method of crawling of the Interface Ianimalclimb");
}
}
The above code, just explain the problem, relatively simple;
Phase III (Optimization phase): Just like problem 3, when we are doing an abstract class or interface, the first thing we consider is the ability to use the line, the result is that the definition of the class or interface is more difficult to maintain and expand, or there is a intersection between the classes, how to optimize the inheritance relationship? How can you make a program maintainable and extensible?
I personally recommend that you have the following aspects in order to:
(1) to have a solid foundation of knowledge and deep foundation skills;
(2) to have a more asked, Dauth heart; for abstract classes and interfaces ask, why not use an abstract class and use an interface? Why is it appropriate to use the interface in this place?
(3) Take a look at the predecessors how to design the interface and class, this aspect of the information on the Internet search a lot;
(4) Personal advice to look more at design patterns in this area of knowledge, because they are predecessors in the design of experience and ideas;
The above views and statements, just on behalf of my personal opinions and suggestions, if there are good ideas, we can communicate with each other, rookie will listen to your comments and suggestions.
Two double-edged swords in C #: abstract Classes and interfaces