inner-classes

Java inner class with the same name as other top level class

百般思念 提交于 2019-12-06 02:12:21
问题 I have question related to Java inner classes. Is there a way to access top level class A from top level class Main that define inner class A? Below is sample code demonstrating the problem: class A { // Outer Class A { System.out.println("A outer"); } } class B { // Outer Class B { System.out.println("B outer"); } } public class Main { class A { // Inner Class A { System.out.println("A inner"); } } public void newA() { class A { // Local Class A { System.out.println("A local"); } } new A();

Java: Issue combining Generics, Inner class, and “implements”

时光毁灭记忆、已成空白 提交于 2019-12-06 00:02:21
I am having an issue combining generics, implements , and inner classes. I am creating a LinkedBinaryHeap class which contains an inner class. This inner class is the generic HeapNode which extends the generic Node class I created; it just adds a variable and methods for a key/priority. In LinkedBinaryHeap I create a generic LinkedList to store HeapNode s. I am assuming the generic data being stored extends Comparable class. Here is a layout of what stores what: BinaryHeap->LinkedList(Nodes)->HeapNode(extends Node)->DATA,KEY My issue is that when declaring the LinkedList : LinkedList<HeapNode>

Defining a class within another class and calling a parent method

邮差的信 提交于 2019-12-05 20:50:00
I have c class definition and some inner class definitions for that class: class DoXJob(): def __init__(self, param1, param2): <choose an inner class to use> def DoIt(self): pass def Finalize(self): <do finalization> class DoXInSomeWay(): def __init__(self): .... .... def DoIt(self): ... class DoXInSomeOtherWay(): def __init__(self): .... .... def DoIt(self): ... Logic is simple, i have some inner class definitions to handle a job, and i call DoXJob with some parameters, it then decides which inner class will be used, and override DoXJob.DoIt method with the method of selected inner class

Inner Class in Java

蓝咒 提交于 2019-12-05 20:29:36
问题 I was reading about Inner class in Learning Java. I found this code class Animal{ class Brain{ } } After compiling, javap 'Animal$Brain' gives output as Compiled from "Animal.java"class Animal$Brain { final Animal this$0; Animal$Brain(Animal); } which explains how the inner class gets the reference to its enclosing instance in the inner class constructor. But when I define the inner class as private like this class Animal{ private class Brain{ } } then after compiling, javap 'Animal$Brain'

Error when defining inner classes in a Test class in JUnit

霸气de小男生 提交于 2019-12-05 17:48:30
问题 I'm having some problems when defining inner classes in a Test class inherited from TestCase, for JUnit 3. Scenario is like following: Foo.java public class Foo { public void method() { ... } } FooTest.java public class FooTest extends TestCase { public class Bar extends Foo { public void method() { ... } } public void testMethod() { ... } } Now, if I run this from Eclipse, the tests run ok, but if I try to run from an Ant task it fails: [junit] junit.framework.AssertionFailedError: Class Foo

Nested class definition outside outer class's, while outer class contains instance of inner class

喜夏-厌秋 提交于 2019-12-05 16:30:29
C++ How can I put the definition of an inner (nested) class outside its outer (enclosing) class's definition, where the outer class has at least one instance of the inner class as a data member? I searched but the most relevant SO answer I found, Nested Class Definition in source file , does not have an example where the outer class has an inner object as a data member. I followed that answer, as far as declaring but not defining the inner class inside the outer class's definition is concerned, but my code is still broken: struct Outer { struct Inner; Inner myinner; Outer() : myinner(2) {} };

How does finish() work in OnClick event?

杀马特。学长 韩版系。学妹 提交于 2019-12-05 15:13:38
I have an Activity with one button to quit the Activity . Through the layout.xml I have to set the OnClick event to cmd_exit and a call to 'this.finish()' works fine public void cmd_exit(View editLayout){ this.finish(); } , but when I add a OnClickListener instead cmd_exit = (Button) this.findViewById(R.id.cmd_ExitApp); cmd_exit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { finish(); } }); this.finish() gives error. It has to be only finish() . I understand that finish() lives in the Activity class so my question is how is it working in the second

“illegal generic type of instanceof” when using instanceof on an inner class type?

a 夏天 提交于 2019-12-05 13:50:01
I coded in NetBeans something like this: public class Grafo<V, E> { class Par { int a, b; Par(int a, int b) { this.a = a; this.b = b; } @Override public boolean equals(Object ob) { if(ob instanceof Par) { Par p = (Par)ob; return this.a==p.a && this.b==p.b; } return false; } } //stuff... } //end of class Grafo The error is in the method equals() from inner class "Par". NetBeans says that the error is "illegal generic type of instanceof". The error is in the line below. if(ob instanceof Par) { What is the cause of the error ? Try ob instanceof Grafo<?,?>.Par I think that the compiler thinks that

When extending a trait within a trait, what does 'super' refer to?

痞子三分冷 提交于 2019-12-05 11:39:36
问题 I want to to extend a trait within a trait, like this: trait NodeTypes { trait Node { def allNodesHaveThis: Int } } trait ScrumptiousTypes extends NodeTypes { trait Node extends super.Node { def scrumptiousness: Int } } trait YummyTypes extends NodeTypes { trait Node extends super.Node { def yumminess: Int } } object Graph extends NodeTypes with ScrumptiousTypes with YummyTypes { case class Node() extends super.Node { override def allNodesHaveThis = 1 override def scrumptiousness = 2 // error

java nested interfaces and inner classes

笑着哭i 提交于 2019-12-05 10:57:10
Why can't a java nested Interface be non-static ? And why can't an inner class contain static non final members ? I came across the questions while going through Gosling and haven't been able to figure out the answer yet. If an nested class is non-static (i.e. an inner class), this means that each instance of it is bound to an instance of the outer class. As an interface has no instances of its own, it seems to not be useful for the implementing classes to be bound to an outer object, so having it static by default seems reasonable. I'm not sure why you can't have static non final members in