我想做这样的事情
static <T> T mine()
{
return new Object(){void hello(){}};
}
这样我才能做到这一点
mine().hello();
打算做这样的事情
mine.hello().hi().bye();
所以要么我用1方法hello,hi,bye声明每个类,然后返回那些实例,或者以某种方式我可以从上面的每个方法返回一个匿名类(使用新定义的方法)。所以我不必写很多课程。
喜欢它我能做到
static <T> T tunnel(T t)
{
return t;
}
tunnel(new Object()
{
void hello()
{
System.out.println("hello");
}
}).hello();
但它没用,我想从隧道返回匿名对象(在隧道本身中创建),而不是返回传递的参数
答案 0 :(得分:0)
你可能想要一个流利的课程。像
这样的东西public class MyFluentClass {
public MyFluentClass methodOne() {
// do something
return this;
}
public MyFluentClass methodTwo() {
// do something
return this;
}
}
然后在你的主要方法中:
MyFluentClass myFuent = new MyFluentClass();
myFluent.methodOne().methodTwo();