C#中:base的问题
usingSystem;classVehicle//定义汽车类{publicintwheels;protectedfloatweight;publicVehicle(){...
using System;
class Vehicle//定义汽车类
{
public int wheels;
protected float weight;
public Vehicle()
{
;
}
public Vehicle(int w, float g)
{
wheels = w;
weight = g;
}
public void Show()
{
Console.WriteLine("the wheel of vehicle is :{0}", wheels);
Console.WriteLine("the weight of vehicle is :{0}", weight);
}
}
class train//定义火车类
{
public int num;
private int passengers;
private float weight;
public train()
{
;
}
public train(int n, int p, float w)
{
num = n; passengers = p; weight = w;
}
public void Show()
{
Console.WriteLine("the num of train is:{0}", num);
Console.WriteLine("the weight of train is:{0}", weight);
Console.WriteLine("the passengers of train is:{0}", passengers);
}
}
class Car : Vehicle
{
int passengers;
public Car(int w, float g, int p):base(w,g)
{
wheels = w;
weight = g;
passengers = p;
}
new public void Show()
{
Console.WriteLine("the wheels of Car is:{0}", wheels);
Console.WriteLine("the weight of Car is:{0}", weight);
Console.WriteLine("the passengers of Car is:{0}", passengers);
}
}
class Test
{
public static void Main()
{
Vehicle v1 = new Vehicle(4, 5);
train t1 = new train();
train t2 = new train(10, 100, 100);
Car c1 = new Car(4, 2, 4);
v1.Show();
t1.Show();
t2.Show();
c1.Show();
Console.Read();
}
}
程序中去掉:base(w,g)
结果一样,那这一句是什么意思,具体点 展开
class Vehicle//定义汽车类
{
public int wheels;
protected float weight;
public Vehicle()
{
;
}
public Vehicle(int w, float g)
{
wheels = w;
weight = g;
}
public void Show()
{
Console.WriteLine("the wheel of vehicle is :{0}", wheels);
Console.WriteLine("the weight of vehicle is :{0}", weight);
}
}
class train//定义火车类
{
public int num;
private int passengers;
private float weight;
public train()
{
;
}
public train(int n, int p, float w)
{
num = n; passengers = p; weight = w;
}
public void Show()
{
Console.WriteLine("the num of train is:{0}", num);
Console.WriteLine("the weight of train is:{0}", weight);
Console.WriteLine("the passengers of train is:{0}", passengers);
}
}
class Car : Vehicle
{
int passengers;
public Car(int w, float g, int p):base(w,g)
{
wheels = w;
weight = g;
passengers = p;
}
new public void Show()
{
Console.WriteLine("the wheels of Car is:{0}", wheels);
Console.WriteLine("the weight of Car is:{0}", weight);
Console.WriteLine("the passengers of Car is:{0}", passengers);
}
}
class Test
{
public static void Main()
{
Vehicle v1 = new Vehicle(4, 5);
train t1 = new train();
train t2 = new train(10, 100, 100);
Car c1 = new Car(4, 2, 4);
v1.Show();
t1.Show();
t2.Show();
c1.Show();
Console.Read();
}
}
程序中去掉:base(w,g)
结果一样,那这一句是什么意思,具体点 展开
2个回答
展开全部
public Car(int w, float g, int p):base(w,g)
{
wheels = w;
weight = g;
passengers = p;
}
上面这个构造函数有重复操作,当然就成了你说的去掉:base(w,g)结果都一样。
因为wheels = w; weight = g; 这两句在基类构造函数中已经执行,也就是
:base(w,g)就已经执行wheels = w; weight = g;这两句,结果你在上面的Car构造函数里又执行了一次。
上面的函数应该写为:
public Car(int w, float g, int p):base(w,g)
{
passengers = p;
}
你在试试去掉与不去掉的效果
{
wheels = w;
weight = g;
passengers = p;
}
上面这个构造函数有重复操作,当然就成了你说的去掉:base(w,g)结果都一样。
因为wheels = w; weight = g; 这两句在基类构造函数中已经执行,也就是
:base(w,g)就已经执行wheels = w; weight = g;这两句,结果你在上面的Car构造函数里又执行了一次。
上面的函数应该写为:
public Car(int w, float g, int p):base(w,g)
{
passengers = p;
}
你在试试去掉与不去掉的效果
追问
那如果函数中还有父类的父类,也有带两个参数的构造函数,则:base 是调用父类中的构造函数还是父类的父类中的呢
追答
一级一级地往上调用啊,父类构造里再调用父类的父类的构造函数啊
展开全部
构造子类需要调用父类的构造函数,通过:base 可以调用带有参数的指定的构造函数
本例为
new Vehicle(w,g);
去掉:base(w,g),默认调用
new Vehicle();
因为Vehicle(w,g)只是初始化wheels和weight,而Car(int w, float g, int p)中也初始化了wheels和weight,所以结果一样。
如果改成
public Car(int w, float g, int p):base(w,g)
{
passengers = p;
}
就可以看出差别了
如果删除
public Vehicle()
{
;
}
去掉:base(w,g)则会报错
本例为
new Vehicle(w,g);
去掉:base(w,g),默认调用
new Vehicle();
因为Vehicle(w,g)只是初始化wheels和weight,而Car(int w, float g, int p)中也初始化了wheels和weight,所以结果一样。
如果改成
public Car(int w, float g, int p):base(w,g)
{
passengers = p;
}
就可以看出差别了
如果删除
public Vehicle()
{
;
}
去掉:base(w,g)则会报错
追问
那如果函数中还有父类的父类,也有带两个参数的构造函数,则:base 是调用父类中的构造函数还是父类的父类中的呢
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询