Android & Java 内部类概念
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17299100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android & Java inner class concept
提问by User2364902
i followed the link http://developer.android.com/reference/android/app/AlertDialog.htmland i try to create new AlertDialog like this
我按照链接http://developer.android.com/reference/android/app/AlertDialog.html我尝试像这样创建新的 AlertDialog
AlertDialog myAlertDialog = new AlertDialog.Builder(MainActivity.this).create();
as per the document AlerDialog is the outerclass and Builder is the inner class within AlertDialog. Now i linked the same concept with java in accessing the inner class like this Outer myOuter2 = new Outer.Inner();
this piece of gives error when i try to access, here is the complete java code
根据文档,AlerDialog 是外部类,Builder 是 AlertDialog 中的内部类。现在我在访问内部类时将相同的概念与 java 联系起来,Outer myOuter2 = new Outer.Inner();
当我尝试访问时,这块给出了错误,这是完整的 java 代码
package com.test;
public class Outer {
public void OuterMethod() {
System.out.println("OuterMethod");
}
public static void main(String[] args) {
Outer myOuter = new Outer();
myOuter.OuterMethod();
Outer myOuter2 = new Outer.Inner();//this piece of code gives error
}
class Inner {
Inner() {
System.out.println("constructor Inner");
}
public void InnerMethod() {
System.out.println("Inside InnerMethod");
}
}
}
so my question over here is how to understand the same inner class concept in android and accessing the methods within that
所以我在这里的问题是如何理解 android 中相同的内部类概念并访问其中的方法
回答by Motti Strom
You have created an inner non-static class (an inner instanceclass), whereas AlertDialog.Builder
is a static class.
您创建了一个内部非静态类(内部实例类),而AlertDialog.Builder
是一个静态类。
To get your code to work as is you need an interesting way of invoking new
that goes like this:
为了让您的代码按原样工作,您需要一种有趣的调用方式,new
如下所示:
Outer.Inner myOuter2 = myOuter.new Inner();
This is because it acts much like any other non-static field within Outer - it requires an instance of Outer in order to be valid. In any event, this is often not a good idea as publicinner non-static classes are rare.
这是因为它的行为与 Outer 中的任何其他非静态字段非常相似——它需要一个 Outer 的实例才能有效。无论如何,这通常不是一个好主意,因为公共内部非静态类很少见。
More likely you want Inner
to be a static class, i.e. one declared as:
您更有可能想要Inner
成为一个静态类,即声明为:
static class Inner {
Essentially this decouples Inner
from its containing class, it just happens to live inside it and so can be instantiated via new Outer.Inner()
. It could happily live as a public class in its own right in a new .java file instead.
本质上,这Inner
与其包含的类分离,它恰好位于其中,因此可以通过new Outer.Inner()
. 它可以愉快地作为一个公共类独立存在于一个新的 .java 文件中。
Inner static classes are useful when the inner class is only used in relation the outer class, so it shows the relationship between them.
当内部类仅用于外部类的关系时,内部静态类很有用,因此它显示了它们之间的关系。
In Android's case you use an AlertDialog.Builder
only when building an AlertDialog
. If it was a general Builder
used by other classes (e.g. a plain Dialog
) is would have instead been declared as its own public class (i.e. a standalone class that is not nested inside another).
在 Android 的情况下,您AlertDialog.Builder
仅在构建AlertDialog
. 如果它是Builder
其他类(例如普通类Dialog
)使用的通用类,则它会被声明为自己的公共类(即未嵌套在另一个类中的独立类)。
回答by Duncan Jones
There is no relationship between Outer
and Inner
except that they share a class file. Hence, you cannot type:
除了它们共享一个类文件之外,Outer
和之间没有任何关系Inner
。因此,您不能键入:
Outer myOuter2 = new Outer.Inner();
Perhaps you meant:
也许你的意思是:
Outer.Inner myInner = new Outer.Inner();
The Inner
class will need to be declared as static
for this to work.
该Inner
班将需要被宣布为static
这个工作。
Note that a normal builder will return a type that is equal to the enclosing type. Here's a small example using similar class names to your code:
请注意,普通构建器将返回与封闭类型相同的类型。这是一个使用与您的代码相似的类名的小示例:
public class Outer {
public static void main(String[] args) {
Outer outer = new Outer.Builder().withParam("foo").build();
}
private final String someParam;
private Outer(String someParam) {
this.someParam = someParam;
}
public static class Builder {
private String someParam;
public Builder() {
}
public Builder withParam(String value) {
this.someParam = value;
return this;
}
public Outer build() {
return new Outer(someParam);
}
}
}
You may also wish to read Item #2 of Joshua Bloch's Effective Java, 2nd Editionfor a good description of builder design and rationale. Available online: here.
您可能还希望阅读 Joshua Bloch 的Effective Java,第 2 版的第 2 项,以获得对构建器设计和基本原理的良好描述。在线提供:在这里。
回答by WAKE UP Ravindra
Your inner class is non static type. We should first create instance of your outer class:
您的内部类是非静态类型。我们应该首先创建外部类的实例:
Outer o=new Outer();
Outer.Inner oi=o.new Inner();
This is the basic way of create non static inner class object.
这是创建非静态内部类对象的基本方式。
Suppose if your inner is of type static (i.e. static class Inner{....}
),
then for creating object:
假设你的内部是静态类型(即static class Inner{....}
),然后创建对象:
Outer.Inner oi=new Outer.inner();
回答by Ahmad
回答by User2364902
Finally i figured out here is the code
最后我发现这里是代码
package com.test;
public class Outer {
public void OuterMethod() {
System.out.println("OuterMethod");
}
public static void main(String[] args) {
Outer myOuter = new Outer();
myOuter.OuterMethod();
Outer myOuter2 = new Outer.Inner().InnerMethod();
}
static class Inner {
Inner() {
System.out.println("constructor Inner");
}
public Outer InnerMethod() {
Outer myOuter = new Outer();
System.out.println("Inside InnerMethod");
return myOuter;
}
}
}