定义方法调用者在方法内部创建的匿名类的方法
在我现在拥有的一个类中,
A a = new A(){
stuffhere
};
我发现我需要在方法内创建新的 A 并返回它,但我必须从类调用者那里定义这些内容。 java中有没有办法做到这一点?类似的东西
A a = createAClass(){
stuffhere
};
public A createAClass()[T]{
return new A(){T};
}
或者类似的东西。我不想使用接口来传递给 create 方法,因为我的匿名类不仅重写方法,而且还添加属性和新函数,而且我不认为我可以用接口传递它们。
有什么想法吗?
为 -1ers 进行编辑(一条简单的评论就足够了) 使用语法 [T],显然是错误的,我的意思是可以传递通用代码的东西,比如说代码的复制粘贴。
createAClass()[int a; String b; @override public void mymethod(){dosomethigb;} public void dosomethingelse(){dosomethingelse;}];
会像这样工作
public A createAClass(){
return new A()
{
int a;
String b;
@override public void mymethod()
{dosomethigb;}
public void dosomethingelse()
{dosomethingelse;}};
};}
,但如果我在程序的另一部分编写,
createAClass()[float c; List d; public void yourmethod(){dosomething2;} @override public void dosomethingelse(){dosomethingelse2;}];
它会像我的坏处一样工作
public A createAClass(){
return new A()
{
float c;
List d;
public void yourmethod()
{dosomething2;}
@override public void dosomethingelse()
{dosomethingelse2;}
};}
,我选择了一个不好的例子,但我认为这是最清晰的方法。也许我应该使用 X 而不是 T?...
长话短说: 我想在方法内创建一个匿名类,但定义匿名类在方法调用者中做什么,而不是在方法内(如标题所示)
编辑2: 我知道我无法从类中访问新方法,我现在所做的是创建一个匿名类,添加一些属性和方法,然后在重写的方法中使用它们。添加的方法不是问题,因为我可以使方法调用者传递一个由创建的匿名类中的重写方法调用的接口,问题是属性。我不知道如何将它们添加到从方法调用者传递它们的匿名类中。
in a class i have
A a = new A(){
stuffhere
};
now i found that i need to create the new A inside a method and return it, but i have to define the stuffhere from the class caller. Is there a way in java to do so? Something like
A a = createAClass(){
stuffhere
};
public A createAClass()[T]{
return new A(){T};
}
or something similar. I would prefer not to use an interface to pass to the create method, since my anonymous classes not only override methods, but also adds attributes and new functions, and i don't think i can pass them with an interface..
Any thought?
EDIT for the -1ers (a simple comment would suffice)
with the syntax [T], obviously wrong, i meant something that can pass a generic code, let's say a copy-paste of code.
createAClass()[int a; String b; @override public void mymethod(){dosomethigb;} public void dosomethingelse(){dosomethingelse;}];
would work like
public A createAClass(){
return new A()
{
int a;
String b;
@override public void mymethod()
{dosomethigb;}
public void dosomethingelse()
{dosomethingelse;}};
};}
but if i write in another part of the program
createAClass()[float c; List d; public void yourmethod(){dosomething2;} @override public void dosomethingelse(){dosomethingelse2;}];
it would instead work like
public A createAClass(){
return new A()
{
float c;
List d;
public void yourmethod()
{dosomething2;}
@override public void dosomethingelse()
{dosomethingelse2;}
};}
My bad, i choose a bad may of making an example, but i thought it was the clearest way. Maybe i should have used X instead of T?..
Long story short:
i want create an anonymous class inside a method, but define what the anonymous class does in the method caller, and not inside the method(like the title says)
EDIT2:
i know i can't access the new methods from the class, what i do now is create an anonymous class, add a few attributes and method, and then use them in an overridden method. The added methods are not a problem, since i can make the method caller to pass an interface that is called by the overridden method in the anonymous class created, the problems are the attributes. I don't know how to add them in the anonymous class passing them from the method caller.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
类似以下内容通常有效:
如果您正在寻找其他内容,请澄清问题。
编辑
答案是否定的,你不能这样做。您正在尝试创建一个没有为
A
定义 API 的A
。即使您可以按照您的建议进行操作,如果A
未在某处定义,A
的任何用户如何知道哪些方法/字段可用?要使A
发挥作用,您需要有一个A
实现的 API。Something like the following usually works:
If you are looking for something else, please clarify the question.
Edit
Answer is no you can't do that. You are trying to create an
A
with no defined API forA
. Even if you could do what you propose, how would any user ofA
know what methods / fields are available ifA
is not defined somewhere? ForA
to be useful, you need to have an API thatA
implements.不确定我是否完全理解。但模式是这样的:
问题编辑后:
最简单的方法是重写方法:
然后 A 可以调用 onSomeEvent。
Not sure whether fully understood by me. But the pattern is like this:
AFTER QUESTION EDITED:
The simplest way is to override a method:
Then A can call onSomeEvent.