[英]difference between constructor overloading and chaining in c#
这是我第一周的面向对象操作,因此,如果问题是关于基础知识的,那么我不愿接受新的道歉,因为我无法解决这个问题。 我不明白构造函数重载和链接之间的区别。 我正在使用以下父类:
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
。 实例
public Car(string color = null)
和 public Car(string color, int noOfTyres, double fuelTankSize, double efficiency)
两者都有不同的签名(参数个数),并且都通过调用在Program
被重载
Console.WriteLine(car2.Color);
和 Console.WriteLine(car1.milesPerGalon);
。 该程序识别出car1
有四个参数,因此使用了car的第二个实例。 而car2
只有一个参数,因此Program
使用了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(来自Vehicle),如果它有一个:
public class Vehicle
{
public Vehicle(Color color)
{
// whatever
}
}
public class Car : Vehicle
{
public Car(Color color) : base(color)
{
// whatever
}
}
来自InBetween的评论更正:
Vehicle的构造函数必须始终被调用,您必须先构造Vehicle才能创建Car。 如果没有显式调用其他基本构造函数,则会发生默认的构造函数被隐式调用(如果存在)的情况。
欢迎使用C#!
既然您说过这只是您在这的第一周。 我将使用一个更通用的示例来帮助您理解一些基本的构造方法。
假设我要创建一个名为Foo
的对象。
var foo = new Foo(1);
这是设计类的三种不同方式,以便它构造相同的对象:
public class Foo
{
private int _one;
private int _two;
// simple
public Foo(int one)
{
_one = one; // assign the private field "_one" the value passed in
_two = 0; // assign the private field "_two" with a default value of 0
}
// overloaded (note that the method signature is different)
public Foo(int one, int two)
{
_one = one;
_two = two;
}
}
public class Foo
{
private int _one;
private int _two;
// optional parameter
public Foo(int one, int two = 0) // if no value is passed into "two", it defaults to 0
{
_one = one;
_two = two;
}
}
public class Foo
{
private int _one;
private int _two;
// constructor chaining (note: 'this')
public Foo(int one) : this(one, 0) { }
// constructor
public Foo(int one, int two)
{
_one = one;
_two = two;
}
}
所有这三个都将产生一个名为
Foo
的对象,两个私有字段都设置为_two = 2
_one = 1
和_two = 2
。
另外,它们允许您调用构造函数方法
var foo = new Foo(1,0); // the exact same result (again) as just calling "new Foo(1);"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.