【发布时间】:2012-04-21 14:10:59
【问题描述】:
在阅读文档时,我读到了一些 ViewHolder 是静态类的地方,但它需要在静态类上创建新的吗? 在那个例子中,他们做了新的吗?但根据概念,新的不应该在静态类上完成吗?
【问题讨论】:
-
类上的静态修饰符与变量或方法上的静态修饰符是不同的概念。techrepublic.com/article/…
-
@Blundell 谢谢,正是我要找的东西
在阅读文档时,我读到了一些 ViewHolder 是静态类的地方,但它需要在静态类上创建新的吗? 在那个例子中,他们做了新的吗?但根据概念,新的不应该在静态类上完成吗?
【问题讨论】:
你只能用四种方式构造类:
1 和 3 是迄今为止最常用的
【讨论】:
将类称为静态,实际上并不是最好的表达方式,因为它通常意味着类不必被实例化。但这也可能取决于上下文,这需要首先讨论,因为您的问题没有给出。
【讨论】:
创建嵌套类实例的语义可以是 令人困惑。下面是一个简单的类,它定义了一个静态嵌套类 和一个内部类。特别注意 main 方法,其中一个 创建每个实例类的实例。
// creating an instance of the enclosing class NestedClassTip nt = new NestedClassTip(); // creating an instance of the inner class requires // a reference to an instance of the enclosing class NestedClassTip.NestedOne nco = nt.new NestedOne(); // creating an instance of the static nested class // does not require an instance of the enclosing class NestedClassTip.NestedTwo nct = new NestedClassTip.NestedTwo(); public class NestedClassTip { private String name = "instance name"; private static String staticName = "static name"; public static void main(String args[]) { NestedClassTip nt = new NestedClassTip(); NestedClassTip.NestedOne nco = nt.new NestedOne(); NestedClassTip.NestedTwo nct = new NestedClassTip.NestedTwo(); } class NestedOne { NestedOne() { System.out.println(name); System.out.println(staticName); } } static class NestedTwo { NestedTwo() { System.out.println(staticName); } } }嵌套类可能会令人困惑,但一旦您了解它们的用途 并习惯语义,它们并不多。如果你愿意 想了解更多关于嵌套类的详细信息,请查看 Java 语言规范。
【讨论】: