anonymous-class

Why can't I use <Class>.this in anonymous class?

非 Y 不嫁゛ 提交于 2020-01-14 09:07:56
问题 I recently use this code, and realize that in anonymous class, I can't access the instance by .this, like this: Sprite sprFace = new Sprite() { @Override protected void onManagedUpdate(float pSecondElapsed) { runOnUpdateThread(new Runnable() { @Override protected void run() { Sprite.this.getParent().detach(Sprite.this); // Here }}); } }; I know how to solve it (just declare a "me" variable), but I need to know why I can't use <Class>.this ? 回答1: The <Class>.this syntax gives a special way of

Anonymous interface implementation

佐手、 提交于 2020-01-09 11:59:13
问题 I´ve read 'C# anonymously implement interface (or abstract class)' thread for implementing an interface anonymously. But I wondered if this is also possible using .NET 2.0 (NO LINQ) using delegates or any similar approach. I know from JAVA the following possible: MyClass m = MyMethod(new MyInterface() { @override public void doSomething() {...} } (I hope I remember well, is a time ago that I used JAVA, but I suppose it was something similar). This might be helpful whenever a method needs an

Anonymous classes in C#

孤人 提交于 2020-01-07 04:14:05
问题 In C++ I can declare a fully functional anonymous class inside a piece of code where it's needed so that I don't have to declare it if I need it only once. The code should be like this Class MyClass { Class { String string1; String string2; void MyMethod(); } Strings; } And call it the members with MyClass.Strings.string1 , MyClass.Strings.MyMethod() and so on. This way I can elegantly group my code. Is there a way to do the same thing in C#? 回答1: Try making the inner class static. That way

How can I access private class members of container class within the anonymouse inner class?

冷暖自知 提交于 2020-01-04 09:03:14
问题 How can I access all the member field of the class which contains the function initTimer() from within the AbstractActionClass? Thanks private void initTimer() { Action updateClockAction = new AbstractAction() { public void actionPerformed(ActionEvent e){ JLabel secLabel = m_GameApplet.GetJpanelStartNetGame().GetJlabelSeconds(); secLabel.setFont(new java.awt.Font("Lucida Handwriting", 1, 36)); secLabel.setForeground(Color.red); secLabel.setText(Integer.toString(m_TimerSeconds)); if(m

How can I access private class members of container class within the anonymouse inner class?

喜夏-厌秋 提交于 2020-01-04 09:02:08
问题 How can I access all the member field of the class which contains the function initTimer() from within the AbstractActionClass? Thanks private void initTimer() { Action updateClockAction = new AbstractAction() { public void actionPerformed(ActionEvent e){ JLabel secLabel = m_GameApplet.GetJpanelStartNetGame().GetJlabelSeconds(); secLabel.setFont(new java.awt.Font("Lucida Handwriting", 1, 36)); secLabel.setForeground(Color.red); secLabel.setText(Integer.toString(m_TimerSeconds)); if(m

Why can't I call a method outside of an anonymous class of the same name

喜你入骨 提交于 2020-01-04 05:38:09
问题 The code at the end produces a compile error: NotApplicable.java:7: run() in cannot be applied to (int) run(42); ^ 1 error The question is why? Why does javac think I am calling run(), and does not find run(int bar)? It correctly called foo(int bar). Why do I have to use NotApplicable.this.run(42);? Is it a bug? public class NotApplicable { public NotApplicable() { new Runnable() { public void run() { foo(42); run(42); // uncomment below to fix //NotApplicable.this.run(42); } }; } private

Boolean vs boolean(s) as trilean switches

余生长醉 提交于 2020-01-03 17:48:10
问题 I'm developing an Android application and I just ran into something. I have some anonymous classes (event listeners). They are parameterized from the database. What I did is this: buttonA.setOnTouchListener(new View.OnTouchListener() { private Boolean isActive = null; private boolean isTrigger; private int onLevel; private int offLevel; private int chIdx; @Override public boolean onTouch(View v, MotionEvent event) { if (isActive == null) { Cursor btnSettings = dbHelper.getButtonsTable()

Using $this in “anonymous” object

断了今生、忘了曾经 提交于 2020-01-02 06:56:06
问题 I am using the following class to mimic anonymous objects in PHP: class AnonymousObject { protected $methods = array(); public function __construct(array $options) { $this->methods = $options; } public function __call($name, $arguments) { $callable = null; if (array_key_exists($name, $this->methods)) $callable = $this->methods[$name]; elseif(isset($this->$name)) $callable = $this->$name; if (!is_callable($callable)) throw new BadMethodCallException("Method {$name} does not exist"); return

Unintended consequences of anonymous class created just for sake of adding instance initialization block

微笑、不失礼 提交于 2020-01-02 06:13:27
问题 This is a question about Java code such as: List<String> list = new ArrayList<String>() {{add("hello"); add("goodbye");}} where the programmer has extended ArrayList anonymously just for the sake of shoving in an instance initialization block. The question is: if the sole intention of the programmer is merely to achieve the same as: List<String> list = new ArrayList<String>(); list.add("hello"); list.add("goodbye"); then what are the unintended consequences of doing it the first way? 回答1: The

Anonymous Inner class

浪尽此生 提交于 2020-01-01 21:38:15
问题 class One { Two two() { return new Two() { Two(){} Two(String s) { System.out.println("s= "+s); } }; } } class Ajay { public static void main(String ...strings ){ One one=new One(); System.out.println(one.two()); } } The above sample code cannot be compiled.It says "Two cannot be resolved". What is the problem in this code?? 回答1: new Two() { Two(){} Two(String s) { System.out.println("s= "+s); } }; An anonymous inner class is called anonymous because it doesn't have its own name and has to be