Casting Objects
using System;
public class MotorVehicle {
public string model;
public MotorVehicle(string model) {
this.model = model;
}
public void Start() {
Console.WriteLine(model + " started");
}
}
public class Product : MotorVehicle {
public bool convertible;
public Product(string model, bool convertible) :
base(model) {
this.convertible = convertible;
}
}
public class Motorcycle : MotorVehicle {
public bool sidecar;
public Motorcycle(string model, bool sidecar) :
base(model) {
this.sidecar = sidecar;
}
public void PullWheelie() {
Console.WriteLine(model + " pulling a wheelie!");
}
}
class MainClass {
public static void Main() {
Product myProduct = new Product("MR2", true);
MotorVehicle myMotorVehicle = (MotorVehicle)myProduct;
Console.WriteLine("myMotorVehicle.model = " + myMotorVehicle.model);
myMotorVehicle.Start();
Motorcycle myMotorcycle = new Motorcycle("V-Rod", true);
MotorVehicle myMotorVehicle2 = (MotorVehicle)myMotorcycle;
Console.WriteLine("myMotorVehicle2.model =" + myMotorVehicle2.model);
myMotorVehicle2.Start();
Motorcycle myMotorcycle2 = (Motorcycle)myMotorVehicle2;
Console.WriteLine("myMotorcycle2.model = " + myMotorcycle2.model);
Console.WriteLine("myMotorcycle2.sidecar = " + myMotorcycle2.sidecar);
myMotorcycle2.Start();
myMotorcycle2.PullWheelie();
}
}
|
HTML code for linking to this page:
Related in same category :
-
-
-
|