黑馬程序員――面向對象--內部類
來源:程序員人生 發布時間:2014-12-14 09:00:11 閱讀次數:6460次
------Java培訓、Android培訓、iOS培訓、.Net培訓、期待與您交換! -------
內部類:
內部類的訪問規則:
1.內部類可以直接訪問外部類中的成員,包括私有
之所以可以直接訪問外部類中的成員,是由于內部類中持有了1個外部類的援用,格式: 外部類名.this
2.外部類要訪問內部類,必須建立內部類對象。
class outer{
int num = 5;
class inner{
System.out.println("num="+num);//可以直接調用外部成員
}
void method(){
inner in = new inner();//創建內部類的對象
show();//調用內部類的方法
}
}
訪問格式:
1.當內部類定義在外部類的成員位置上,而且非私有,可以在外部其他類中
可以直接建立內部類對象
格式:
外部類名.內部類名 變量名 = 外部類對象.內部類對象;
outer.Inner in = new outer().new Inner();
2.當內部類在成員位置上,就能夠被成員修飾符所修飾
比如:private:將內部類在外部類中進行封裝
static:內部類就具有static的特性
當內部類被static修飾后,只能直接訪問外部類中的static成員,出現了訪問局限
在外部其他類中,如何直接訪問static內部類的非靜態成員呢?
new outer.Inner().function();
在外部其他類中,如何直接訪問static內部類的靜態成員呢?
outer.Inner.function();
注意:當內部類中定義了靜態成員,該內部類必須是static的
當外部類中的靜態方法訪問內部類時,內部類也必須是static的
當描寫事物時,事物的內部還有事物,該事物類用內部類來描寫。
由于內部事務在使用外部事物的內容
class Body{
private class zhangcheng//內部類
{
}
public void show()
{
new zhangcheng();//必須建立對象
}
}
class Outer
{
private static int x = 3;
static class Inner
{
static void function(){
System.out.println("inner:"+x);
}
}
static class Inner2{
void show(){
System.out.println("inner2 show");
}
}
public static void method(){
new Inner2().show();
}
}
class InnerClassDemo2
{
public static void main(String args[]){
Outer.Inner.function();
new Outer.inner().function();//直接訪問內部類中的成員
Outer.Inner in = new Outer().new Inner();//創建內部類對象
in.function();//調用內部類的方法
}
}
內部類在定義時:
1.不可以被成員修飾符修飾
2.可以直接訪問外部類中的成員,由于還持有外部類中的援用
但是不可以訪問他所在的局部中的變量,只能訪問被final修飾的局部變量
class Outer
{
int x = 3;
void method(final int a){
final int y = 4;
class Inner{
void function(){
System.out.println(y);
}
}
new Inner().function();
}
class InnerClassDemo{
public static void main(String args[]){
Outer out = new Outer();
out.method(7);
out.method(8);
}
}
匿名內部類:
1.匿名內部類其實就是內部類的簡寫格式
2.匿名內部類的條件:
內部類必須是繼承1個類或是實現接口
3.匿名內部類的格式:
new 父類或接口(){定義子類的內容}
4.其實匿名內部類就是1個匿名子類對象,而且這個對象有點胖,可以理解為帶內容的對象
5.匿名內部類中定義的方法最好不要超過3個。
abstract class AbsDemo{
abstract void show();
}
class outer{
int x = 3;
class Inner extends AbsDemo{
int num = 90;
void show(){
System.out.println('show:"+num);
}
void abc(){
System.out.println("abc");
}
}
public void function(){
AbsDemo d = new AbsDemo(){
int num = 9;
void show(){
System.out.println("num="+num);
}
void abc(){
System.out.println("abcccc");
}
};//注意這個分號
d.show();//ok,實現了接口
d.abc();//error,自定義方法,沒有實現接口或是繼承1個類,抽象類不能繼承,只能實現
}
}
class InnerclassDemo{
public static void main(String args[]){
new Outer().function();
}
}
interface Inter{
void method();
}
class test{
static class Inner implements Inter
{
public void method()
{
System.out.println("method run");
}
}
static Inter function(){
return new Inter(){
public void method(){
System.out.println("method run");
}
}; }
}
class Innerclasstest
{
public static void main(String args[]){
//test.function():test類中有1個靜態方法function
//method():function這個方法運算后的解惑是1個對象,而且是1個Inner類型的對象
//由于只有是Inter類型的對象,才可以調用method方法
test.function().method();
inter in = test.function();
in.method();
show(new Inter()
{
public void method()
{
System.out.println("method show run");
}
});
}
public static void show(Inter in)
{
in.method();
}
}
class Innertest
{
public static void main(String args[]){
new object()
{
public static function()
{
}
}.function();
}
}
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈