Last active
August 2, 2016 15:09
-
-
Save mrtank/a8888fa630ae285259d7c1913b851a4a to your computer and use it in GitHub Desktop.
This file contains 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.Collections.Generic; | |
namespace test | |
{ | |
public class Something | |
{ | |
public Something() | |
{ | |
OrderableItems c1 = new Car(200); | |
OrderableItems b1 = new Bus(31); | |
List<OrderableItems> items = new List<OrderableItems> { c1, b1 }; | |
// c1 is an object, it needs cast to show value of BHP | |
// but if I use my custom ToString method I will be able to get the value | |
SomethingCommon bhp = items[0].GetSomethingCommon(); | |
} | |
} | |
} | |
class OrderableItems | |
{ | |
public virtual SomethingCommon GetSomethingCommon() | |
{ | |
// default for all OrderableItems. | |
// Check abstract too. | |
return new SomethingCommon(0); | |
} | |
} | |
public class SomethingCommon | |
{ | |
private int _someMeasurer; | |
public SomethingCommon(int someMeasurer) | |
{ | |
_someMeasurer = someMeasurer; | |
} | |
} | |
class Car: OrderableItems | |
{ | |
public Car(int bhp) | |
{ | |
BHP = bhp; | |
} | |
public int BHP { get; private set; } | |
public override SomethingCommon GetSomethingCommon() | |
{ | |
// fun | |
return new SomethingCommon(BHP); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment