anonymous-class

Is there a way to convert a dynamic or anonymous object to a strongly typed, declared object?

a 夏天 提交于 2019-12-18 18:37:10
问题 If I have a dynamic object, or anonymous object for that matter, whose structure exactly matches that of a strongly typed object, is there a .NET method to build a typed object from the dynamic object? I know I can use a LINQ dynamicList.Select(dynamic => new Typed { .... } type thing, or I can use Automapper, but I'm wondering if there is not something specially built for this? 回答1: You could serialize to an intermediate format, just to deserialize it right thereafter. It's not the most

The Anonymous Class Conundrum

ⅰ亾dé卋堺 提交于 2019-12-18 09:37:53
问题 I think I understand the basics of Anonymous classes but I'd like to clarify something. when I have a syntax such as this class A { class AnonymousClass1 Implements ActionListener{} } class A { public A() { JButton btn = new JButton(); btn.addActionListener( new ActionListener(){} ); } } If the anonymous class is actually an inner class of class A, as in the first example: in theory, is the semantics right? What happens exactly? I think when the java file is compiled, a .class file is created

Java: Anonymous inner class using a local variable

六眼飞鱼酱① 提交于 2019-12-17 18:59:55
问题 How can I get the value of userId passed to this method in my anonymous inner subclass here? public void doStuff(String userID) { doOtherStuff(userID, new SuccessDelegate() { @Override public void onSuccess() { Log.e(TAG, "Called delegate!!!! "+ userID); } }); } I get this error: Cannot refer to a non-final variable userID inside an inner class defined in a different method I'm pretty sure I can't assign it as final since it's a variable with an unknown value. I had heard that this syntax

What does a variable being “effectively final” mean? [duplicate]

眉间皱痕 提交于 2019-12-17 16:32:55
问题 This question already has answers here : Difference between final and effectively final (13 answers) Closed 5 years ago . The documentation on Anonymous Classes states An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively final. I don't understand what does a variable being "effective final" mean. Can someone provide an example to help me understand what that means? 回答1: Effectively final means that it is never changed after

How to start anonymous thread class

旧城冷巷雨未停 提交于 2019-12-17 15:24:50
问题 I have the following code snippet: public class A { public static void main(String[] arg) { new Thread() { public void run() { System.out.println("blah"); } }; } } Here, how do I call the start() method for the thread without creating an instance of the thread class? 回答1: You're already creating an instance of the Thread class - you're just not doing anything with it. You could call start() without even using a local variable: new Thread() { public void run() { System.out.println("blah"); } }

Anonymous vs named inner classes? - best practices?

笑着哭i 提交于 2019-12-17 10:23:22
问题 I have a class, let's call it LineGraph, that renders a line graph. I need to subclass it, but the derived class is only used in one place and is coupled to the class that uses it. So I am using an inner class. I see two ways to do this: Anonymous inner class public class Gui { LineGraph graph = new LineGraph() { // extra functionality here. }; } Named inner class public class Gui { MyLineGraph graph = new MyLineGraph(); private class MyLineGraph extends LineGraph { // extra functionality

Local variable access to inner class needs to be declared final

馋奶兔 提交于 2019-12-17 09:49:35
问题 I got a problem of local variable access to inner class need to be declared final. It is from method createGrids() -> " squares[i][j] = 0; " that i is a local variable that need to be declared final. I don't know why and I have added final in fields but it is not working as well. import java.util.ArrayList; import java.util.Random; //omitted public class Minesweeper{ private JFrame frame; private int cols = 9; private int rows = 9; public static final int GRID_HEIGHT = 9; public static final

Do anonymous classes *always* maintain a reference to their enclosing instance?

♀尐吖头ヾ 提交于 2019-12-17 05:01:53
问题 I'm working with some code where one object, "foo", is creating another object, "bar", and passing it a Callable . After this foo will return bar, and then I want foo to become unreachable (ie: available for garbage collection). My initial thought was to just create the Callable anonymously. eg: class Foo { ... public Bar createBar() { final int arg1 = ... final int arg2 = ... final int arg3 = ... return new Callable<Baz>() { @Override public Baz call() { return new Baz(arg1, arg2, arg3); } }

Access “this” from Java anonymous class

☆樱花仙子☆ 提交于 2019-12-17 03:53:23
问题 Given the following code: public interface Selectable { public void select(); } public class Container implements Selectable { public void select() { ... } public void createAnonymousClass() { Selectable s = new Selectable() { public void select() { //see comment below. } }; } } I want to access Container.select() from within my anonymous class' select() method. However, this.select() would again call the anonymous class' select() method. My suggestion would be: Introduce a field into

Java 8 Lambda Expressions - what about multiple methods in nested class

南笙酒味 提交于 2019-12-17 02:28:55
问题 I'm reading about the new features at: http://www.javaworld.com/article/2078836/java-se/love-and-hate-for-java-8.html I saw the example below: Using Anonymous Class: button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { System.out.println("Action Detected"); } }); With Lambda: button.addActionListener(e -> { System.out.println("Action Detected"); }); What would someone do with a MouseListener if they wanted to implement multiple methods within the