Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C语言中的多态性与接口#_C# - Fatal编程技术网

C# C语言中的多态性与接口#

C# C语言中的多态性与接口#,c#,C#,通过继承类Building、Car和Bicycle创建三个不相关的小类。使用GetCarbonFootprint方法编写接口ICarbonFootprint。让您的每个类实现该接口,以便其GetCarbonFootprint方法为该类计算适当的碳足迹(查看一些解释如何计算碳足迹的网站)。编写一个应用程序,创建三个类中每个类的对象,在列表中放置对这些对象的引用,然后遍历列表,以多态方式调用每个对象的GetCarbonFootprint方法。汽车建造师初始化“加仑汽油”,建筑建造师初始化建筑面积 如

通过继承类Building、Car和Bicycle创建三个不相关的小类。使用GetCarbonFootprint方法编写接口ICarbonFootprint。让您的每个类实现该接口,以便其GetCarbonFootprint方法为该类计算适当的碳足迹(查看一些解释如何计算碳足迹的网站)。编写一个应用程序,创建三个类中每个类的对象,在列表中放置对这些对象的引用,然后遍历列表,以多态方式调用每个对象的GetCarbonFootprint方法。汽车建造师初始化“加仑汽油”,建筑建造师初始化建筑面积

如何计算碳足迹

一加仑的汽油可以产生20磅的二氧化碳

将建筑面积乘以50

自行车没有

我的导师代码:

public static void Main(string[] args)
        {
            ICarbonFootprint[] list = new ICarbonFootprint[3];

            // add elements to list
            list[0] = new Bicycle();
            list[1] = new Building(2500);
            list[2] = new Car(10);

            // display carbon footprint of each object
            for (int i = 0; i < list.Length; i++)
                list[i].GetCarbonFootprint();
        } // end Main
    }

现在,因为春假前的最后两天因天气(雪)而取消,我们无法讨论。我的代码似乎符合说明的要求,但我想让我的讲师的课堂程序代码与我的代码一起工作。可能有人能帮我解决这些错误吗?

你应该为建筑和汽车编写构造函数,如下所示:

public Building(int MyValue)
{
...
}

您的代码也可以正常工作。

建议:Car和Bicycle共享属性以及
ICarbonFootprint
实现,因此您可以使用抽象方法创建基类。另外,来自
ICarbonFootprint
GetCarbonFootprint
接口必须是
System.Double
类型

public interface ICarbonFootprint
{
    int GetCarbonFootprint();
}

public class Building : ICarbonFootprint
{
    public int BuildingSquareFootage { get; set; }
    public string Address { get; set; }

    public Building(int buildingSquareFootage, string address)
    {
        BuildingSquareFootage = buildingSquareFootage;
        Address = address;
    }

    public int GetCarbonFootprint()
    {
        return BuildingSquareFootage * 50;
    }

    public override string ToString()
    {
        return string.Format("Building");
    }
}

public abstract class CarBicycleBase : ICarbonFootprint
{
    public string Make { get; set; }
    public string Model { get; set; }
    protected CarBicycleBase(string make, string model)
    {
        Make = make;
        Model = model;
    }
    public abstract int GetCarbonFootprint();
}

public class Bicycle : CarBicycleBase
{
    public Bicycle(string make, string model)
        : base(make, model) { }

    public override int GetCarbonFootprint()
    {
        return 0;
    }

    public override string ToString()
    {
        return string.Format("Bike");
    }
}

public class Car : CarBicycleBase
{
    public int GallonOfGas { get; set; }

    public Car(int gallonOfGas, string make, string model)
        : base(make, model)
    {
        GallonOfGas = gallonOfGas;
    }

    public override int GetCarbonFootprint()
    {
        return GallonOfGas * 20;
    }

    public override string ToString()
    {
        return string.Format("Car");
    }
}
例如:

...
var list = new List<ICarbonFootprint>(3)
{
    new Car(10, "...", "..."),
    new Bicycle("...", "..."),
    new Building(20, "...")
};

foreach (ICarbonFootprint item in list)
    item.GetCarbonFootprint();
...
。。。
变量列表=新列表(3)
{
新车(10,“…”,“…”),
新自行车(“…”,“…”),
新大楼(20“…”)
};
foreach(列表中的iCarFootPrint项)
item.GetCarbonFootprint();
...

我希望它能有所帮助。

您的代码存在一些问题

首先,您需要包含构造函数来编译代码

对于
建筑
,这看起来像:

    private int squareFootage;
    public Building(int squareFootage)
    {
        this.squareFootage = squareFootage;
    }
    private int gasGallons;
    public Car(int gasGallons)
    {
        this.gasGallons = gasGallons;
    }
对于
汽车
,这看起来像:

    private int squareFootage;
    public Building(int squareFootage)
    {
        this.squareFootage = squareFootage;
    }
    private int gasGallons;
    public Car(int gasGallons)
    {
        this.gasGallons = gasGallons;
    }
其次,你没有遵守计算碳足迹的规则

它们应该是:

    //Bicycle
    public int GetCarbonFootprint()
    {
        return 0;
    }

    //Building
    public int GetCarbonFootprint()
    {
        return 50 * squareFootage;
    }

    //Car
    public int GetCarbonFootprint()
    {
        return 20 * gasGallons;
    }
最后,讲师的代码实际上不会显示任何结果。for循环中的代码应更改为
Console.WriteLine(列表[i].GetCarbonFootprint())如果这是一个控制台应用程序

因此,所有代码应如下所示:

public static void Main(string[] args)
{
    ICarbonFootprint[] list = new ICarbonFootprint[3];

    // add elements to list
    list[0] = new Bicycle();
    list[1] = new Building(2500);
    list[2] = new Car(10);

    // display carbon footprint of each object
    for (int i = 0; i < list.Length; i++)
        Console.WriteLine(list[i].GetCarbonFootprint());
}

    public class Bicycle : ICarbonFootprint
    {
        public string Make { get; set; }
        public string Model { get; set; }

        public int GetCarbonFootprint()
        {
            return 0;
        }
    }

    public class Building : ICarbonFootprint
    {
        private int squareFootage;
        public Building(int squareFootage)
        {
            this.squareFootage = squareFootage;
        }

        public string Address { get; set; }

        public int GetCarbonFootprint()
        {
            return 50 * squareFootage;
        }
    }

    public class Car : ICarbonFootprint
    {
        private int gasGallons;
        public Car(int gasGallons)
        {
            this.gasGallons = gasGallons;
        }

        public string Make { get; set; }
        public string Model { get; set; }

        public int GetCarbonFootprint()
        {
            return 20 * gasGallons;
        }
    }

    public interface ICarbonFootprint
    {
        int GetCarbonFootprint();
    }

您的错误只是说,
“Miller.Building”不包含带1个参数的构造函数
,这意味着
Building
没有带1个参数的构造函数,您必须缺少部分代码。您缺少这部分问题:“汽车构造函数初始化”加仑汽油,建筑构造师将初始化建筑面积。“它将创建是的,但不会满足要求。输出:01250000,2002此外,我必须使用公共静态void Main(字符串[]args)或静态void Main(字符串[]args),而不是void Main()ALONEGATIVITY,是否为0125000,200听起来很合理,使用public static void Main(string[]args)还是static void Main(string[]args)更合适@雀巢-我修复了
Main
signature-我使用LINQPad对答案进行编码,但在复制和粘贴时忘记修复签名。我还将输出添加到我的答案中。我认为最好是明确地使用
public
修饰符。
public static void Main(string[] args)
{
    ICarbonFootprint[] list = new ICarbonFootprint[3];

    // add elements to list
    list[0] = new Bicycle();
    list[1] = new Building(2500);
    list[2] = new Car(10);

    // display carbon footprint of each object
    for (int i = 0; i < list.Length; i++)
        Console.WriteLine(list[i].GetCarbonFootprint());
}

    public class Bicycle : ICarbonFootprint
    {
        public string Make { get; set; }
        public string Model { get; set; }

        public int GetCarbonFootprint()
        {
            return 0;
        }
    }

    public class Building : ICarbonFootprint
    {
        private int squareFootage;
        public Building(int squareFootage)
        {
            this.squareFootage = squareFootage;
        }

        public string Address { get; set; }

        public int GetCarbonFootprint()
        {
            return 50 * squareFootage;
        }
    }

    public class Car : ICarbonFootprint
    {
        private int gasGallons;
        public Car(int gasGallons)
        {
            this.gasGallons = gasGallons;
        }

        public string Make { get; set; }
        public string Model { get; set; }

        public int GetCarbonFootprint()
        {
            return 20 * gasGallons;
        }
    }

    public interface ICarbonFootprint
    {
        int GetCarbonFootprint();
    }
0
125000
200