-1

我正在创建一个控制台应用程序,它利用继承来访问我也在创建的接口中的方法。这将是方法的接口。(代码仍有待添加。) 编辑:我正在创建一个应用程序,用于捕获飞机和火车的交通预订信息。CAircraftBookings、CTrainBookings 是包含和管理飞机/火车预订的预订类。

namespace Vehicle_Bookings
{
public interface IVehichlesBookings : CAircraftBookings, CTrainBookings
{
    public int Add
    {
    }

    public int Update
    {
    }

    public int Delete
    {
    }

    public int Retrieve
    {
    }
}

}

现在在我的控制台应用程序中,用户选择适当的选项,即添加、更新、删除和检索。然后选择如下:

switch (choice)
        {
            case "1": 



                break;

            case "2":



                break;

            case "3":



                break;

            case "4":



                break;
        }
        while (choice != "5")

我将如何从该接口实现特定方法。如果用户按 1 选择 1,将使用 Add 方法。如果他们按 2,将使用更新方法等。

4

3 回答 3

2

“我将从 IVehicleBookings 类派生和实施飞机和火车的预订类”

我认为这就是你想要的:

public interface IVehichlesBookings
{
    int Add();
    int Update();
    int Delete();
    int Retrieve();
}

public class CAircraftBookings : IVehichlesBookings
{
    public int Add()
    {
        throw new NotImplementedException();
    }

    public int Update()
    {
        throw new NotImplementedException();
    }

    public int Delete()
    {
        throw new NotImplementedException();
    }

    public int Retrieve()
    {
        throw new NotImplementedException();
    }
}

public class CTrainBookings : IVehichlesBookings
{
    public int Add()
    {
        throw new NotImplementedException();
    }

    public int Update()
    {
        throw new NotImplementedException();
    }

    public int Delete()
    {
        throw new NotImplementedException();
    }

    public int Retrieve()
    {
        throw new NotImplementedException();
    }
}

一个界面IVehicleBookings,定义了您可以使用“车辆预订”执行的各种操作,CAircraftBookings并将CTrainBookings决定如何完成这些操作。

于 2013-09-11T08:36:52.637 回答
1

假设 VehiclesBooking 是一个实现 IVehiclesBooking 的类:

IVehiclesBooking vehiclesBooking = new VehiclesBooking();

switch (choice)
        {
            case "1": 
            vehiclesBooking.Add();
            break;

            case "2":
            vehiclesBooking.Update();
            break;

...
于 2013-09-11T08:34:56.077 回答
1

我想我在做别人的功课:)

我可以建议您,您应该阅读有关接口以及如何使用它们的信息。看这里:我们为什么要使用接口?仅仅是为了标准化吗?

我认为接口的定义是正确的,除了它不应该从类派生。您应该从接口派生。

示例:

public interface IVehichlesBookings
{
    // members.
}

public class CAircraftBookings : IVehichlesBookings
{
}

public class CTrainBookings : IVehichlesBookings
{
}

这意味着,aCAircraftBookings支持 interface IVehichlesBookings,因此它应该实现这些IVehichlesBookings成员。接下来,您可以处理CTrainBookingsCAircraftBookings作为的实例IVehichlesBookings

你需要像这样构造它:

IVehichlesBookings booking = new CTrainBookings();

char key = Console.ReadKey();

switch(key)
{
    case '1':
        booking.Add(...);
        break;
}

到你了..

于 2013-09-11T08:37:43.140 回答