111,119
社区成员
发帖
与我相关
我的任务
分享using System;
using System.Collections.Generic;
using System.Text;
namespace Test1
{
class Vehicle
{
public int wheels;
protected float weight;
public Vehicle() { }
public Vehicle(int w, float g)
{
wheels = w;
weight = g;
}
public virtual void Speak()
{
Console.WriteLine("the w vehicle is speaking!");
}
}
class car : Vehicle
{
int passengers;
public car(int w, float g, int p)
{
passengers = p;
}
public override void Speak()
{
Console.WriteLine("The car is speaking:Di-di!");
}
}
class Truck : Vehicle
{
int passengers;
float load;
public Truck(int w, float g, int p, float l)
{
passengers = p;
load = l;
}
public override void Speak()
{
Console.WriteLine("The truck is speaking:Ba-ba!");
}
}
class Test1
{
public static void Main()
{
Vehicle v1 = new Vehicle(2, 3);
car c1 = new car(4, 2, 5);
Truck t1 = new Truck(6, 5, 3, 10);
v1.Speak();
v1 = c1;
v1.Speak();
c1.Speak();
v1 = t1;
v1.Speak();
t1.Speak();
Console.ReadKey();
}
}
}
public car(int w, float g, int p)
: base(w, g)
{
passengers = p;
}