N
N
Nube2017-02-15 16:59:17
Java
Nube, 2017-02-15 16:59:17

Question about inheritance, how?

There is an abstract class Message, from which the Notification and Registration classes are inherited. So, the Registration class is sent from the client, and the server receives only the Message class. The Message class has a message-type field (Notification or Registration ) and if the message type is registration, then it should pull out the data of the fields of the Registration class. The problem is this: I wrap the message that came from the client in the Message class and I cannot wrap it in the Registration class. Tell me what can be done.
here is an example

try {
            // пришедшие данные являются ли они сообщением
            if (!(msg instanceof Message)){
                System.out.println("Чужеродная субстанция" +
                        "");
                return ;
            }
            // проверка что за  сообщение
            message = (Message)msg;
            if (msg!= null){
                switch (message.getTypeMessage()){
                    case MESSAGE:
                        System.out.println("Классическое сообщение" );
                        break;
                    case REGISTRATION:
                        System.out.println(" Регистрация");
                        Account account = (Account) message;
                        String  email=  account.getEmail().toString();
                        String  password = account.getEmail().toString();
                        System.out.println(email);
                        //saveTraz = new SaveTraz();
                        //saveTraz.saveData(email,password);
                        break;
                }
            }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Cyril, 2017-02-15
@Nube

Try typing in the Generics solution. Say that you are waiting for the input of a descendant of your abstract class.
PS: Why do you need switch / case if you have only two conditions, and one of them is not interesting to you. Try changing it to something like:

if (message!= null && message.getTypeMessage() == REGISTRATION) {
// TODO
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question