现在是够懒得了,放假的时候就想把这篇笔记写出来,一直拖到现在,最近在读《Java编程思想》,我想会做不止这一篇笔记,因为之前面试的时候总会问道一些内部类的问题,那这本书的笔记就从内部类开始说起。
一.为什么需要内部类
1.一个内部类的对象可以访问它的外围类的私有数据。
2.对于同一个包中的其他类来说,内部类能够隐藏起来。
3.使用内部类实现多重继承。
二.内部类重要提要
1.在方法内部或者方法内部的作用域的内部类
eg:方法内部的内部类
public interface InnerInterface {
String getStr1(String xxx);
}
public class Outer {
private InnerInterface getStr() {
class Inner implements InnerInterface {
String sss = ""; @Override
public String getStr1(String xxx) {
sss = xxx;
return sss;
}
} Inner inner = new Inner();
return inner;
} public static void main(String[] args) {
InnerInterface innerInterface = new Outer().getStr();
System.out.println(innerInterface.getStr1("1234"));
}
}
eg方法作用域的内部类
public interface Inface {
void sysout();
}public class Outer {
Inface getInface(boolean flag) {
if (flag) {
class Inner implements Inface{
@Override
public void sysout() {
System.out.println("876");
}
}
Inner inner = new Inner();
return inner;
} else {
class Inner implements Inface {
@Override
public void sysout() {
System.out.println("123");
}
}
Inner inner = new Inner();
return inner;
}
} public static void main(String[] args) {
Outer outer = new Outer();
Inface inface = outer.getInface(true);
inface.sysout(); inface = outer.getInface(false);
inface.sysout();
}
}2.匿名内部类。
匿名内部类只能使用一次,主要用来简化代码
但使用匿名内部类还有个前提条件:内部类必须继承一个父类或实现一个接口
eg:实现接口的demo
public class Outer {
private String str = "123"; public interface InnerCalss{
String value(String xxx);
} public InnerCalss getInnerCalss() {
return new InnerCalss() {
private String i;
public String value(String xxx){
i = xxx;
return i;
}
};
} public static void main(String[] args) {
Outer outer = new Outer();
System.out.println(outer.getInnerCalss().value("12345"));
}
}
eg:继承父类的的demo
public class Outer {
public abstract class InnerInterface {
abstract String getStr1(String xxx);
}
private InnerInterface getStr() {
return new InnerInterface() {
private String sss = "";
@Override
public String getStr1(String xxx) {
sss = xxx;
return sss;
}
};
} public static void main(String[] args) {
InnerInterface innerInterface = new Outer().getStr();
System.out.println(innerInterface.getStr1("1234"));
}
}
看着这个实现匿名内部类是不是觉得不是很常用下面的代码或许就知道原来自己一直在写匿名内部类,只是未察觉而已
public class ThreadInnerClass {
public static void main(String[] args) {
Thread t = new Thread() {
public void run() {
System.out.println("1234567");
}
};
t.start();
}
}
public class RunnableInnerClass {
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
System.out.print("1234567");
}
};
Thread t = new Thread(r);
t.start();
}
}
另外匿名内部类参数必须为final类型,这里借用书里的demo
public class Parcel10 {
public Destination
destination(final String dest, final float price) {
return new Destination() {
private int cost;
// Instance initialization for each object:
{
cost = Math.round(price);
if(cost > 100)
System.out.println("Over budget!");
}
private String label = dest;
public String readLabel() { return label; }
};
}
public static void main(String[] args) {
Parcel10 p = new Parcel10();
Destination d = p.destination("Tasmania", 101.395F);
}
} /* Output:
Over budget!
*///:~
3.嵌套类
嵌套类主要是两种
(1)嵌套类将内部类用static修饰或者是接口内部类
eg:这里引用书中的demo
public class Parcel11 {
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 whereTo) {
label = whereTo;
}
public String readLabel() { return label; }
// Nested classes can contain other static elements:
public static void f() {}
static int x = 10;
static class AnotherLevel {
public static void f() {}
static int x = 10;
}
}
public static Destination destination(String s) {
return new ParcelDestination(s);
}
public static Contents contents() {
return new ParcelContents();
}
public static void main(String[] args) {
Contents c = contents();
Destination d = destination("Tasmania");
}
}
public interface ClassInInterface {
void howdy();
class Test implements ClassInInterface {
public void howdy() {
System.out.println("Howdy!");
}
public static void main(String[] args) {
new Test().howdy();
}
}
}
关于内部类就总结这些,因为这些平时很少用到,做下笔记,当做个备忘录吧
参考文档:《Java编程思想》第四版