Problem with abstract classes

0

It turns out that I must create the game Ahorcado , then I applied all the code but it turns out that I want to generate random words, of course words already defined and grab everything in an array where with the class Math.Random() can generate random numbers inside of the range of the arrangement and thus be able to take random words so that the game is more didactic. Here is part of the code that I still do not know how to handle abstract classes, that's why I ask for your help:

public abstract class Palabras
{
    private String nombre;
    private String Descripcion;
    public abstract void setNombre(String nombre);
    public abstract void getNombre();
}
public class PlatosTipicos extends Palabras
{
    //...
}

So that's my question, I do not know how to proceed.

    
asked by J.zer 08.11.2016 в 23:59
source

2 answers

0

Am I misunderstanding or do you want to inherit Words from other types of words?

If I am correct, I must tell you, I do not know if it is worth creating several classes for different types of words, you could use a structure that allows you to store several types of things, such as the ArrayList ArrayList.

By how you did that it seems more oriented to an interface-implementing than to an inheritance. The interfaces are like the abstract classes but with the difference that you only put in the methods that you force to implement. ex:

public interfaz observable
{
    public void mostrarDatos();
}
    
answered by 09.11.2016 / 14:09
source
0

Abstract classes serve to share methods and attributes of a higher class, and they should not be implementable, (you can not create an object of it), for example, you have your getter and setter in the Words class, so that in PlatosTipicos you will have to implement that setName (String name) and getName () and when doing this as the attribute you have it in private, you will not be able to access it in a simple way. (You can via reflect).

Abstract classes allow you to abstract your problem as much as possible, right now your problem is to generate a new word, so I would do something like that.

public abstract class Palabras{ //por favor es Java no C o C# las llaves en Java suelen escribirse en la firma de la clase o método.
   //Podrías hacer que nombre y descripción esten dentro de un constructor.

    public Palabras(String nombre, String descripcion){
        this.nombre=nombre;
        this.descripcion=descripcion;
    }

    private String nombre;
    private String Descripcion;
    public void setNombre(String nombre){
         this.nombre=nombre;
    }
    public String getNombre(){
         return nombre;
    }
    public abstract String obtenerPalabra(); 
}
public class PlatosTipicos extends Palabras{
    public String obtenerPalabra(){
    //Tu algoritmo de obtener aleatoriamente la palabra.
    }
}

And already for the execution it would be something like that

Palabras palabra = new PlatosTipicos("nombre","descripcion");
palabras.obtenerPalabra();
    
answered by 09.11.2016 в 00:56