anonymous-class

The Anonymous Class Conundrum

冷暖自知 提交于 2019-11-29 17:51:59
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 for the anonymous class so it can be referenced (but I couldn't find it). When an object of A is

When do I need anonymous class in C++?

两盒软妹~` 提交于 2019-11-29 17:11:50
问题 There's a feature called anonymous class in C++. It's similar with anonymous struct in C. I think this feature is invented because of some needs, but I can't figure out what that is. Can I have some example which really needs anonymous class? 回答1: The feature is there because struct and class are the same thing - anything you can do with one, you can do with the other. It serves exactly the same purpose as an anonymous struct in C; when you want to group some stuff together and declare one or

Declaring a method when creating an object

∥☆過路亽.° 提交于 2019-11-29 16:01:12
问题 Why first way is correct, but second isn't? First way: new Object() { public void a() { /*code*/ } }.a(); Second way: Object object = new Object() { public void a() { /*code*/ } }; object.a(); And where can I find more information about it? 回答1: java.lang.Object has no a methods declared (2), while the anonymous class returned by the class instance creation expression new Object() { public void a() {} } does (1). Use Java 10's local variable type inference ( var ) to make the second option as

Why does adding a public field to an anonymous class in Java not work?

痞子三分冷 提交于 2019-11-29 09:31:15
I have an example class defined like below: public class FooBar { void method1(Foo foo){ // Should be overwritten ... } } Later, when I try this: FooBar fooBar = new FooBar(){ public String name = null; @Override void method1(Foo foo){ ... } }; fooBar.name = "Test"; I get an error saying that the name field does not exist. Why? Because the type of the variable "fooBar" is FooBar (the run-time type of the object in said variable is that of the anonymous class implementing FooBar which is also a subtype of FooBar )... ...and the type FooBar does not have said member. Hence, a compile error.

Interface-implementing anonymous class in C#? [duplicate]

Deadly 提交于 2019-11-29 09:06:39
This question already has an answer here: Can anonymous class implement interface? 8 answers Is there a construct in C# which allows you to create a anonymous class implementing an interface, just like in Java? As has already been stated, no, this is not possible. However, you can make a class that implements the desired interface and accepts a lambda in it's constructor so that you can turn a lambda into a class that implements the interface. Example: public class LambdaComparer<T> : IEqualityComparer<T> { private readonly Func<T, T, bool> _lambdaComparer; private readonly Func<T, int>

Java anonymous class that implements ActionListener?

血红的双手。 提交于 2019-11-29 04:46:40
I was recently doing a programming assignment that required us to implement in code a program specified by a UML diagram. At one point, the diagram specified that I had to create an anonymous JButton that displayed a count (starting at one) and decremented each time it was clicked. The JButton and its ActionListener both had to be anonymous. I came up with the following solution: public static void main(String[] args) { JFrame f = new JFrame("frame"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(400, 400); f.getContentPane().add(new JButton() { public int counter; { this.counter

Dynamic construction of anonymous class confusion

微笑、不失礼 提交于 2019-11-29 03:25:30
I'm trying to make instances of anonymous classes using reflection. But ocassionally I've seen strange behaviour during instantination. Please, look at these similar fragments of code public class HideAndSeek { @SuppressWarnings("unchecked") public static void main(String[] args) throws IllegalAccessException, InstantiationException{ final String finalString = "I'm final :)"; Object object2 = new Object(){ { System.out.println("Instance initializing block"); System.out.println(finalString); } private void hiddenMethod() { System.out.println("Use reflection to find me :)"); } }; Object tmp =

Anonymous classes in PHP 7

倖福魔咒の 提交于 2019-11-29 02:59:29
Where can i use and should i use anonymous classes that are presented in PHP 7 ? I can't find a use case for them. $message = (new class() implements Message { public function getText() { return "Message"; }}); You can find the information you are looking for here , where the RFC is presented. The key points of the section Use cases are the following: Mocking tests becomes easy as pie. Create on-the-fly implementations for interfaces, avoiding using complex mocking APIs. Keep usage of these classes outside the scope they are defined in Avoid hitting the autoloader for trivial implementations

Passing final variables to anonymous classes

有些话、适合烂在心里 提交于 2019-11-29 02:11:52
问题 In final variable passed to anonymous class via constructor, Jon Skeet mentioned that variables are passed to the anonymous class instance via an auto-generated constructor. Why would I not be able to see the constructor using reflection in that case: public static void main(String... args) throws InterruptedException { final int x = 100; new Thread() { public void run() { System.out.println(x); for (Constructor<?> cons : this.getClass() .getDeclaredConstructors()) { StringBuilder str = new

C#: Creating an instance of an abstract class without defining new class

左心房为你撑大大i 提交于 2019-11-29 01:55:26
问题 I know it can be done in Java, as I have used this technique quite extensively in the past. An example in Java would be shown below. (Additional question. What is this technique called? It's hard to find an example of this without a name.) public abstract class Example { public abstract void doStuff(); } public class StartHere{ public static void main(string[] args){ Example x = new Example(){ public void doStuff(){ System.out.println("Did stuff"); } }; x.doStuff(); } } Now, my main question