Created
January 2, 2015 02:55
-
-
Save NickersF/b50c808fa1608b42374c to your computer and use it in GitHub Desktop.
Basic class inheritance uses in C#.NET
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; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace inheritance | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Car myCar = new Car(); | |
myCar.Make = "BMW"; | |
myCar.Model = "BigOne"; | |
myCar.Year = 1999; | |
myCar.Color = "Black"; | |
printVehicleDetails(myCar); | |
Truck myTruck = new Truck(); | |
myTruck.Make = "Ford"; | |
myTruck.Model = "F950"; | |
myTruck.Color = "Black"; | |
myTruck.Year = 2011; | |
myTruck.towingCapacity = 1200; | |
printVehicleDetails(myTruck); | |
Console.ReadLine(); | |
} | |
private static void printVehicleDetails(Vehicle vehicle) | |
{ | |
Console.WriteLine("Here are the car's details: {0}", | |
vehicle.formatMe()); | |
} | |
} | |
abstract class Vehicle | |
{ | |
public string Model { get; set; } | |
public string Make { get; set; } | |
public int Year { get; set; } | |
public string Color { get; set; } | |
public abstract string formatMe(); | |
} | |
class Car : Vehicle | |
{ | |
public override string formatMe() | |
{ | |
return String.Format("{0} - {1} - {2} - {3}", | |
this.Model, | |
this.Make, | |
this.Year, | |
this.Color); | |
} | |
} | |
class Truck : Vehicle | |
{ | |
public int towingCapacity { get; set; } | |
public override string formatMe() | |
{ | |
return String.Format("{0} - {1} - {2} Towing units.", | |
this.Model, | |
this.Make, | |
this.towingCapacity); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment