Actually write this articleArticleMy own ideas are not so reliable, but I want to write my own ideas. The theme is of course inheritance.
As we all know, C # does not support multiple inheritance. A class can only inherit one parent class at the same time, but can inherit multiple interfaces at the same time. Therefore, when standardizing a class, we need to weigh whether to write an abstract class as its parent class or write an interface as its interface. What is the difference between an abstract class and an interface that can be searched through the book. Abstract classes can provide implementation of a method, but interfaces cannot. abstract classes can define their own attributes, while interfaces can only declare static fields. Interfaces cannot write access modifiers, in the end, they are all public and abstract classes are random.
Although the differences have been listed many times, in general, there is one sentence: interfaces are more conducive to standardizing the behavior of a class, while abstract classes are more inclined to standardize the attributes of a class.
For example, if I want to write a class for all people in the world, I 'd better write an abstract class because people have many attributes, such as gender, age, and name, the interface cannot do this. So what methods should be written in this category? This needs to be studied. The political book says, "The difference between humans and animals is that humans can make and use tools ", the biological book says "Humans are animals that can walk upright", but I think these things are put into this basic base class (this class directly inherits from the object, it is not good to consider animal or other things. Therefore, from my personal understanding, these things can be put into the interface, it's a big deal to let a required class inherit that interface.
I thought, what are the common behaviors of humans? well, by the way, all people will go down. So I wrote an interface called idieable, which is a method called Die (), then I asked human to inherit this interface,CodeIt looks like this:
Code
1 Public Abstract Class Human: idieable
2 {
3 Private Int M_age;
4 Public Virtual Int Age
5 {
6 Get {ReturnM_age ;}
7 Set
8 {< br> 9 If (value = 0 )
10 m_age = value;
11 }
12 }
13 Public Abstract Sex {Get;Set;}
14 Public Virtual String Name {Get;Set;}
15
16 Idieable members # Region Idieable members
17
18 Public Virtual Void Die ()
19 {
20Console. writeline (name+"Die");
21}
22
23 # Endregion
24 }
25
26 Public Interface Idieable
27 {
28VoidDie ();
29}
30
31 Public Enum Sex: Byte
32 {
33Male= 0,
34Famale= 1
35}
Next I want to derive a subclass which is used to describe a female student. What should I do? Gender is a property of human beings. Occupation is also a property of a normal person, so I should create an abstract class "student" that inherits from human and then "female student" that inherits "student" and then changes its sex attributes, or should we create an abstract class "female" inherited from human and then write an interface as "Learning" and make "female student" inherited from the abstract class "female" and "Learning" respectively? Both of them are justified. I wrote their code separately:
First, virtual type
Code
Public Abstract Class Studet: Human
{
Public Virtual Void Study ()
{
Console. writeline (name + " Is studying! " );
}
}
Public Class Famalestudent: studet
{
Public Override Sex
{
Get
{
Return Sex. famale;
}
Set
{
Throw New Notsupportedexception ();
}
}
}
Second, virtual class and Interface Type
Code
Public Abstract Class Famale: Human
{
Public Override Sex
{
Get
{
Return Sex. famale;
}
Set
{
Throw New Notsupportedexception ();
}
}
}
Public InterfaceIstudyable
{
VoidStudy ();
}
Public ClassFamalestudent: famale, istudyable
{
# RegionIstudyable members
Public VoidStudy ()
{
Console. writeline (name+ "Is studying!");
}
# Endregion
}
which of the two is better? I don't know myself. After so many years of learning and using it for so many years, I am still confused about object orientation, I have almost memorized the things in those textbooks, but I don't know how to use them. As mentioned above, the first method saves one interface, which is concise and clear. The second method standardizes behavior with interfaces, which is intuitive. If I really want to pick it up, I still look at the first one. What is the reason? I can't say it myself. I wrote this article today and hope someone can give me some advice, I don't want to get answers like "Everything works" or "one person's programming habits ......