inner-classes

Implementing inner traits in Scala like we do with inner interfaces in Java

末鹿安然 提交于 2019-11-30 03:48:37
问题 This code in Java compiles without errors: interface T { interface Q { } } class C implements T.Q { } whereas this code in Scala does not: trait T { trait Q { } } class C extends T.Q { } What is the correct translation (if it exists) of the Java code listing into Scala? Theoretical explanations about language design are welcome. 回答1: The inner type Q is defined only for specific instance implementation of the T trait. Since scala has path-dependent types, each instance of T will have his own

How does java implement inner class closures?

こ雲淡風輕ζ 提交于 2019-11-30 01:42:08
In Java an anonymous inner class can refer to variables in it's local scope: public class A { public void method() { final int i = 0; doStuff(new Action() { public void doAction() { Console.printf(i); // or whatever } }); } } My question is how is this actually implemented? How does i get to the anonymous inner doAction implementation, and why does it have to be final ? The compiler automatically generates a constructor for your anonymous inner-class, and passes your local variable into this constructor. The constructor saves this value in a class variable (a field), also named i , which will

Are Inner Classes lightweight?

主宰稳场 提交于 2019-11-29 19:16:55
问题 Are inner classes more lightweight than normal classes, or in the end java compiles inner classes just like normal classes? I know classes in java are not all very lightweight themselves, and they occupy part of the permgen memory, so I'd like to know if it's best to use closure-like functions as inner classes, or if standard classes would also do fine? 回答1: Inner classes and anonymous inner classes both compile down to .class files. For example: class Outer { class Inner { } Object function(

How many times can classes be nested within a class?

那年仲夏 提交于 2019-11-29 13:18:51
问题 I came across this questions on one of the online Java Tests. The options were 4,5,8 and any number of times. I have only used one inner class, but have never tried multiple ones. I was wondering if anyone knows the answer. 回答1: It's a completely irrelevant question and I hope they weren't using the results for anything important. I guess the answer they were looking for was 'any number of times' but in practice there will be a limit in any given implementation of Java. If it's not defined

Can't access protected inner class while inheriting

*爱你&永不变心* 提交于 2019-11-29 12:11:02
问题 Reading through "Thinking in Java" i stuck in ex:6 of Inner Classes chapter. Exercise 6: (2) Create an interface with at least one method, in its own package. Create a class in a separate package. Add a protected inner class that implements the interface. In a third package, inherit from your class and, inside a method, return an object of the protected inner class, upcasting to the interface during the return. This is my code: IOne.java interface package intfpack; public interface IOne{ void

Why compile time constants are allowed to be made static in non static inner classes?

帅比萌擦擦* 提交于 2019-11-29 11:34:33
Suppose we have code as below. public class Outer{ class Inner{ public static final String s = "abc"; } static class Nested{ public static final SomeOtherClass instance = new SomeOtherClass(); } } I understand to instantiate object of non static inner classes an object of Outer class is needed . static means class related and for accessing it an object is not required to be instantiated. Non static inner class can only be used once we have an object of Outer class instantiated. Having any static references in it may not make sense. My Questions: Can non static inner class get loaded without

How to make an outer class inherited from an inner class?

自古美人都是妖i 提交于 2019-11-29 10:04:09
How can I make something like this work: class Outer { int some_member; abstract class InnerBase { abstract void method(); } } class OuterExtendsInner extends Outer.InnerBase { OuterExtendsInner(Outer o) { o.super(); } void method() { // How do I use some_member here? // Writing Outer.this.some_member -> error about Outer not being an enclosing class // Writing just some_member -> no-go, either } } The workaround is to have a method in InnerBase that returns Outer.this and call THAT from derived classes, but is there another way? I primarily want to extend the InnerBase from outside in order

Practical side of the ability to define a class within an interface in Java?

随声附和 提交于 2019-11-29 07:36:07
问题 What would be the practical side of the ability to define a class within an interface in Java: interface IFoo { class Bar { void foobar () { System.out.println("foobaring..."); } } } 回答1: I can think of another usage than those linked by Eric P: defining a default/no-op implementation of the interface. ./alex interface IEmployee { void workHard (); void procrastinate (); class DefaultEmployee implements IEmployee { void workHard () { procrastinate(); }; void procrastinate () {}; } } Yet

Scala can't access Java inner class?

女生的网名这么多〃 提交于 2019-11-29 07:27:37
问题 I have a java class that looks like this: public class Constants { public class Commands { public static final String CreateOrder = "CreateOrder"; } } I want to access "CreateOrder" constant, In java I can access it easily like this: String co = Constants.Commands.CreateOrder But in Scala this doesn't work why??? How can I access "CreateOrder" from Scala, I can't alter the Java code. Thanks. 回答1: As far as I know, there is no way to do what you want in Scala. But if you absolutely cannot

How to refer to enclosing instance from C++ inner class?

谁说我不能喝 提交于 2019-11-29 06:42:49
In C++, an object refers to itself via this . But how does an instance of an inner class refer to the instance of its enclosing class? class Zoo { class Bear { void runAway() { EscapeService::helpEscapeFrom ( this, /* the Bear */ ??? /* I need a pointer to the Bear's Zoo here */); } }; }; EDIT My understanding of how non-static inner classes work is that Bear can access the members of its Zoo , therefore it has an implicit pointer to Zoo . I don't want to access the members in this case; I'm trying to get that implicit pointer. BЈовић Unlike Java, inner classes in C++ do not have an implicit