nested-class

Generic base class wraps nested generic class to reduce type argument specification: Is there a name for this pattern?

青春壹個敷衍的年華 提交于 2020-01-11 07:27:06
问题 Ok question title is far from being self-explanatory. I see myself doing this often: From this answer: public static class Equality<T> { public static IEqualityComparer<T> CreateComparer<K>(Func<T, K> keySelector) { return new KeyEqualityComparer<K>(keySelector); } class KeyEqualityComparer<K> : IEqualityComparer<T> { readonly Func<T, K> keySelector; public KeyEqualityComparer(Func<T, K> keySelector) { this.keySelector = keySelector; } public bool Equals(T x, T y) { ---- } public int

Why can't I declare and initialize static variable in inner class? [duplicate]

北慕城南 提交于 2020-01-03 02:40:11
问题 This question already has answers here : Why does Java prohibit static fields in inner classes? (8 answers) Closed 3 years ago . class A is the outer class. Class B is an inner class of A, and class C is an inner class of B. All three classes declare and initialize one static final variable. This is the object in class A: static final Object x = new Object(); class B: static final Object x1 = new Object(); class C: static final Object x2 = new Object(); The problem is that the variable in

Inheriting Nested classes into Subclass

谁说我不能喝 提交于 2020-01-01 10:17:06
问题 When I was going through this article, under the section Private Members in a Superclass , i saw this line A nested class has access to all the private members of its enclosing class—both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass. My question is how can we DIRECTLY access the Nested class of Base in Derived (like we can access any public , protected fields)? and if there is a

Inheriting Nested classes into Subclass

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-01 10:15:34
问题 When I was going through this article, under the section Private Members in a Superclass , i saw this line A nested class has access to all the private members of its enclosing class—both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass. My question is how can we DIRECTLY access the Nested class of Base in Derived (like we can access any public , protected fields)? and if there is a

Declare array of fixed length in nested classes

笑着哭i 提交于 2019-12-31 02:57:46
问题 I have a class A, which has a nested class B. Class A, will create n(run time parameter) instances of class B. In the constructor of A, after computations that need to be made at run time, I compute a size, let's say s. Now, every class B, will hold an array of size s. However, I am not allowed to use .cpp files and all the work must be done in header files. This means, as far as I understand, that I can not use these approach (with static ): class B { static const int s; int a[s]; }; So, I

Why can I access the private members of an enclosing class reference

折月煮酒 提交于 2019-12-31 02:36:08
问题 I have seen many questions about accessing private members of an enclosing class. However, my question is the opposite. If I have (as an example), the following code: public class A { private String outerString = "silly string"; static class B { private final A someA = new A(); public void foo() { String b = someA.outerString ; } } } I'm wondering why this compiles? I would have expected an error by virtue of the way in which I am accessing the 'outerString' instance variable from class A

Why outer classes are not static in java? [duplicate]

随声附和 提交于 2019-12-30 11:34:12
问题 This question already has answers here : Why are you not able to declare a class as static in Java? (14 answers) Closed 6 years ago . In java, An outer class may be public, final, default or abstract. Why not Static like public static class MyClass{} 回答1: An outer class is already implicitly static. Non-static nested class (= inner class) means thats the inner class implicitly has a reference to its parent class. That's why, for nested class, you can distinguish between static and non-static.

Are static inner classes a good idea or poor design?

柔情痞子 提交于 2019-12-29 08:43:05
问题 I'm find I have several places that having public static inner classes designed that extend "helper" classes makes my code a lot more type safe and, in my opinion, readable. For example, imagine I have a "SearchCriteria" class. There are a lot of commonalities for the different things I search for (a search term and then a group of search term types, a date range, etc.) By extending it in a static inner class, I tightly couple the extension and the searchable class with the specific

Are static inner classes a good idea or poor design?

て烟熏妆下的殇ゞ 提交于 2019-12-29 08:42:26
问题 I'm find I have several places that having public static inner classes designed that extend "helper" classes makes my code a lot more type safe and, in my opinion, readable. For example, imagine I have a "SearchCriteria" class. There are a lot of commonalities for the different things I search for (a search term and then a group of search term types, a date range, etc.) By extending it in a static inner class, I tightly couple the extension and the searchable class with the specific

Nested type problem

此生再无相见时 提交于 2019-12-29 01:38:08
问题 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? 回答1: You are probably coming from a Java background where this code would work as expected. In C#, nested types are