The----of the Sunwen Tutorial C # advanced 6

Source: Internet
Author: User
Tutorial Sunwen Tutorial----C # Advanced
Six
Mrfat@china.com
Hello, I am Sunwen of Wuhan Hua Division. It is May 2 night 19:27, because there is nothing to do, all again write up. Yesterday took more than 10 photo, will be able to go to pick up, I do not know the photo is not very handsome, hehe! Now I am listening to the 2000 European Cup music, so I am more excited. Alas, As the 2000-level football team in our college, I have not played football for a long time!

Now I'm talking about version processing in C #. In fact, this is a problem that any software must consider. There are more than one version of each software (except what I write), so versioning is very important. Java handles this very well, and I personally think that C # draws on Java's approach, so it's done well.

In C #, if you use the keyword virtual when declaring a method, in a derived class, you can discard it or ignore it by using the override or the new keyword. If you use the keyword virtual in the parent class, Instead of using the override or New keyword in a derived class and referencing a method that has the same name directly, the compiler will complain and will run in new ways, that is, to ignore the methods in the derived class. The following examples can help you understand:


M://Versioning\versioning.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 will hide the method Meth3 ()
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
It can be clearly seen, the output of the latter two new keywords is the output of the methods in the parent class, so it can be seen that the key to new is that if you had this method in the previous version, you would follow the previous method without my current method content. And the virtual method works just the opposite, Its role is if there is such a method in the parent class, then use the content of the method I write now, let the former fuck off! However, it's not good to use new here, it's misleading (bad,!&*%$#@ is going to hit me again).

If you remove line No. 037, change the MB in 039-041 to MD, and the output becomes:

Myderived-meth1
Myderived-meth2
Myderived-meth3

And what does that mean? shows that the override and new keyword will not take effect until the object of the derived class is reshaped by the parent class. Oh, this is a bit difficult to understand, we only have to do their own, in order to find out this organ, the so-called "practice is to test the only standard of C #", haha!

In C #, you are free to add a method to a derived class, or to overwrite a parent class, as shown below, very simply:

Class Base {}
Class Derived:base
{
public void F () {}
}
And:

Class Base
{
public void F () {}
}
Class Derived:base
{
public void F () {}
}
Well, this section is over, the music is not finished, but I really want to rest, see you tomorrow!




Next page


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.