using System;
using System.Collections.Generic;
using System.Text;namespace Test1
{
class Vehicle
{
public int wheels;
protected float weight;
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)
: base(w, g)
{
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)
: base(w, g)
{
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 Truck(int w, float g, int p, float l): base(w, g)为什么要接上基类base(w, g)??
using System.Collections.Generic;
using System.Text;namespace Test1
{
class Vehicle
{
public int wheels;
protected float weight;
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)
: base(w, g)
{
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)
: base(w, g)
{
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 Truck(int w, float g, int p, float l): base(w, g)为什么要接上基类base(w, g)??
public Truck(int w, float g, int p, float l): base(w, g)
表示显式调用父类的构造函数
运行是将先执行父类构造函数,然后才执行子类构造函数的主题
如
public Truck(int w, float g, int p, float l): base(w, g)
改为
public Truck(int w, float g, int p, float l)
行运时也会调用基类的构造函数public Vehicle(int w, float g)
{
wheels = w;
weight = g;
}
public car(int w, float g, int p)
{
Vehicle(w, g);//虽然不能这么写
passengers = p; }
接着运行public Vehicle(int w, float g)
如果没有显示调用,则只运行 public Vehicle(int w, float g)
是不是啊???
public car(int w, float g, int p)
: base(w, g)
{
passengers = p; }
调用了基类的构造函数来构造w,g.
自己只需要写代码构造p代码重用撒
但是你的这个程序不写就报错了,因为你的基类没有提供无参的默认构造函数,不写的话就是调用默认的无参构造函数,可是你的基类没有提供(写了有参的构造函数,默认无参构造函数就不存在了,需要手动添加),所以就报错了。下面的这个程序也可以运行。
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();
}
}
}