c#中构造函数重载和链接之间的区别

时间:2017-08-05 08:56:14

标签: 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
{
    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;}

}
}

以下儿童班" Car"被驱使:

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。 实例

  1. public Car(string color = null)
  2. public Car(string color, int noOfTyres, double fuelTankSize, double efficiency)
  3. 两者都有不同的签名(nr。参数),并通过调用

    Program中重载
    1. Console.WriteLine(car2.Color);
    2. Console.WriteLine(car1.milesPerGalon);
    3. 程序识别car1有四个参数,因此使用汽车的第二个实例。而car2只有一个参数,因此Program使用汽车的第一个实例。

      同样的汽车实例看起来就像链接的完美例子,因为我在互联网上找到了解释与链接相同过程的例子。

2 个答案:

答案 0 :(得分:4)

这里没有构造函数链接,这些只是构造函数的两个不同重载。

构造函数链接看起来像这样:

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的构造函数,否则无法创建Car   没有先构建车辆。会发生什么是默认值   如果没有其他基础,则隐式调用构造函数(如果有)   构造函数被显式调用。

答案 1 :(得分:0)

欢迎来到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的对象,私有字段都设置为_one = 1_two = 2

此外,它们允许您调用构造函数方法

var foo = new Foo(1,0); // the exact same result (again) as just calling "new Foo(1);"