anonymous-class

Anonymous classes

故事扮演 提交于 2019-12-23 12:42:28
问题 In Java, it's common to write the following (e.g. for event handling) in order to make use of the template method pattern: abstract class SomeAbstractClass { public abstract void SomeFunction (); } //... SomeAbstractClass obj = new SomeAbstractClass () { public void SomeFunction () { /* implementation */ } }; In C++, the following compiles: class SomeAbstractClass { virtual void SomeFunction () = 0; }; // ... SomeAbstractClass * obj = new ( class : public SomeAbstractClass { virtual void

Can an anonymous type inherit from another type?

假装没事ソ 提交于 2019-12-23 07:51:33
问题 According to the MSDN documentation on the StringComparer.OrdinalIgnoreCase property: The OrdinalIgnoreCase property actually returns an instance of an anonymous class derived from the StringComparer class. Is this a feature I'm unfamiliar with—anonymous types with inheritance? Or by "anonymous class" did the author simply mean "internal class deriving from StringComparer , not visible to client code"? 回答1: If you look at the source code for StringComparer, you can see that OrginalIgnoreCase

Can anonymous class be used as return types in C++?

眉间皱痕 提交于 2019-12-21 03:48:51
问题 Is there any way to use anonymous classes in C++ as return types? I googled that this may work: struct Test {} * fun() { } But this piece of code doesn't compile, the error message is: new types may not be defined in a return type Actually the code doesn't make any sense, I just want to figure out whether an anonymous class can be used as return type in C++. Here is my code: #include <typeinfo> #include <iterator> #include <iostream> #include <fstream> #include <cstring> #include <cstdlib>

Best practice for inner/anonymous classes [closed]

[亡魂溺海] 提交于 2019-12-21 01:59:29
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed last year . What's the best practise(design and performance wise) for anonymous classes and static inner classes? Personally I would like to think that static inner classes provide better encapsulation and should give better performance as they dont have access to finalized variables

Can I specify a meaningful name for an anonymous class in C#?

会有一股神秘感。 提交于 2019-12-20 09:46:30
问题 We all know that when we create an anonymous class like this: var Employee = new { ID = 5, Name= "Prashant" }; ...at run time it will be of type: <>f__AnonymousType0<int,string> Is there any way to specify a meaningful name to such classes? 回答1: public class Employee {} 回答2: It's an anonymous type, that defeats the purpose. Those objects are designed to be temporary. Hell, the properties are even read-only. Sorry, I'm being a smart-ass. The answer is no, there is no way to tell the compiler

setOnCheckedChangeListener arguments

牧云@^-^@ 提交于 2019-12-20 07:26:43
问题 RadioGroup radioGroup = (RadioGroup) findViewById(R.id.orientation); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.horizontal: group.setOrientation(LinearLayout.HORIZONTAL); break; case R.id.vertical: group.setOrientation(LinearLayout.VERTICAL); break; } } }); What is radioGroup.setOnCheckedChangeListener eactly? Is it a method? What are it's arguments then? 回答1:

How can I access enclosing class instance variables from inside the anonymous class?

a 夏天 提交于 2019-12-20 03:21:46
问题 How do I access instance variables from inside the anonymous class's method ? class Tester extends JFrame { private JButton button; private JLabel label; //..some more public Tester() { function(); // CALL FUNCTION } public void function() { Runnable r = new Runnable() { @Override public void run() { // How do I access button and label from here ? } }; new Thread(r).start(); } } 回答1: How do I access instance variables from inside the anonymous class's method ? You simply access them if need

How can I access enclosing class instance variables from inside the anonymous class?

谁说我不能喝 提交于 2019-12-20 03:21:38
问题 How do I access instance variables from inside the anonymous class's method ? class Tester extends JFrame { private JButton button; private JLabel label; //..some more public Tester() { function(); // CALL FUNCTION } public void function() { Runnable r = new Runnable() { @Override public void run() { // How do I access button and label from here ? } }; new Thread(r).start(); } } 回答1: How do I access instance variables from inside the anonymous class's method ? You simply access them if need

Is it possible to give a definition of a class in C++ during allocation, as is allowed in java

∥☆過路亽.° 提交于 2019-12-19 19:55:11
问题 Or simply put can I do some thing like class A { public: virtual void foo() = 0; }; class B { public: A *a; b(){ a = new A() { void foo() {printf("hello");} } }; 回答1: No, C++ doesn't have anonymous classes like Java's. You can define local classes, like this: class B { public: A *a; b(){ struct my_little_class : public A { void foo() {printf("hello");} }; a = new my_little_class(); } }; Or maybe just a nested class: class B { private: struct my_little_class : public A { void foo() {printf(

Java: Access local variables from anon inner class? (PriorityQueue)

允我心安 提交于 2019-12-19 10:07:53
问题 I want to use a PriorityQueue to do a topological sort on a graph. For brevity, I'd like to use an anonymous inner class for the comparator. However, I need access to the graph g in order to determine the in degree of the nodes I'm looking at. Is this possible? /** * topological sort * @param g must be a dag */ public static Queue<String> topoSort(DirectedGraph<String, DefaultEdge> g) { Queue<String> result = new PriorityQueue<String>(g.vertexSet().size(), new Comparator<String>() {