SUNWEN tutorial ---- C # advanced (6)

Source: Internet
Author: User

Hello everyone, I am SUNWEN from Wuhan Hashi. it is now at on July 27, May 2. I wrote it again because I had nothing to do. I took over a dozen photos yesterday and will be able to fetch them later. I wonder if I am handsome in the photo! Now I am listening to the 2000 European Cup music in my ear, so I am very excited. Alas, as a 2000-level football leader in our school, I have not played football for a long time!

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 (){}
}
Now, the music is over, but I have to rest. See you tomorrow!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.