nested-class

nested classes in Python

假如想象 提交于 2019-11-29 01:13:59
问题 Dealing with classes (nested etc) does not look easy in Python , surprisingly ! The following problem appeared to me recently and took several hours (try, search ...) without success. I read most of SO related links but none of them has pointed the issue presented here! #------------------------------------ class A: def __init__(self): self.a = 'a' print self.a class B(A): def __init__(self): self.b = 'b' A.a = 'a_b' print self.b, A.a #------------------------------------ class C: class A:

“Public” nested classes or not

独自空忆成欢 提交于 2019-11-28 23:30:08
问题 Suppose I have a class 'Application'. In order to be initialised it takes certain settings in the constructor. Let's also assume that the number of settings is so many that it's compelling to place them in a class of their own. Compare the following two implementations of this scenario. Implementation 1: class Application { Application(ApplicationSettings settings) { //Do initialisation here } } class ApplicationSettings { //Settings related methods and properties here } Implementation 2:

GC performance hit for inner class vs. static nested class

偶尔善良 提交于 2019-11-28 22:58:30
I just came across a weird effect and while tracking it down, I noticed that there seems to be a substantial performance difference for collecting inner vs. static nested classes. Consider this code fragment: public class Test { private class Pointer { long data; Pointer next; } private Pointer first; public static void main(String[] args) { Test t = null; for (int i = 0; i < 500; i++) { t = new Test(); for (int j = 0; j < 1000000; j++) { Pointer p = t.new Pointer(); p.data = i*j; p.next = t.first; t.first = p; } } } } So what the code does is create a linked list using an inner class. The

Issue with constructors of nested class

社会主义新天地 提交于 2019-11-28 20:40:57
This question is about interesting behavior of Java: it produces additional (not default) constructor for nested classes in some situations. This question is also about strange anonymous class, which Java produces with that strange constructor. Consider the following code: package a; import java.lang.reflect.Constructor; public class TestNested { class A { A() { } A(int a) { } } public static void main(String[] args) { Class<A> aClass = A.class; for (Constructor c : aClass.getDeclaredConstructors()) { System.out.println(c); } } } This will prints: a.TestNested$A(a.TestNested) a.TestNested$A(a

What is an anonymous inner class?

狂风中的少年 提交于 2019-11-28 14:30:49
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() { int OuterClassVariable = 10; System.out.println("OuterClassVariable" + " " + OuterClassVariable); } }

Nested type problem

守給你的承諾、 提交于 2019-11-28 13:34:09
I just tried to create this simple implementation: class Test { private int abc = 0; public class TestClass { private void changeABC() { abc = 123; } } } If I compile it, it will complain: Cannot access a non-static member of outer type 'A.Test' via nested type 'B.Test.TestClass' I dont like the solution of setting: static int abc = 0; Is there any other solution for this? You are probably coming from a Java background where this code would work as expected. In C#, nested types are static (in the parlance of Java), i.e. they are not bound to an instance of the parent class. This is why your

Class.Class vs Namespace.Class for top level general use class libraries?

China☆狼群 提交于 2019-11-28 12:32:53
Which one is more acceptable (best-practice)?: namespace NP public static class IO public static class Xml ... // extension methods using NP; IO.GetAvailableResources (); vs public static class NP public static class IO public static class Xml ... // extension methods NP.IO.GetAvailableResources (); Also for #2 , the code size is managed by having partial classes so each nested class can be in a separate file, same for extension methods (except that there is no nested class for them) I prefer #2 , for a couple of reasons like being able to use type names that are already commonly used, like IO

C++: nested class of a template class

喜你入骨 提交于 2019-11-28 10:55:33
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/static_assert.hpp> #include <boost/type_traits/is_same.hpp> template < typename T > struct A { typedef

Create array of inner class object in a different class

青春壹個敷衍的年華 提交于 2019-11-28 10:37:53
Consider the following nested classes. class Outerclass { class innerclass { } } class util { //how to declare an array of innerclass objects here? } 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(); } OuterClass outerObject = new OuterClass(); OuterClass.InnerClass innerArray[] = new OuterClass.InnerClass[3]; // Creating Objects of Inner Class

PHP Nested classes work… sort of?

徘徊边缘 提交于 2019-11-28 10:10:19
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 work? From the manual ( http://php.net/manual/en/function.include.php ): When a file is included, the