C
chris sennitt
the following code shows a simple class setup.
i want to be able to use code in the following manner.
b.aa().bb();
rather than lots of seperate lines
b.aa();
b.bb();
as there will be many method calls per object
the code fails for obvious reasons. the return type of b.aa() is of class
aClass which doesnt have the method bb().
so, i thought, duplicate the code in bClass and return the bClass object.
public bClass aa() {
super.aa(); // call the real method that does the
work....
return this;
}
however, this complains that the return type is incompatible with
aClass.aa(), so that doesnt work.
any suggestions on class design / changes so that this can be made to work ?
the class tree is way more complex than shown here.
public class test {
public static void main(String[] args) {
aClass a;
bClass b;
a = new aClass();
b = new bClass();
a.aa();
b.aa();
b.bb();
b.aa().bb();
}
}
class aClass {
public void call() {
System.out.println( "aClass" );
}
public aClass aa() {
System.out.println( "aa" );
return this;
}
}
class bClass extends aClass {
public bClass bb() {
System.out.println( "bb" );
return this;
}
}
i want to be able to use code in the following manner.
b.aa().bb();
rather than lots of seperate lines
b.aa();
b.bb();
as there will be many method calls per object
the code fails for obvious reasons. the return type of b.aa() is of class
aClass which doesnt have the method bb().
so, i thought, duplicate the code in bClass and return the bClass object.
public bClass aa() {
super.aa(); // call the real method that does the
work....
return this;
}
however, this complains that the return type is incompatible with
aClass.aa(), so that doesnt work.
any suggestions on class design / changes so that this can be made to work ?
the class tree is way more complex than shown here.
public class test {
public static void main(String[] args) {
aClass a;
bClass b;
a = new aClass();
b = new bClass();
a.aa();
b.aa();
b.bb();
b.aa().bb();
}
}
class aClass {
public void call() {
System.out.println( "aClass" );
}
public aClass aa() {
System.out.println( "aa" );
return this;
}
}
class bClass extends aClass {
public bClass bb() {
System.out.println( "bb" );
return this;
}
}