Advertisement
social1986

Untitled

Feb 27th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 KB | None | 0 0
  1. public interface ICar
  2. {
  3.     string Model { get; }
  4.     string Color { get; }
  5.     string Start();
  6.     string Stop();
  7. }
  8.  
  9. public interface IElectricCar : ICar
  10. {
  11.     int Battery { get;}
  12. }
  13.  
  14. public abstract class Car : ICar
  15. {
  16.  
  17.     public Car(string model, string color)
  18.     {
  19.         this.Model = model;
  20.         this.Color = color;
  21.     }
  22.  
  23.     public string Model { get; set; }
  24.     public string Color { get; set; }
  25.  
  26.     public virtual string Start()
  27.     {
  28.         return "Engine start";
  29.     }
  30.  
  31.     public virtual string Stop()
  32.     {
  33.         return "Breaaak!";
  34.     }
  35.  
  36.     public override string ToString()
  37.     {
  38.         return $"{this.Color} {this.GetType().Name} {this.Model}";
  39.     }
  40. }
  41.  
  42. public abstract class ElectricCar : Car, IElectricCar
  43. {
  44.     public ElectricCar(string model, string color, int batteries)
  45.         :base(model, color)
  46.     {
  47.         this.Battery = batteries;
  48.     }
  49.     public int Battery { get; private set; }
  50. }
  51.  
  52. using System;
  53. using System.Text;
  54.  
  55. public class Seat : Car
  56. {
  57.     public Seat(string model, string color)
  58.         : base(model, color)
  59.     {
  60.     }
  61.  
  62.     public override string ToString()
  63.     {
  64.         var sb = new StringBuilder(base.ToString() + Environment.NewLine);
  65.         sb.AppendLine(Start());
  66.         sb.AppendLine(Stop());
  67.         return sb.ToString();
  68.     }
  69. }
  70.  
  71. using System.Text;
  72.  
  73. public class Tesla : ElectricCar
  74. {
  75.     public Tesla(string model, string color, int batteries)
  76.         :base(model, color, batteries)
  77.     {        
  78.     }
  79.  
  80.     public override string ToString()
  81.     {
  82.         var sb = new StringBuilder();
  83.         sb.AppendLine($"{base.ToString()} with {this.Battery} Batteries");
  84.         sb.AppendLine(Start());
  85.         sb.AppendLine(Stop());
  86.         return sb.ToString().TrimEnd();        
  87.     }  
  88. }
  89.  
  90.  
  91.        Car seat = new Seat("Leon", "Grey");
  92.        Car tesla = new Tesla("Model 3", "Red", 2);
  93.  
  94.        Console.WriteLine(seat);
  95.        Console.WriteLine(tesla);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement