Version processing in C #
Now I want to talk about version processing in C. in fact, this is a problem that must be considered by any software. each software has more than one version (except what I wrote), so version processing is very important. JAVA has handled this problem well, and I personally think C # has used JAVA's processing method for reference, so it is also doing well.
In C #, if you use the virtual keyword when declaring a method, you can use the override or new keyword in the derived class to discard or ignore it. if you use the virtual keyword in the parent class and do not use the override or new keyword in the derived class, but directly reference a method with the same name, the compiler will report an error, the new method is used to ignore the methods in the derived class. the following example can help you understand:
000: // Versioningversioning. cs
001: public class MyBase
002 :{
003: public virtual string Meth1 ()
004 :{
005: return "MyBase-Meth1 ";
006 :}
007: public virtual string Meth2 ()
008 :{
009: return "MyBase-Meth2 ";
010 :}
011: public virtual string Meth3 ()
012 :{
013: return "MyBase-Meth3 ";
014 :}
015 :}
016:
017: class MyDerived: MyBase
018 :{
019: public override string Meth1 ()
020 :{
021: return "MyDerived-Meth1 ";
022 :}
023: public new string Meth2 ()
024 :{
025: return "MyDerived-Meth2 ";
026 :}
027: public string Meth3 () // The system will have a warning here and the method Meth3 () will be hidden ()
028:
029:
030 :{
031: return "MyDerived-Meth3 ";
032 :}
033:
034: public static void Main ()
035 :{
036: MyDerived mD = new MyDerived ();
037: MyBase mB = (MyBase) mD;
038:
039: System. Console. WriteLine (mB. Meth1 ());
040: System. Console. WriteLine (mB. Meth2 ());
041: System. Console. WriteLine (mB. Meth3 ());
042 :}
043 :}
Output:
MyDerived-Meth1
MyBase-Meth2
MyBase-Meth3
Obviously, the output of the last two new keywords is the output of the methods in the parent class. Therefore, we can see that if the new keyword is used in previous versions, the previous method is used instead of the content of the current method. the role of virtual methods is the opposite. If there is such a method in the parent class, use the method content I have written to get rid of the previous ones! However, it seems that it is not good to use new here. It is a misunderstanding (bad, it's time to beat me again! & * % $ #@).
If you delete row 037th, change all mB in row 039-041 to mD, and change the output:
MyDerived-Meth1
MyDerived-Meth2
MyDerived-Meth3
What does this mean? It means that the objects of the derived class will take effect only when they are reshaped by the parent class. well, this is indeed a bit difficult to understand. You only need to do it yourself to figure out the relevant authorities. The so-called "practice is the only criterion for testing C #", haha!
In C #, You can freely Add a method to a derived class or overwrite the parent class method, as shown below, which is very simple:
Class Base {}
Class Derived: Base
{
Public void F (){}
}
And:
Class Base
{
Public void F (){}
}
Class Derived: Base
{
Public void F (){}
}
Okay, this section is over. See you tomorrow!