Last active
December 15, 2015 20:40
-
-
Save s2kw/5320020 to your computer and use it in GitHub Desktop.
virtual -> override
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Base | |
{ | |
public virtual void Test(){ | |
Debug.print("Base"); | |
} | |
} | |
class Derived:Base | |
{ | |
public override void Test(){ | |
Debug.print("Derived"); | |
} | |
} | |
class VirtualTest | |
{ | |
public void M() | |
{ | |
Base base1 = new Base(); | |
base1.Test();//Base | |
Base base2 = new Derived(); | |
base2.Test();//Derived | |
Derived derived1 = new Derived(); | |
derived1.Test();//derived | |
/* これはどうなる? */ | |
// Derived derived2 = new Base(); | |
/* | |
error CS0266: Cannot implicitly | |
convert type `Base' to `Derived'. | |
An explicit conversion exists (are you missing a cast?) | |
*/ | |
// derived2.Test(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment