This article is a mirror article of machine translation, please click here to jump to the original article.

View: 19862|Reply: 1

[Source] .net/c# feature inheritance in detail

[Copy link]
Posted on 2/14/2017 11:13:38 AM | | |
Description:

There are two classes, A and B, I A inherits class B, and then B is the parent class

I have set the TestAttribute properties in class A and class B respectively...

Then, take the characteristics of the object, and the code is as follows:



I found that the number of results is 1, and the attribute is the attribute set by A, not the attribute set by B, let alone merged, why is this???


[Help("BaseClass")] publicclass Base{} publicclass Derive : Base{}
There are four possible combinations here:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false ]  
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false ]  
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true ]  
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true ]  
The first case:

If we query the Derive class (we'll see later how to query a class's properties at runtime), we'll find that the Help feature doesn't exist because the inherited property is set to false.

Second case:

Same as in the first case, because inherited is also set to false.



The third situation:

To explain the third and fourth cases, let's first add some code to the derived class:

[Help("BaseClass")] publicclass Base{}[Help("DeriveClass")] publicclass Derive : Base{}
Now let's query the Help feature, we can only get the properties of the derived class, because inherited is set to true, but AllowMultiple is set to false. Therefore, the Help feature of the base class is overridden by the help feature of the derived class.

The fourth situation:

Here we will find that the derived class has both the Help feature of the base class and its own Help feature, because AllowMultiple is set to true.







Previous:C# assignment between two object properties
Next:Uncaught TypeError: a(...). parents(...). andSelf is not a function
 Landlord| Posted on 3/30/2017 9:35:49 AM |
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com