Advertisement
WilleMahMille

Exempel - Klassen Tåg

Oct 18th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.38 KB | None | 0 0
  1.     class Program {
  2.         static void Main(string[] args) {
  3.             Tåg tåg = new Tåg("Göteborg", 140, 5);
  4.             //all the information available from tåg
  5.             Console.WriteLine("Tåg\nDestination: " + tåg.destination + "\nMaxhastighet: " + tåg.MaxHastighet());
  6.             Console.ReadKey(true);
  7.         }
  8.     }
  9.  
  10.     public class Fordon {
  11.         public Fordon(string destination, int hastighet) {
  12.             this.destination = destination;
  13.             this.hastighet = hastighet;
  14.         }
  15.         public virtual int MaxHastighet() {
  16.             return hastighet;
  17.         }
  18.         public string destination { get; }
  19.         int hastighet;
  20.     }
  21.  
  22.     public class Tåg : Fordon {
  23.         public Tåg(string destination, int hastighet, int antalVagnar) : base(destination, hastighet) {
  24.             this.antalVagnar = antalVagnar;
  25.         }
  26.         public override int MaxHastighet() {
  27.             if(antalVagnar >= 1 && antalVagnar <= 3) {
  28.                 return 125;
  29.             }
  30.             if(antalVagnar >= 4 && antalVagnar <= 7) {
  31.                 return 110;
  32.             }
  33.             return 95;
  34.         }
  35.         public int antalVagnar { get {
  36.                 return antalVagnar;
  37.             }
  38.             set {
  39.                 //inline if-statement
  40.                 antalVagnar = value > 1 ? value : antalVagnar;
  41.             }
  42.         }
  43.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement