Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program {
- static void Main(string[] args) {
- Tåg tåg = new Tåg("Göteborg", 140, 5);
- //all the information available from tåg
- Console.WriteLine("Tåg\nDestination: " + tåg.destination + "\nMaxhastighet: " + tåg.MaxHastighet());
- Console.ReadKey(true);
- }
- }
- public class Fordon {
- public Fordon(string destination, int hastighet) {
- this.destination = destination;
- this.hastighet = hastighet;
- }
- public virtual int MaxHastighet() {
- return hastighet;
- }
- public string destination { get; }
- int hastighet;
- }
- public class Tåg : Fordon {
- public Tåg(string destination, int hastighet, int antalVagnar) : base(destination, hastighet) {
- this.antalVagnar = antalVagnar;
- }
- public override int MaxHastighet() {
- if(antalVagnar >= 1 && antalVagnar <= 3) {
- return 125;
- }
- if(antalVagnar >= 4 && antalVagnar <= 7) {
- return 110;
- }
- return 95;
- }
- public int antalVagnar { get {
- return antalVagnar;
- }
- set {
- //inline if-statement
- antalVagnar = value > 1 ? value : antalVagnar;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement