inner-classes

how to restrict setting a property of innerclass just from the outer class in c#

爱⌒轻易说出口 提交于 2019-12-02 13:09:34
I have the nub of the code like this: public class OuterClass { public static InnerClass GetInnerClass() { return new InnerClass() { MyProperty = 1 }; } public class InnerClass { public int MyProperty { get; set; } } } what is the solution to property named MyProperty just be settable from the InnerClass and the OuterClass , and out of these scopes, MyProperty just be readonly There is no protection level for that. internal is the tightest you can use, which is limited to files in the same assembly. If you cannot make it a constructor parameter as has been proposed, you could use an interface:

Are anonymous classes a subset of inner class?

随声附和 提交于 2019-12-02 13:07:23
It might sound like a dumb question, but all anonymous classes must be defined and instantiated within an existing class; therefore, they must be inner classes at the same time. This is quite true. Your anonymus class could not be implemented outside of other classes, as a seperate class, because as it is anonymus you would not be even able to refer to it in any way. Additional information: From JLS: An anonymous class is always an inner class (§8.1.3); it is never static (§8.1.1, §8.5.1). Anonymous classes are types of inner classes. See http://docs.oracle.com/javase/tutorial/java/javaOO

creating inner class objects with reflection

故事扮演 提交于 2019-12-02 12:44:29
How do you create an inner class object with reflection? Both Inner and Outer classes have default constructors that take no parameters Outer class { Inner class{ } public void createO() { Outer.Inner ob = new Inner ();//that works Inner.class.newInstance(); //<--why does this not compile? } } "If the constructor's declaring class is an inner class in a non-static context, the first argument to the constructor needs to be the enclosing instance; see section 15.9.3 of The Java™ Language Specification ." That means you can never construct an inner class using Class.newInstance ; instead, you

Custom Enum in Java

早过忘川 提交于 2019-12-02 12:06:13
I need to implement custom Enum. It should some class which implements Enum methods like valueOf, values and ordinal without directly extending java.lang.Enum. I have been thinking about creating it in such a way where instead of the String should be some generic type T: public class CustomEnum { private static int size; private static final InnerEnum enumValues; public CustomEnum(){ } public String valueOf(String innerEnum, String name){ if(name.equals(innerEnum)){ return innerEnum; } return null; } public InnerEnum values(){ return enumValues; } public int ordinal(){} public static class

Scala abstract types in classes within objects

柔情痞子 提交于 2019-12-02 08:54:33
If I do this: object Parent { class Inner extends Testable { type Self <: Inner } def inner = new Inner() } object Child { class Inner extends Parent.Inner { type Self <: Inner } def inner = new Inner() } trait Testable { type Self def test[T <: Self] = {} } object Main { // this works val p: Parent.Inner = Child.inner // this doesn't val h = Parent.inner h.test[Child.Inner] } I get this error: error: type arguments [Child.Inner] do not conform to method test's type parameter bounds [T <: Main.h.Self] h.test[Child.Inner] Why does this error when I my Self type is Parent.Inner and Child.Inner <

Java - How to access Outer class field if the fields have same name

陌路散爱 提交于 2019-12-02 08:46:15
Consider the following code class OuterClass{ class InnerClass{ int x; int y; void calculateX(){ x = y+z;//I want to access the y field of the outer class } void printX(){ print(); } } int y; int z; InnerClass instance; OuterClass(int y,int z){ this.y = y; this.z = z; instance = new InnerClass(); instance.y = 10; instance.calculateX(); instance.printX(); } void print(){ System.out.println("X:"+instance.x+"\nY:"+y+"\nZ:"+z+"\n"); } } How to access field of the outer class if there is any overlap in name? I have tried the following: x=super.y; x=OuterClass.y; and received compilation error. Will

Must Inner classes have a reference to the enclosed class?

ⅰ亾dé卋堺 提交于 2019-12-02 08:42:49
I have an inner class (non-static) which is using a reference to an enclosing class in its initialization. Will the inner class keep a reference to the enclosing class now? class Enclosing { class Inner { private final ABC innerField = outerField.computeSomething(); } private final XYZ outerField = something(); } UPDATE I am very much aware that one can reference the outer class with Enclosing.this . But, if the class doesn't use the reference, must the reference be there after compilation? Is it necessary even if the reference is only used in the initialization? Where does it say that an

How can I instantiate a member class through reflection on Android

偶尔善良 提交于 2019-12-02 08:36:54
问题 I have a saving/loading framework that is supposed to save arbitrary object graphs. This includes instances of non-static nested classes. Nested classes require are owned by the instance of their parent class that created them, and the nested class knows what instance it belongs to using a synthetic field. As a simple example, I present this class: public class Foo implements Savable { private class Bar implements Savable { public void saveState(Saver saver) { saver.putInt(3); } } private Bar

Android databinding on inner class not updating TextView

怎甘沉沦 提交于 2019-12-02 07:49:49
App runs but the TextView doesn't get update here is the relevant code. activity_picker_dashboard.xml <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Top header --> <include layout="@layout/layout_header" /> This layout has databinding <!-- DrawerLayout --> <android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout" android:layout_width="0dp" android:layout_height="0dp" android:background="@color

Compiler is creating extra class files with $ in them

两盒软妹~` 提交于 2019-12-02 06:37:29
I'm using Eclipse and I've written a Java application using SWT. When Eclipse compiles my program, it renames my main file into 4 different files like this: MainFile.class MainFile$1.class MainFile$2.class MainFile$3.class When I go to run this program from command line, I get Could not find the main class: MainFile.class. Program will exit. I really don't understand why this is happening. Louis Wasserman The $ classes are for anonymous inner classes and perfectly normal. Could we see the command line you ran? You probably need to write java MainFile instead of java MainFile.class . 来源: https: