Код: Выделить всё
FooКод: Выделить всё
AbstractFooКод: Выделить всё
FooImpl1Код: Выделить всё
this.foo.addBar(this);Пример 1
Код: Выделить всё
class Main {
public static void main(String[] args) {}
interface Foo {
Set getBars();
}
abstract class AbstractFoo> implements Bar {
private final F foo;
public AbstractBar(F foo) {
this.foo = foo;
// this following line is breaking my head...
this.foo.addBar(this);
}
@Override
public F getFoo() {
return this.foo;
}
}
class BarImpl1 extends AbstractBar {
public BarImpl1(FooImpl1 foo) {
super(foo);
}
}
class BarImpl2 extends AbstractBar {
public BarImpl2(FooImpl2 foo) {
super(foo);
}
}
}
Каждый класс, реализующий Foo, должен содержать набор реализации Bar и те Bar< Реализации /code> должны иметь поле foo, набранное с помощью этой конкретной реализации Foo.
Конкретно, если FooImpl реализует Foo, тогда BarImpl реализует Bar.
Когда реализация Bar создается с помощью параметра Foo, устанавливается поле foo. с его значением и текущим экземпляром Bar следует добавить в набор этого поля.
Код: Выделить всё
class FooImpl implements Foo {
private Set bars;
public void addBar(BarImpl bar) {
this.bars.add(bar);
}
}
class BarImpl implements Bar {
private FooImpl foo;
public BarImpl(FooImpl foo) {
this.foo = foo;
this.foo.addBar(this);
}
}
Код: Выделить всё
class AbstractFoo> implements Bar {
private F foo;
public AbstractBar(F foo) {
this.foo = foo;
this.foo.addBar(this);
}
}
Я пытался быть более точным при вводе общих параметров абстрактных классов, но ничего не получилось (пример 2).
Подробнее здесь: https://stackoverflow.com/questions/787 ... s-question