您的位置:首页 > 编程语言 > Java开发

JAVA内部类学习笔记01

2010-09-04 18:54 369 查看

JAVA内部类小结

定义:

一个类定义置入另一个类定义中。这就叫作“内部类”。内部类分为成员内部类和局部内部类。其中成员内部类包括实例内部类与静态内部类。局部内部类分为有名内部类与匿名内部类。



语法:

实例内部类

1. 类可以声明为public,private,protected,默认,abstract,final。
2. 在行为上类似类的属性和方法且禁止声明static的方法与属性。
3. 内部类拥有指向封装类的一个链接,所以可以访问封装类对象的成员。除此以外,内部类拥有封装类所有元素的访问权。
4. 外部类按常规的类访问方式使用内部类,唯一的差别是外部类可以访问内部类的所有方法与属性。包括私有方法与属性。
5. 在类外或本类的static方法中创建一个内部类,必须拥有外部类的一个实例。因为内部类的对象已同创建它的外部类默默的连接在一起了(内部类拥有一个指向外部对象的链接)。
6. 例子:
package inner;
public class Parcel1 {
private String label = "Outer";
class Destination {
private String label;
Destination(String whereTo) {
label = whereTo;
}
private void readLabel() {
// 访问外部类的私有属性.
System.out.println("Outer Lable = " + Parcel1.this.label);
System.out.println("Inner Lable = " + this.label);
}
}
public void ship(String dest) {
Destination d = new Destination(dest);
// 访问内部类的私有属性.
System.out.println("private attribute label = " + d.label);
}
public static void main(String[] args) {
Parcel1 p = new Parcel1();
p.ship("123");
Parcel1.Destination d = p.new Destination("Inner");
// 访问内部类的私有方法.
d.readLabel();
}
}

静态内部类

1. 类可以声明为public,private,protected, 默认,abstract,final。
2. 静态内部类不依赖于外部类的实例,所以静态内部类不能访问外部类的非静态的属性与方法。
3. 外部类可以访问内部类的所有方法与属性。
4. 静态类可以声明static的方法与属性(static的方法与属性只能声明在外部类与静态内部类中)。
5. 例子:
package inner.stati;
public class Parcel10 {
private static String outer = "Outer";
private static class PContents {
private static int i = 11;         //static的属性.
/*只能访问外部类的静态属性与方法.*/
public String printOut() { return " outer=" + outer;};
public static int value(){ return i; }   //static的方法.
}
public static void main(String[] args) {
Parcel10.PContents pp = new Parcel10.PContents(); //静态内部类独立于外部类的实例.
System.out.println("i="+ pp.i);                    //访问静态内部类的私有属性.
System.out.println(pp.printOut());
}
}


局部内部类

1. 局部内部类是定义在方法内部的类,只可以在作用域内使用。
2. 类只能声明为final和abstract。
3. 例子
有名内部类:
package inner.complex1;
public class Parcel4 {
public Destination dest(String s) {
class PDestination implements Destination {
private String label;
private PDestination(String whereTo) {label = whereTo;}
public String readLabel() {return label;}
}
/**
* PDestination类属于dest()的一部分,而不是Parcel4的一部分。因此,PDestination
* 不可从dest()的外部访问。
* 注意:不能由于类PDestination的名字置于dest()内部,就认为在dest()返回之后
* PDestination不是一个有效的对象。
*/
return new PDestination(s);
}
private void internalTracking(boolean b) {
if(b) {
/**
* TrackingSlip类嵌套于一个if语句的作用域内。这并不意味着类是有条件创建的,它
*会随同其他所有东西得到编译。然而,在定义它的那个作用域之外,它是不可使用的。
*除这些以外,它看起来和一个普通类并没有什么区别。
*/
class TrackingSlip {
private  String id;
TrackingSlip(String s) { id = s; }
public  String getSlip() { return id; }
}
TrackingSlip ts = new TrackingSlip("slip");
System.out.println("s="+ts.getSlip());
}
}
public static void main(String[] args) {
Parcel4 p = new Parcel4();
Destination d = p.dest("Tanzania");
System.out.println(d.readLabel());
p.internalTracking(true);
}
}

匿名内部类:
package inner.complex1;
interface Content {
int value();
}
public class Parcel6 {
public Content cont() {
/**
* “创建从Content 衍生出来的匿名类的一个对象”。
* 由new表达式返回的句柄会自动上溯造型成一个Content 句柄。
*/
return new Content() {
private int i = 11;
public int value() {
System.out.println("value i = " + i);
return i;
}
}; // Semicolon required in this case
}

public static void main(String[] args) {
Parcel6 p = new Parcel6();
Content c = p.cont();
c.value();
}
} // /:~


从内部类继承

package inner.extend;
//: InheritInner.java
//Inheriting an inner class
class WithInner {
class Inner {}
}

public class InheritInner extends WithInner.Inner {
//! InheritInner() {} // Won't compile
InheritInner(WithInner wi) {
wi.super();
}
public static void main(String[] args) {
WithInner wi = new WithInner();
InheritInner ii = new InheritInner(wi);
}
} ///:~

内部类构建器必须同封装类对象的一个句柄联系到一起

2010-09-04
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: