anonymous-class

Anonymous delegate implementation in Objective-C?

荒凉一梦 提交于 2019-11-30 11:54:39
问题 Is it possible to declare anonymous implementations of things like Delegates in Objective-C. I think I have the terminology right, but here's a java example: myClass.addListener(new FancyInterfaceListener({ void onListenerInterestingAction(Action a){ ....interesting stuff here } }); So for example to handle an UIActionSheet call I have to declare another method in the same class, which seems a bit silly if I want to pass it data, because I'd have to store that data as a global variable. Here

When do I need anonymous class in C++?

。_饼干妹妹 提交于 2019-11-30 11:49:36
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? 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 more instances of it, but don't need to refer to that type by name. It's less commonly used in C++, partly

Can I create anonymous classes in C++ and capture the outside variables like in Java?

别来无恙 提交于 2019-11-30 10:30:01
问题 In Java, when I need a callback function, I have to implement an anonymous class. Inside the anonymous class, I can access the outside variables if they're final . Now I'm doing the same thing in C++. I understand that C++ lambda works better but sometimes I need to pass in many functions where with anonymous classes, I only need to pass in one instance. I tried the following example. It works with GCC 4.3.4. class IA { public: virtual int f(int x) = 0; }; int main() { class : public IA { int

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

牧云@^-^@ 提交于 2019-11-30 08:02:42
问题 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? 回答1: 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

Accessing inner anonymous class members

三世轮回 提交于 2019-11-30 04:49:41
问题 Is there any way other than using reflection to access the members of a anonymous inner class? 回答1: Anonymous inner classes have a type but no name. You can access fields not defined by the named supertype. However once assigned to a named type variable, the interface is lost. Obviously, you can access the fields from within the inner class itself. One way of adding code is through an instance initialiser: final AtomicInteger y = new AtomicInteger(); new Runnable() { int x; { x = 5; doRun

Passing final variables to anonymous classes

﹥>﹥吖頭↗ 提交于 2019-11-30 04:48:16
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 StringBuilder(); str.append("constructor : ").append(cons.getName()) .append("("); for (Class<?> param

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

自闭症网瘾萝莉.ら 提交于 2019-11-30 04:39:34
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 would be, can this also be done in C#, and if so, how? With lamba expressions and class initializers

Singletons, Enums and anonymous inner classes

社会主义新天地 提交于 2019-11-30 04:17:06
问题 As you may know, some people are declaring singletons with an Enum of 1 instance, because the JVM guarantees that there will always be a single instance with no concurrency problems to handle... Thus what about an Enum with multiple instances? Can we say something like an Enum is a kind of ordered set of singletons sharing a common interface? Why? public enum EnumPriceType { WITH_TAXES { @Override public float getPrice(float input) { return input*1.20f; } public String getFormattedPrice(float

Anonymous delegate implementation in Objective-C?

让人想犯罪 __ 提交于 2019-11-30 02:03:29
Is it possible to declare anonymous implementations of things like Delegates in Objective-C. I think I have the terminology right, but here's a java example: myClass.addListener(new FancyInterfaceListener({ void onListenerInterestingAction(Action a){ ....interesting stuff here } }); So for example to handle an UIActionSheet call I have to declare another method in the same class, which seems a bit silly if I want to pass it data, because I'd have to store that data as a global variable. Here's an example of deleting something with a confirmation dialog asking you if your sure: -(void

Can I create anonymous classes in C++ and capture the outside variables like in Java?

心已入冬 提交于 2019-11-29 20:37:21
In Java, when I need a callback function, I have to implement an anonymous class. Inside the anonymous class, I can access the outside variables if they're final . Now I'm doing the same thing in C++. I understand that C++ lambda works better but sometimes I need to pass in many functions where with anonymous classes, I only need to pass in one instance. I tried the following example. It works with GCC 4.3.4. class IA { public: virtual int f(int x) = 0; }; int main() { class : public IA { int f(int x) { return x + 1; } } a; doFancyWork(&a); return 0; } Is it possible to capture the outside