M
M
Minat0_2020-10-18 14:25:07
Java
Minat0_, 2020-10-18 14:25:07

Is it possible to use a unique primitive or something like that?

Hello, I'm new to Java so don't be too hard on me. ;)
Here, let's say I have a User class. And he has his own Property.
I create an abstract userProperty class of the form:

public abstract class UserProperty {
  private String propertyName;
  private String propertyValue;

  public UserProperty(String propertyName, Integer propertyValue) {
    this.propertyName = propertyName;
    this.propertyValue = propertyValue;
  }

  public UserProperty(String propertyName, String propertyValue) {
    this.propertyName = propertyName;
    this.propertyValue = propertyValue;
  }
  
}


The problem is that propertyValue can be either a String or an Integer or, say, a boolean.
Can I create something like a unique "primitive" that accepts my data?
Or maybe you would recommend something else?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Alexandrov, 2020-10-18
@zhenyavka

public abstract class UserProperty<T> {
  private String propertyName;
  private T propertyValue;

  public UserProperty(String propertyName, T propertyValue) {
    this.propertyName = propertyName;
    this.propertyValue = propertyValue;
  }
}

Гдето там нареализуете класс до нормально
public class UserPropertyString extends UserProperty<String>{
}
или
public class UserPropertyBoolean extends UserProperty<Boolean>{
}
и т.д.

S
Sergey Gornostaev, 2020-10-18
@sergey-gornostaev

Read about generic types.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question