Created
May 21, 2012 20:04
-
-
Save PeterJCLaw/2764328 to your computer and use it in GitHub Desktop.
C# protected and an assault
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
| using System; | |
| namespace Bacon | |
| { | |
| class Victim | |
| { | |
| protected int money = 100; | |
| public override string ToString() { | |
| return this.money.ToString(); | |
| } | |
| public void reclaim(Offender offender) | |
| { | |
| var diff = 100 - this.money; | |
| offender.money -= diff; | |
| this.money = 100; | |
| } | |
| } | |
| class Offender : Victim | |
| { | |
| public void assault(Victim victim) | |
| { | |
| this.money += victim.money; | |
| victim.money = 0; | |
| } | |
| } | |
| class Prog | |
| { | |
| public static void Main() | |
| { | |
| var victim = new Victim(); | |
| var offender = new Offender(); | |
| offender.assault(victim); | |
| Console.WriteLine(victim); | |
| victim.reclaim(offender); | |
| Console.WriteLine(victim); | |
| } | |
| } | |
| } | |
| /* Compiler errros: | |
| goo.cs(26,34): error CS1540: Cannot access protected member `Bacon.Victim.money' via a qualifier of type `Bacon.Victim'. The qualifier must be of type `Bacon.Offender' or derived from it | |
| goo.cs(8,23): (Location of the symbol related to previous error) | |
| goo.cs(27,20): error CS1540: Cannot access protected member `Bacon.Victim.money' via a qualifier of type `Bacon.Victim'. The qualifier must be of type `Bacon.Offender' or derived from it | |
| goo.cs(8,23): (Location of the symbol related to previous error) | |
| Compilation failed: 2 error(s), 0 warnings | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment