[英]How do you instantiate an inner class of a static nested class?
This is my class 这是我的课
public class XYZ{
public static void main(String[] args) {
int n = 5;
Object myObject;
//somehow call returnN(n);
} //end main
static class Inner{
private class Private{
private int returnN(int n){
return n;
}
}
}//end of Inner
}
I am trying to call returnN from main. 我正在尝试从main调用returnN。 I've tried
myObject = new XYZ.Inner.Private();
我已经尝试过
myObject = new XYZ.Inner.Private();
but this does not appear to be the correct instantiation. 但这似乎不是正确的实例。
Private
is not static
. Private
不是static
。 Therefore you need an instance of Inner
before you can create a Private
. 因此,在创建
Private
之前,需要Inner
的实例。 The correct syntax is 正确的语法是
new XYZ.Inner().new Private().returnN(3);
Alternatively, you can make Private
static
and then you can just do 另外,您可以将
Private
static
,然后就可以
new XYZ.Inner.Private().returnN(3);
Sean: why did you delete your recent question after Alain put in effort to answer it? 肖恩(Sean):在阿兰(Alain)努力回答后,您为什么删除最近的问题 ? I will post it here so that it at least has an internet presence, but you should re-open it, and up-vote Alain's answer, at least for the effort he put into answering it.
我将其张贴在这里,以便至少在Internet上存在,但您应该重新打开它,并投票赞成Alain的回答,至少是出于他在回答问题上的努力。
I am supposed to implement a class, when I have lines like this: 当我有这样的代码行时,我应该实现一个类:
double v = className.functionName().main(a,b);
And 和
className.output.display(v);
It is not clear to me what this hierarchy is supposed to look like in the className class. 我不清楚在className类中该层次结构应该是什么样子。
What does it mean to call main() from a function call? 从函数调用中调用main()是什么意思? Calling a main function from a function?
从函数调用主函数? Why possibly isnt this just functionName(a,b)?
为什么可能这不是functionName(a,b)?
And the point of using .output.? 和使用.output的重点。 Is this supposed to be a void with a sub-function display()?
子函数display()是否应该将其无效?
Answer by Alain O'Dea Alain O'Dea的回答
output is a field. 输出是一个字段。 There is no such thing as a sub-function or method, there are just methods on objects.
没有子功能或方法,只有对象上的方法。 The display() method is on the object field of the classname object.
display()方法位于类名对象的对象字段上。
The main() method here is just a method on the object returned by functionName() . 这里的main()方法只是对functionName()返回的对象的方法。 There is no special meaning from being called main() .
被称为main()并没有特殊含义。
I would infer something like this from the lines given: 我将从给定的行中推断出这样的内容:
I'm going to assume that className is an instance of ClassName used like this: 我将假设className是这样使用的ClassName的实例:
public class Main {
public static void main(String[] args) {
ClassName className = new ClassName();
Object a = new Object(); // replace with correct a
Object b = new Object(); // replace with correct b
double v = className.functionName().main(a,b);
className.output.display(v);
}
}
And ClassName would be defined as follows: 而ClassName的定义如下:
public class ClassName {
public Output output = new Output;
public Mainable functionName() {
return new Mainable();
}
}
Output.java: Output.java:
public class Output {
public void display(double v) {
// implement display here
}
}
Mainable.java: Mainable.java:
public class Mainable {
public double main(Object a, Object b) {
return 0.0d; // put actual impl here
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.