日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java静态类和非静态类_关于java:静态和非静态内部类的区别?

發(fā)布時(shí)間:2024/10/14 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java静态类和非静态类_关于java:静态和非静态内部类的区别? 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我正在閱讀有效的Java 2 -項(xiàng)目22,它在標(biāo)題中寫道:

"Favor static member classes over non-static"

但是在這一章的結(jié)尾

Implementations of the collection interfaces, such as Set and List,

typically use nonstatic member classes to implement their iterators:

// Typical use of a nonstatic member class

public class MySet extends AbstractSet {

... // Bulk of the class omitted

public Iterator iterator() {

return new MyIterator();

}

private class MyIterator implements Iterator {

...

}

}

我做了一個(gè)測(cè)試程序,看看它們之間是否有任何區(qū)別,這里就是。

public class JavaApplication7 {

public static void main(String[] args) {

// TODO code application logic here

JavaApplication7 t = new JavaApplication7();

Inner nonStaticObject = t.getAClass();

Sinner staticObject = new JavaApplication7.Sinner();

nonStaticObject.testIt();

staticObject.testIt();

}

public Inner getAClass(){

return new Inner();

}

static class Sinner{

public void testIt(){

System.out.println("I am inner");

}

}

class Inner{

public void testIt(){

System.out.println("I am inner");

}

}

}

輸出是

I am inner

I am inner

所以,他們做了同樣的工作。

我想知道為什么在這個(gè)例子中使用非靜態(tài)類?

迭代器通常需要首先引用用于創(chuàng)建它的集合。可以使用一個(gè)顯式提供了對(duì)集合的引用的靜態(tài)嵌套類來實(shí)現(xiàn)這一點(diǎn),也可以只使用一個(gè)隱式引用的內(nèi)部類。

基本上,如果嵌套類的每個(gè)實(shí)例都需要一個(gè)封閉類的實(shí)例來操作(而該實(shí)例不變),那么您也可以將其設(shè)置為一個(gè)內(nèi)部類。否則,將其設(shè)置為靜態(tài)嵌套類。

區(qū)別在于,非靜態(tài)內(nèi)部類對(duì)包含類具有隱式引用。

public class JavaApplication7 {

//You can access this attribute in non-static inner class

private String anyAttribute;

public Inner getAClass(){

return new Inner();

}

static class Sinner{

public void testIt(){

//Here, you cannot access JavaApplication7.this

}

}

class Inner{

public void testIt(){

//Here, you can access JavaApplication7.this

//You can also access *anyAttribute* or call non-static method getAClass()

}

}

}

static與非靜態(tài)嵌套類的區(qū)別在于,非靜態(tài)嵌套類隱式地與外部類的一個(gè)實(shí)例相關(guān)聯(lián),它們可以稱為OuterClassName.this。此引用在實(shí)現(xiàn)迭代器時(shí)很有用,因?yàn)樗鼈冃枰L問與其相關(guān)的集合的成員。您可以通過使用static嵌套類來實(shí)現(xiàn)相同的事情,該類顯式地將引用傳遞給外部類。

In the case of creating instance, the instance of non s

static inner class is created with the reference of

object of outer class in which it is defined……this

means it have inclosing instance …….

But the instance of static inner class

is created with the reference of Outer class, not with

the reference of object of outer class…..this means it

have not inclosing instance…

For example……

class A

{

class B

{

// static int x; not allowed here…..

}

static class C

{

static int x; // allowed here

}

}

class Test

{

public static void main(String… str)

{

A o=new A();

A.B obj1 =o.new B();//need of inclosing instance

A.C obj2 =new A.C();

// not need of reference of object of outer class….

}

}

沒有靜態(tài)內(nèi)部類,它是靜態(tài)嵌套類。"非靜態(tài)[嵌套]類的每個(gè)實(shí)例都隱式地與其包含類的封閉實(shí)例關(guān)聯(lián)…可以在封閉實(shí)例上調(diào)用方法。"

靜態(tài)嵌套類無(wú)權(quán)訪問封閉實(shí)例。

參考文獻(xiàn):這條索線

總結(jié)

以上是生活随笔為你收集整理的java静态类和非静态类_关于java:静态和非静态内部类的区别?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。