Recently we were applying an example with a stack algorithm where the last element that is added is the first one that comes out.
Having done this, we were asked to remain encapsulated and, by the way we did it, the problem arose that when we used it by using an arraylist, it still responded to access by indexes. For example [1]
(which was wrong, because I would only have to respond to the methods of the TAD). Then we were given a complete example of how the encapsulation should be done (which was a topic we had already seen), but I could not understand the part where the encapsulation is performed with private
.
I understand that private is used to perform the encapsulation, but I asked myself why was not there a'get' or a'et 'to be used in those cases, right?
If anyone besides this can give me a definition of TAD or the way they are used, I would appreciate it.
static void Main(string[] args)
{
Pila pila = new Pila();
pila.push(1);
pila.push("A");
pila.push(2);
Console.WriteLine(pila.pop());
Console.ReadKey();
}
public class Pila
{
private ArrayList elementos;
public void push(object elemento)
{
elementos.Add(elemento);
}
public object pop()
{
object elemento = elementos[elementos.Count - 1];
elementos.Remove(elemento);
return elemento;
}
public Pila()
{
elementos = new ArrayList();
}
}