In-doubt of inheritance in C #

6

I'm trying to solve an inheritance exercise and I found a question I saw that has a chance of appearing elsewhere and so I thought I should ask here. The exercise would ask you to first create a Voo class that represents an airplane flight and is able to manipulate information about seat occupancy, part of which is that each flight has a maximum of 100 seats.

Then you are asked to create an heir class that allows you to set the maximum number of seats and split the plane into smokers and nonsmokers. With regard to part of smokers I know how to implement. What I do not know is the variable number of chairs because of the way I implemented the base class.

Since there are 100 seats, I've created a 100 bool array in the base class where the i -this entry is true if the i seat is busy. Basically I did this by initializing the array in the constructor as follows:

public class Voo
{
    private bool[] ocupacaoAssentos;

    public int Numero { get; private set; }

    public Data Data { get; private set; }

    public Voo(int numeroVoo, Data data)
    {
        this.Numero = numeroVoo;

        this.Data = data;

        this.ocupacaoAssentos = new bool[100];
    }
}

Then methods manipulate this array. The problem is that the heiress class would not be able to modify this. As far as I know, in C # the heir class constructor always calls the base class constructor.

A possible solution (in C #) would be to implement this with a collection class that has no fixed size and then create a readonly static field that says the maximum of seats and then modify it in the heiress class and from there make checks for that everything is within limits. I do not know if the exercise point would be valid, because the exercise is in Java and I do not know if Java has these collection classes like C #.

But what if I had no way to modify the base class for some reason or some other detail of the problem, I simply could not apply such a solution? How could I deal with this? This is a case that it seems to me that the base class is more general than the inherit class.

    
asked by anonymous 17.05.2014 / 03:37

2 answers

4

There are several ways to do this. A simple way is to create a new constructor that allows you to define the number of seats:

public Voo(int numeroVoo, int numeroAssentos, Data data)
{
   ...
   this.ocupacaoAssentos = new bool[numeroAssentos];
   ...
}

In the inherit class invoke this new constructor using ": base ()":

public ClasseHerdeira(int numeroVoo, int numeroAssentos, Data data)
{
   :base(numeroVoo, numeroAssentos, data) { }
}

Another way is to create a method setNumberAssists () in the Flight class and invoke this method in the heir class constructor.

    
17.05.2014 / 04:10
2

To access the inherited constructor use :base you will access the class constructors

The base keyword is used to access the base class members from within a derived class ( Translation ).

Reference: Base (C # Reference)

Example

Base Class

public class Voo
{
    private bool[] ocupacaoassentos;
    public int Numero { get; private set; }
    public DateTime Data { get; private set; }

    public Voo()
    {
        this.Numero = 0;
        this.Data = DateTime.Now;
        this.ocupacaoassentos = new bool[100];
    }
    public Voo(int NumeroVoo, bool[] OcupacaoAssentos)
    {
        this.Numero = NumeroVoo;
        this.Data = DateTime.Now;
        this.ocupacaoassentos = OcupacaoAssentos;
    }
    public Voo(int NumeroVoo, DateTime Data, bool[] OcupacaoAssentos)
    {
        this.Numero = NumeroVoo;
        this.Data = Data;
        this.ocupacaoassentos = OcupacaoAssentos;
    }               
}

Derivative Class (Flight class inheritance)

public class HerdaVoo: Voo
{
    //Construtor sem paramentros igual classe base
    public HerdaVoo()
        :base() { }

    //Construtor com paramentro seguindo o construtor da classe base
    public HerdaVoo(int NumeroVoo, bool[] OcupacaoAssentos)
        : base(NumeroVoo, OcupacaoAssentos) { }

    //Construtor com paramentro seguindo o construtor da classe base
    public HerdaVoo(int NumeroVoo, DateTime Data, bool[] OcupacaoAssentos)
        :base(NumeroVoo, Data, OcupacaoAssentos) { }
}

Notice that it will access the 3 constructors of the base class note in the figure:

Coding

HerdaVoo herda = new HerdaVoo(100, DateTime.Now, new bool[500]);
    
17.05.2014 / 15:48