C#中的多态性与接口

本文关键字:接口 多态性 | 更新日期: 2024-10-21 18:36:20

创建三个继承无关的小类——Building、Car和Bicycle。使用GetCarbonFootprint方法编写一个接口ICarbonFootprint。让您的每个类实现该接口,以便其GetCarbonFootprint方法为该类计算适当的碳足迹(查看一些解释如何计算碳足迹的网站)。编写一个应用程序,创建三个类中每一个类的对象,在List中放置对这些对象的引用,然后遍历List,以多态方式调用每个对象的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
    }

我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Miller
{
    class Program
    {
        static void Main(string[] args)
        {
            Bicycle bike = new Bicycle();
            Building b = new Building();
            Car car = new Car();
            List<ICarbonFootprint> list = new List<ICarbonFootprint>();

            list.Add(bike);
            list.Add(b);
            list.Add(car);
            int totalCarbon = 0;
            foreach (var item in list)
            {
                totalCarbon += item.GetCarbonFootprint();
                Console.WriteLine("{0} has a footprint of: {1}", item,      item.GetCarbonFootprint());
            }
            Console.WriteLine("Total footprint is: {0}", totalCarbon);
            Console.ReadKey();
        }

    }

    public class Bicycle : ICarbonFootprint
    {
        private string _make;
        private string _model;
        public string Make
        {
            get { return _make; }
            set { _make = value; }
        }
        public string Model
        {
            get { return _model; }
            set { _model = value; }
        }
        public int GetCarbonFootprint()
        {
            return 10;
        }
        public override string ToString()
        {
            return string.Format("Bike");
        }
    }
    public class Building : ICarbonFootprint
    {
        private string _address;
        public string Address
        {
            get { return _address; }
            set { _address = value; }
        }
        public int GetCarbonFootprint()
        {
            return 2000;
        }
        public override string ToString()
        {
            return string.Format("Building");
        }
    }
    public class Car : ICarbonFootprint
    {
        private string _make;
        private string _model;

        public string Make
        {
            get { return _make; }
            set { _make = value; }
        }
        public string Model
        {
            get { return _model; }
            set { _model = value; }
        }
        public int GetCarbonFootprint()
        {
            return 1500;
        }
        public override string ToString()
        {
            return string.Format("Car");
        }
    }
    public interface ICarbonFootprint
    {
        int GetCarbonFootprint();

    }

}

我正在整合我的讲师代码(第12-23行更改了AKA课程是唯一更改的内容):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Miller
{
    class Program
    {
        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 class Bicycle : ICarbonFootprint
    {
        private string _make;
        private string _model;
        public string Make
        {
            get { return _make; }
            set { _make = value; }
        }
        public string Model
        {
            get { return _model; }
            set { _model = value; }
        }
        public int GetCarbonFootprint()
        {
            return 10;
        }
        public override string ToString()
        {
            return string.Format("Bike");
        }
    }
    public class Building : ICarbonFootprint
    {
        private string _address;
        public string Address
        {
            get { return _address; }
            set { _address = value; }
        }
        public int GetCarbonFootprint()
        {
            return 2000;
        }
        public override string ToString()
        {
            return string.Format("Building");
        }
    }
    public class Car : ICarbonFootprint
    {
        private string _make;
        private string _model;

        public string Make
        {
            get { return _make; }
            set { _make = value; }
        }
        public string Model
        {
            get { return _model; }
            set { _model = value; }
        }
        public int GetCarbonFootprint()
        {
            return 1500;
        }
        public override string ToString()
        {
            return string.Format("Car");
        }
    }
    public interface ICarbonFootprint
    {
        int GetCarbonFootprint();

    }

}

因此,用我的讲师代码替换我的课堂程序代码,我收到了以下错误:

Program.cs(51,23,51,41): error CS1729: 'Miller.Building' does not contain a         constructor that takes 1 arguments
Program.cs(52,23,52,34): error CS1729: 'Miller.Car' does not contain a   constructor that takes 1 arguments

现在,因为春假前的最后两天因为天气(雪)取消了,我们没能讨论。我的代码似乎按照指示执行,但我想让我的讲师的课堂程序代码与我的代码一起使用。有人可能帮我纠正这些错误吗?

C#中的多态性与接口

您的代码存在一些问题。

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

对于Building,这看起来像:

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

对于Car,这看起来像:

    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(list[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();
    }

我选择了缩短属性定义,而不是用字段来实现它们。

输出为:

0
125000
200

您应该为Building和Car编写类似next:的构造函数

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();
...

我希望它能有所帮助。