The difference between c#override and the new modifier (reproduced)

Source: Internet
Author: User

C # is more than Java a new hidden feature. C # Override overrides a method rewrite that is equivalent to no keyword in java. So the method in Java has no hidden functionality.

C # Override overrides means overriding a virtual method (tag virtual) or abstract method (marked abstract) in a parent class to implement new functionality that must exactly match the signature of the parent class method, and that the accessibility of the parent class method must be consistent with the new hidden Is the same method of redefining a signature in a subclass as a method of the parent class, which can be used without a new modifier, except that a warning message pops up at compile time: if it is intentionally hidden, use the keyword new.

1 usingSystem; 2  Public classTest3 {     4      Public Static voidMain (string[] args) {5A a=NewB (); 6Console.Write (A.getval ());//output: A Although the instance is B, but the method of a is executed7     8A=NewC (); 9Console.Write (A.getval ());//output: The C instance is C, and C overrides the method of a, so the method of C is executedTen      OneB b=NewB ();  AConsole.Write (B.getval ());//Output: b The method of B is only executed if both the definition and the instance are B . -     }       - }      the classA - {      -     //to allow subclasses to be overridden, the method must be identified as virtual or abstract -      Public VirtualString getval () { +         return "A";  -     }       + }       A classb:a at {      -     //The hidden parent method is identified with the new, a compile warning is not identified, and does not affect the use of the -      Public NewString getval () { -         return "B";  -     }       - }       in classc:a - {      to     //The override method must be identified with the override +      Public OverrideString getval () { -         return "C";  the     }       *}

In Java, there is no concept of hiding the parent class method, Java has only the concept of overriding methods, and the same code, executed in Java and executed in C # is completely different:

1 classTest2 {     3      Public Static voidMain (string[] args)4     {     5A a=NewB (); 6 //output: The B instance is B, and the method of B is executed (in C #, this should output a)7 System.out.println (A.getval ()); 8A=NewA (); 9 //output: A method to execute a only if both the definition and the instance are aTen System.out.println (A.getval ());  One     }      A }      - classA - {      the      PublicString getval () { -         returnA;  -     }       - }       + classBextendsA - {      +      PublicString getval () { A         returnB;  at     }       -}

PS: Reproduced from 51CTO only for my study and use

The difference between c#override and the new modifier (reproduced)

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.