Java nested and / or child classes
I was wondering what best practices were in java when running a nested class - im thinking of running an rmi server that would be created upon the instanciation of the parent class so, say
class foo() [the 'parent']
constructor for foo() - instanciates rmifoo(开发者_StackOverflow社区)
this get a bit painful when you want to call a method from rmifoo() back in the 'parent' class (you cant simply import the class, because of rmi). one way is of course to make rmifoo() an actual child class, and then be able to access methods in the parent, but this seems needlessly OTT (esp because then, presumably you need to make the parent abstract, etc etc and that really complicates a lot of other functionality).
I was wonder what the best practise is in terms of setting up a system like this, so one can call methods back in the foo() class from within its server ie. rmifoo()
*********update*******
Code (as im thinking of it currently) would be somethine like:
A.java:
public class A {
B aBinstance;
public fooParent() {
[do something..]
}
}
B.Java
public class B {
public fooChild() {
[super].fooParent() (super wont work...?)
}
}
I think this is what you're trying to do:
public class A {
private B b;
public A() {
b = new b(this);
}
public aMethodInA() {
//stuff goes here
}
}
public class B {
private A a;
public B(A a) {
this.a = a;
}
public aMethodCallingMethodsInTheObjectThatInstantiatedMe() {
a.aMethodInA();
}
}
This is doable and very useful in some cases, but you end up with these circular references, and that is something that you usually want to avoid. Think about why you need to call the class A from B. Maybe there is a way to split your problem into three classes and break your circular references that way.
If rmifoo
is an inner class inside foo
, then it can directly access the methods in foo
.
[EDIT]: This applies only if rmifoo
class is not a static inner class
, in which case it will be only abel to access static methods in foo.
I think you are really asking whether or not is a good idea to use Java nested classes to implement a containment relationship in your application's (conceptual) object model; as in ...
public class A {
public class B {
public A getParent() {
return A.this;
}
}
}
I'm inclined to think that it is not a good idea:
This approach breaks down if the relationship is recursive. For example, if an A can contain a B and B can contain an A, you cannot both make B a nested class of A, and A a nested class of B.
The approach does not allow an instance of B to be created independently of an A, or to be moved from one A instance to another.
Also, while this approach handles the child-to-parent navigation, it does not provide parent-to-children navigation. You'd still have to implement that explicitly; e.g. with an attribute like
private List<B> children;
in the A class.
I think it is generally better (e.g. more flexible) to implement A and B as separate non-nested classes and use explicit parent
and/or chidren
attributes (as required) to represent the containment relationship.
精彩评论