简体   繁体   中英

C# Implementing methods from an interface

i am creating a console app that makes use of inheritance to access methods in an interface which i am also creating. This will be the interface with the methods. (Code still to be added.) EDIT: I am creating an application that captures transport booking info for aircraft and trains. CAircraftBookings, CTrainBookings are booking classes that will contain and manage bookings made for Aicraft/Trains.

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

    public int Update
    {
    }

    public int Delete
    {
    }

    public int Retrieve
    {
    }
}

}

Now in my console app, the user selects the appropriate choice which will be Adding, Updating, Deleting and Retrieving. The choices will then go as follows:

switch (choice)
        {
            case "1": 



                break;

            case "2":



                break;

            case "3":



                break;

            case "4":



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

How would i go about implementing specific methods from that interface. If the user presses 1 for choice 1, the method Add will be used. If they press 2 the update method will be used etc.

"I will be deriving and implementing booking classes for aircraft and trains from the IVehicleBookings class"

I think this is what you want:

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

An interface IVehicleBookings that defines all kinds of things you can do with "vehicle bookings", and CAircraftBookings and CTrainBookings will decide how those things are done.

Assuming VehiclesBooking is a class that implements IVehiclesBooking:

IVehiclesBooking vehiclesBooking = new VehiclesBooking();

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

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

...

I think i'm doing someones homework :)

As I can advise you, you should read about interfaces and how to use them. Look here: Why do we use Interface? Is it only for Standardization?

I think the definition of the interface is right, except it should not derive from a class. You should derive from the interface.

example: PSEUDO

public interface IVehichlesBookings
{
    // members.
}

public class CAircraftBookings : IVehichlesBookings
{
}

public class CTrainBookings : IVehichlesBookings
{
}

This means, that a CAircraftBookings supports the interface IVehichlesBookings , so it should have implement those IVehichlesBookings members. Next you can handle an instance of CTrainBookings or CAircraftBookings as IVehichlesBookings .

And you need to construct it like this:

IVehichlesBookings booking = new CTrainBookings();

char key = Console.ReadKey();

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

Your turn..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM