G
G
gibsonen2017-10-22 22:03:12
1C
gibsonen, 2017-10-22 22:03:12

What object came to me?

I have a class with a constructor that takes a Object. I create 2 instances of this class and pass type Integerand type Doublerespectively to the constructor. How can I determine which one Objectcame to me in the constructor? ( Integeror Double?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
KeyJ, 2017-10-22
@gibsonen

Use the instanceof keyword

public MyClass(Object o){
   if (o instanceof Integer) {
      //Действия для Integer
   }
   if (o instanceof Double) {
      //Действия для Double
   }
}

But if you need to implement such a condition, then most likely you have a mistake somewhere in the application architecture.

A
aol-nnov, 2017-10-22
@aol-nnov

public MyClass(Integer o){
      //Действия для Integer
}
public MyClass(Double o){
      //Действия для Double
}

ne?!
what you are doing is like passing void* everywhere in c++. what is the profit?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question