nested-class

Using nested enum types in Java

℡╲_俬逩灬. 提交于 2019-11-27 11:55:42
I have a data structure in mind that involves nested enums, such that I could do something like the following: Drink.COFFEE.getGroupName(); Drink.COFFEE.COLUMBIAN.getLabel(); And if there were method declarations: someMethod(Drink type) someOtherMethod(DrinkTypeInterface type) Then I could say (appropriately): someMethod(Drink.COFFEE) someOtherMethod(Drink.COFFEE.COLUMBIAN) This is what I came up with: public enum Drink { COFFEE("Coffee"); private String groupName; private Drink(String groupName) { this.groupName = groupName; } public enum Coffee implements DrinkTypeInterface { COLUMBIAN(

What is an anonymous inner class?

守給你的承諾、 提交于 2019-11-27 08:33:07
问题 When I tried to do some sample on an abstract class in Java I accidentally got some thing like anonymous inner class in Eclipse. I have pasted the piece of code below. I don't understand how the abstract class is related to anonymous class. package com.Demo; abstract class OuterClass { abstract void OuterClassMethod(); } public abstract class InnerClass extends OuterClass { public static void main(String[] args) { InnerClass myInnerClass = new InnerClass() { @Override void OuterClassMethod()

Creating an instance of a nested class in XAML

自古美人都是妖i 提交于 2019-11-27 07:47:05
in a XAML file (a WPF UserControl), is there a way to reference an inner class "B" defined in another class "A" ? public class A { public class B { } } Something like : <local:A.B ... /> This syntax does not work because "B" is interpreted as a property named "B" in class "A". I've tried more exotic syntaxes like "::" or "+" but none seems to work. I'm currently using Silverlight 4 with VS2010 . Thanks in advance for your help. Ashley Grenon I was searching and searching, because if this is possible, I would like to know. Unfortunately, I found this on msdn : Your custom class must not be a

Why instantiation of static nested class object is allowed?

China☆狼群 提交于 2019-11-27 05:42:45
问题 I have started learning Java language for Android Application developement. As per my understanding based on static class, we cannot instantiate object of static class. But why instantiation of static nested class object is allowed in following situaltion? class EnclosingClass { //... class static StaticInnerClass { //... } } Why we can create object of inner class if it is marked as static? EnclosingClass.StaticInnerClass s = new EnclosingClass.StaticInnerClass() 回答1: As per my understanding

Inner class in interface vs in class

送分小仙女□ 提交于 2019-11-27 05:22:06
问题 What is the difference between these two innerclass declarations? Also comment on advantages/disadvantages? case A: class within a class. public class Levels { static public class Items { public String value; public String path; public String getValue() { return value;} } } and case B: class within interface. public interface Levels{ public class Items { public String value; public String path; public String getValue() { return value;} } } Made correction: to placement of getvalue method.

What are reasons why one would want to use nested classes? [duplicate]

て烟熏妆下的殇ゞ 提交于 2019-11-27 03:58:20
This question already has an answer here: Why/when should you use nested classes in .net? Or shouldn't you? 13 answers In this stackoverflow answer a commenter mentioned that " private nested classes" can be quite useful so I was reading about them in articles such as this one which tend to explain how nested classes function technically , but not why you would use them. I suppose I would use private nested classes for little helper classes that belong to a larger class, but often I will need a helper class from another class and so I would just have to take the extra effort to (1) make the

C++: nested class of a template class

☆樱花仙子☆ 提交于 2019-11-27 03:54:37
问题 Consider the following code: template < typename T > struct A { struct B { }; }; template < typename T > void f( typename A<T>::B ) { } int main() { A<int>::B x; f( x ); // fails for gcc-4.1.2 f<int>( x ); // passes return 0; } So here gcc-4.1.2 requires the template argument of f to be explicitly specified. Is this meet the standard? Does the newer versions of GCC have this issue fixed? How can I avoid explicitly specifying int while calling f ? Update: Here is a workaround. #include <boost

Create array of inner class object in a different class

久未见 提交于 2019-11-27 03:49:49
问题 Consider the following nested classes. class Outerclass { class innerclass { } } class util { //how to declare an array of innerclass objects here? } 回答1: You can declare an array of innerclass objects like this. class util { Outerclass.innerclass[] inner = new Outerclass.innerclass[10]; } And to instantiate them you can do something like this inside the util class. void test() { Outerclass outer = new Outerclass(); inner[0] = outer.new innerclass(); } 回答2: OuterClass outerObject = new

PHP Nested classes work… sort of?

扶醉桌前 提交于 2019-11-27 03:28:24
问题 So, if you try to do a nested class like this: //nestedtest.php class nestedTest{ function test(){ class E extends Exception{} throw new E; } } You will get an error Fatal error: Class declarations may not be nested in [...] but if you have a class in a separate file like so: //nestedtest2.php class nestedTest2{ function test(){ include('e.php'); throw new E; } } //e.php class E Extends Exception{} So, why does the second hacky way of doing it work, but the non-hacky way of doing it does not

Nested Class member function can't access function of enclosing class. Why?

半腔热情 提交于 2019-11-27 03:09:33
问题 Please see the example code below: class A { private: class B { public: foobar(); }; public: foo(); bar(); }; Within class A & B implementation: A::foo() { //do something } A::bar() { //some code foo(); //more code } A::B::foobar() { //some code foo(); //<<compiler doesn't like this } The compiler flags the call to foo() within the method foobar(). Earlier, I had foo() as private member function of class A but changed to public assuming that B's function can't see it. Of course, it didn't