About C #, inheritance rewriting and hiding the basic class method!

xiaoxiao2021-03-06  65

Using

System;

Using

System.collections;

public

Class

NEVERMORE60CUSTOMER: CUSTOMER

...

{Public override string GetFunnyString () ... {return "Nevermore60.Nevenmore!";} Public class Customer ... {public virtual string GetFunnyString () ... {return "Plain ordinary customer.Kaark!";} Public class SamplesArray ... {public static void Main () ... {Customer Cust1 = new Nevermore60Customer (); Nevermore60Customer Cust2; // Cust1 = new Customer (); Console.WriteLine ( "Customer referencing Customer." Cust1.GetFunnyString ( )); Cust1 = new Nevermore60Customer (); Console.WriteLine ( "Customer referencing Nevenmore60Customer:" Cust1.GetFunnyString ()); Cust2 = new Nevermore60Customer (); Console.WriteLine ( "Nevenmore60Customer referencing" Cust2.GetFunnyString ()) }}}}}

Customer Cust1 = new nevermore60customer (); if it is overwritten:, if the declared base class object, point to derived class, then call the method of the method of fundamental rewriting in the derived class (as shown above) If it is a hidden base class method: public new string getFunnystring () {return "nevermore60.nevenmore!";}: Customer Cust1 = new nevermore60customer (); At this time, Cust1 call is still a getFunNystring method in the base class when declaring. But the two declarations and assignments are not in the same class, and this problem will not happen.

转载请注明原文地址:https://www.9cbs.com/read-85477.html

New Post(0)