定义方法调用者在方法内部创建的匿名类的方法

发布于 2024-12-21 07:43:13 字数 1540 浏览 4 评论 0原文

在我现在拥有的一个类中,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画▽骨i 2024-12-28 07:43:13

类似以下内容通常有效:

public A createAClass(final String value){
      return new A(){
         // some code here that can access value
      };
}

如果您正在寻找其他内容,请澄清问题。

编辑

答案是否定的,你不能这样做。您正在尝试创建一个没有为 A 定义 API 的 A。即使您可以按照您的建议进行操作,如果 A 未在某处定义,A 的任何用户如何知道哪些方法/字段可用?要使 A 发挥作用,您需要有一个 A 实现的 API。

Something like the following usually works:

public A createAClass(final String value){
      return new A(){
         // some code here that can access value
      };
}

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 for A. Even if you could do what you propose, how would any user of A know what methods / fields are available if A is not defined somewhere? For A to be useful, you need to have an API that A implements.

岁月蹉跎了容颜 2024-12-28 07:43:13

不确定我是否完全理解。但模式是这样的:

public class Here {
    private int stuff;
    public class A {
        private A() { ... }
        ... ++stuff; ...
    }
    public A createA() { ... }
}
...

Here here = ...
A a = here.createA();

问题编辑后:

最简单的方法是重写方法:

final Object stuff = ...;
A a = new A() {
   @Override
   protected void onSomeEvent() {
       ... stuff.toString();
   }
}

然后 A 可以调用 onSomeEvent。

Not sure whether fully understood by me. But the pattern is like this:

public class Here {
    private int stuff;
    public class A {
        private A() { ... }
        ... ++stuff; ...
    }
    public A createA() { ... }
}
...

Here here = ...
A a = here.createA();

AFTER QUESTION EDITED:

The simplest way is to override a method:

final Object stuff = ...;
A a = new A() {
   @Override
   protected void onSomeEvent() {
       ... stuff.toString();
   }
}

Then A can call onSomeEvent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文