C# c中构造函数重载和链接的区别#
这是我的第一周OOP,所以我退出新的,道歉,如果问题是在基础上,因为我不能得到我的头周围。我不理解构造函数重载和链接之间的区别。我正在使用以下父类:C# c中构造函数重载和链接的区别#,c#,oop,C#,Oop,这是我的第一周OOP,所以我退出新的,道歉,如果问题是在基础上,因为我不能得到我的头周围。我不理解构造函数重载和链接之间的区别。我正在使用以下父类: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Vehicle_Application { abstract class Vehicle {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Vehicle_Application
{
abstract class Vehicle
{
private string color;
public string Color { get { return color; } set { color = value; }}
private double fuelTankSize;
public double FuelTankSize { get {return fuelTankSize;} set { fuelTankSize = value;}}
private double efficiency;
public double Efficiency { get { return efficiency; } set { efficiency = value; } }
private double fullTankMileage = 100;
public double milesPerGalon;
public void calculateMPG()
{this.milesPerGalon = (fullTankMileage/(fuelTankSize/4))* efficiency;}
}
}
从中驾驶以下子级“汽车”:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Vehicle_Application
{
class Car : Vehicle
{
public Car(string color = null)
{ this.Color = color; }
public int NumberOfTyres;
public Car(string color, int noOfTyres, double fuelTankSize, double efficiency)
{
this.Color = color;
this.NumberOfTyres = noOfTyres;
this.FuelTankSize = fuelTankSize;
this.Efficiency = efficiency;
this.calculateMPG();
}
}
}
该计划如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Vehicle_Application
{
class Program
{
static void Main(string[] args)
{
Car car1 = new Car("Red", 4, 5, 6);
Car car2 = new Car("Red");
car1.Color = "Red";
car2.Color = "Green";
System.Console.WriteLine("The color of the first vehicle is: ");
Console.WriteLine(car1.Color);
System.Console.WriteLine("The color of the second vehicle is: ");
Console.WriteLine(car2.Color);
Console.WriteLine(car1.milesPerGalon);
Console.ReadLine();
}
}
}
现在我的困惑出现在构造器Car
中。
实例
公共汽车(字符串颜色=null)
和公共汽车(字符串颜色、整数正午、双油箱大小、双效率)
Console.WriteLine(car2.Color)代码>和
Console.WriteLine(car1.milesPerGalon)代码>李>
程序识别出car1
有四个参数,因此使用car的第二个实例。而car2
只有一个参数,因此程序使用car的第一个实例
在我看来,同样的汽车实例似乎是链接的完美例子,因为我在互联网上找到了解释链接过程的例子 这里不涉及构造函数链接,这只是一个构造函数的两个不同重载
构造函数链接如下所示:
public Car(string color = null) : this(color, 4, 5, 6)
{
}
public Car(string color, int noOfTyres, double fuelTankSize, double efficiency)
{
this.Color = color;
this.NumberOfTyres = noOfTyres;
this.FuelTankSize = fuelTankSize;
this.Efficiency = efficiency;
this.calculateMPG();
}
注意:this()
调用第一个构造函数。通过传递颜色和一些默认值,可以链接到另一个ctor
您还可以链接到基类ctor(来自车辆),如果它有一个:
public class Vehicle
{
public Vehicle(Color color)
{
// whatever
}
}
public class Car : Vehicle
{
public Car(Color color) : base(color)
{
// whatever
}
}
评论中中间的更正:
必须始终调用车辆的构造函数,您不能创建车辆
没有先造一辆车。发生的情况是默认值
如果没有其他基,则隐式调用构造函数(如果有)
构造函数被显式调用
这里不涉及构造函数链接,这只是一个构造函数的两个不同重载
构造函数链接如下所示:
public Car(string color = null) : this(color, 4, 5, 6)
{
}
public Car(string color, int noOfTyres, double fuelTankSize, double efficiency)
{
this.Color = color;
this.NumberOfTyres = noOfTyres;
this.FuelTankSize = fuelTankSize;
this.Efficiency = efficiency;
this.calculateMPG();
}
注意:this()
调用第一个构造函数。通过传递颜色和一些默认值,可以链接到另一个ctor
您还可以链接到基类ctor(来自车辆),如果它有一个:
public class Vehicle
{
public Vehicle(Color color)
{
// whatever
}
}
public class Car : Vehicle
{
public Car(Color color) : base(color)
{
// whatever
}
}
评论中中间的更正:
必须始终调用车辆的构造函数,您不能创建车辆
没有先造一辆车。发生的情况是默认值
如果没有其他基,则隐式调用构造函数(如果有)
构造函数被显式调用
欢迎来到C#
既然你说这只是你第一周来这里。我将使用一个更通用的示例来帮助您理解一些基本的构造方法
假设我想创建一个名为Foo
的对象
var foo = new Foo(1);
下面是设计类的三种不同方法,以便它构造相同的对象:
超载
可选参数
构造函数链接
这三个字段都将生成一个名为Foo
的对象,其私有字段都设置为\u one=1
和\u two=2
此外,它们允许您调用构造函数方法
var foo = new Foo(1,0); // the exact same result (again) as just calling "new Foo(1);"
欢迎来到C#
既然你说这只是你第一周来这里。我将使用一个更通用的示例来帮助您理解一些基本的构造方法
假设我想创建一个名为Foo
的对象
var foo = new Foo(1);
下面是设计类的三种不同方法,以便它构造相同的对象:
超载
可选参数
构造函数链接
这三个字段都将生成一个名为Foo
的对象,其私有字段都设置为\u one=1
和\u two=2
此外,它们允许您调用构造函数方法
var foo = new Foo(1,0); // the exact same result (again) as just calling "new Foo(1);"
您确实不需要指定颜色双色@Lennart:我现在明白了链接是通过使用:this(xyz)
或从base调用:base(xyz)
来实现的。我的问题是:在你答案的第一行代码中,公共汽车(string color=null):这(color,4,5,6)
:这是对超载的构造函数的调用公共汽车(string color,int noOfTyres,double fuelTankSize,double efficiency)
。在你回答的第一段代码中:公共汽车(字符串颜色,整数noOfTyres,双燃料箱大小,双效率){this.Color=Color;..
:这也是一个变化,还是仅仅是另一个构造函数whithinCar
?@palm 1:是的,这是对过载的构造函数的调用。2:不,这不是链接,这只是另一个构造函数。“这样,车辆中的构造函数也会被调用。”此声明具有误导性。必须始终调用车辆
的构造函数,如果不先构造车辆
,则无法创建车辆
。发生的情况是默认构造函数被隐式调用(如果有)如果没有显式调用其他基构造函数。谢谢。我已经尝试使用链式构造函数:public Car(string color,int noOfTyres):这个(color,noOfTyres,1,1){}
除了子类Car
(参见上面的代码)中的两个构造函数之外,还尝试在程序中打印:Console.WriteLine(car3.noOfTyres);
,但我收到了此错误错误1“Vehicle\u Application.Car”不包含“noOfTyres”的定义,并且找不到接受类型为“Vehicle\u Application.Car”的第一个参数的扩展方法“noOfTyres”(是否缺少using指令或部件引用?)
。感谢您的任何声明。您真的不需要指定双色标记。@Lennart:我现在知道链接是通过使用:this(xyz)
或从base调用时发生的:base(xyz)
。我的问题