嵌套类

如果你不不需要内部类与外部类之间的联系,可以将内部类生命为static的,这就是嵌套类。
注意:
1.要创建嵌套类的对象,并不需要外部类的对象。
2.不能从嵌套类对象中访问非静态的外部类对象。
嵌套类与普通类的区别:
1.普通类不能包含static数据和static属性,也不能包含嵌套类
2.

public class Parcel10 {
private static class ParcelContents implements Contents {
private int i = 11;
public int value() {
return i;
}
}
protected static class ParcelDestination implements Destination {
private String label;
private ParcelDestination(String str) {
this.label = str;
}
public String readLabel() {
return label;
}
public static void f() { }
static int x = 10;
static class AnotherLevel {
public static void f () {
}
static int x = 10;
}
}
public static Contents count() {
return new ParcelContents();
}
public static Destination dest(String s) {
return new ParcelDestination(s);
}
public static void main(String[] args) {
Contents c = count();
Destination d = dest("hello");

}
}

上代码:创建内部类对象时候不需要创建外部类对象。
1.嵌套类可以做为接口的一部分,并不会报错
interface A {
static class Inner {
int i;
public Inner() {}
void f(){}
}
}

interface A {
class Inner {
int i;
public Inner() {}
void f(){}
}
}

当然这样也不报错。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值