I have a base class builder like below:
public class BaseClass implements BaseInterface
{
private final String meaning;
<T extends Builder<T>> BaseClass(Builder<T> builder)
{
this.meaning = builder.meaning;
}
public static class Builder<T extends Builder<T>>
{
private String meaning;
@SuppressWarnings("unchecked")
public T meaning(String meaning)
{
this.meaning = meaning;
return (T) this;
}
public BaseClass build()
{
return new BaseClass (this);
}
}
}
And the derived class :
public class DerivedClass extends BaseClass implements DerivedInterface
{
private final long sequence;
<T extends Builder<T>> DerivedClass(Builder<T> builder)
{
super(builder);
this.sequence = builder.sequence;
}
@Override
public long getSequence()
{
return sequence;
}
public static class Builder<T extends Builder<T>> extends BaseClass.Builder<T>
{
private long sequence;
@SuppressWarnings("unchecked")
public T sequence(long sequence)
{
this.sequence = sequence;
return (T) this;
}
@Override
public DerivedClass build()
{
return new DerivedClass (this);
}
}
- Is this the right way?
Also, I am trying to instantiate base class object but having trouble with it.
BaseClass baseClass = new BaseClass.Builder<Builder>();
The above code gives this error
Bound mismatch: The type BaseClass.Builder is not a valid substitute for the bounded parameter <T extends BaseClass.Builder\<T\>> of the type BaseClass.Builder<T>
- How do I fix this?
Aucun commentaire:
Enregistrer un commentaire