Java: newbie inheritance issue

Suppose I have a base class B and a derived class D. I want to have a foo () method in my base class that returns a new object of any type of instance. So, for example, if I call B.foo (), it returns an object of type B, and if I call D.foo (), it returns an object of type D; Meanwhile, the implementation is performed exclusively in base class B.

Is it possible?

+3
source share
8 answers

not to do. Make a paragraph to the "foo" method.

abstract class B {
    public abstract B foo();
}

Or get an abstract factory through the constructor of the base class:

abstract class B {
    private final BFactory factory;
    protected B(BFactory factory) {
        this.factory = factory;
    }
    public B foo() {
        return factory.create();
    }
}
interface BFactory {
    B create();
}

Add covariant return types and generics to taste.

+3
source

So far, each class has a default constructor:

    public B instance() throws Exception {
        return getClass().newInstance();
    }
+3

, , .. :

public ClassName getFoo() throws InstantiationException, IllegalAccessException
{
    return getClass().newInstance();
}

ClassName - .

, , ... , !

: newInstance() , , , , .

, () .

+1

, , , "this" , -

public B foo() {
    return this.getClass().newInstance();
}

- ? D, d.foo(), D, B. , , .

+1

, , , , , , .

() , B.foo(), D.foo(), , , . , .

- foo(), :

public class B {
    public B foo()
    throws IllegalAccessException, InstantiationException {
        return this.getClass().newInstance();
    }
}

public class D  extends B{    
}

public class Test {
    public static final void main(String[] args)
    {
        try {
            System.out.println((new B()).foo());
            System.out.println((new D()).foo());
        } catch (IllegalAccessException e) {
            e.printStackTrace();  
        } catch (InstantiationException e) {
            e.printStackTrace();  
        }
    }
}
+1

, getClass(). newInstance(), (, InstantationException IllegalAccessException).

- , ( ) , getNewInstance(), .

.

Thing foo() {
    Thing th = getNewInstance();
    // do some stuff with th
    return th;
}

Thing getNewInstance() {
    return getClass().newInstance();
}

getNewInstance() , , , .

Thing getNewInstance() {
    return new BigThing(10, ThingSize.METRES);
}
+1

@Rik

, , Thing. Thing , getNextThing(), Thing.

, BigThing, LittleThing, SomethingThing, getNextThing() . , ... .

?

0

, , . .

public class B <X extends B>{

public X foo() throws InstantiationException, IllegalAccessException{
    return (X)this.getClass().newInstance();
}
}        
public class C extends B<C>{        

}

. CS- , , . , . getNextThing Thing, , , Thing Iterable

That means you just need to write an Iterator that can encapsulate the logic of getting the next thing, t seems to fit into your inheritance model. Another advantage of this is that from this you get some powerful syntax devices (improved for understanding the loop).

0
source

Source: https://habr.com/ru/post/1697857/


All Articles