1. 内部类的特性
2. 使用.this
package net.mindview.innerclasses;
public class DotThis {
void f(){
System.out.println("DotThis.f()");
}
public class Inner {
public DotThis outer() {
System.out.println(DotThis.this);
return DotThis.this;
}
}
public Inner inner(){
return new Inner();
}
public static void main(String[] args) {
DotThis dt = new DotThis();
DotThis.Inner di = dt.inner();
di.outer().f();
}
}
3.使用.new
package net.mindview.innerclasses;
public class DotNew {
public class Inner {
}
public static void main(String[] args) {
DotNew dn = new DotNew();
DotNew.Inner di = dn.new Inner();
}
}
4. 方法的内部类
package net.mindview.innerclasses;
/**
* 方法内部类的作用域就在方法内,方法外不可以访问.
* @author samsung
*
*/
public class Parcel5 {
public Destination destination(String s){
//在方法里定义了一个内部类
class PDestination implements Destination{
private String label;
public PDestination(String label){
System.out.println(this);
this.label = label;
}
@Override
public String readLabel() {
return label;
}
}
return new PDestination(s);
}
public static void main(String[] args) {
Parcel5 p = new Parcel5();
System.out.println(p.destination("上海").readLabel());
System.out.println(p.destination("北京").readLabel());
}
/**
* 注意:PDestination定义在方法里面,并不是说只有调用方法的时候,这个类才被创建.
* 实际上,这个类和其他类一样,已经经过编译了.
*/
}
5. 定义在作用域内的内部类, 此作用域在方法的内部.
package net.mindview.innerclasses;
public class Parcel6 {
private void internalTracking(boolean b){
if(b){
class TrackingSlip{
private String id;
TrackingSlip(String s){
id = s;
}
String getSlip(){
return id;
}
}
TrackingSlip ts = new TrackingSlip("100");
String s = ts.getSlip();
}
//作用域外不可使用
//TrackingSlip ts = new TrackingSlip("100");
}
public void track(){
internalTracking(true);
}
public static void main(String[] args) {
Parcel6 p = new Parcel6();
p.track();
}
}
6.匿名内部类
package net.mindview.innerclasses;
public class Parcel7 {
public Contents contents(){
//定义了一个匿名内部类.将返回值和创建类放在了一起
return new Contents(){
private int i = 11;
@Override
public int value() {
return i;
}
};
}
public static void main(String[] args) {
Parcel7 p = new Parcel7();
Contents c = p.contents();
System.out.println(c.value());
}
}
package net.mindview.innerclasses;
public class Wrapping {
private int i;
public Wrapping(int x){
this.i = x;
}
public int value(){
return i;
}
}
package net.mindview.innerclasses;
public class Parcel8 {
public Wrapping wrapping(int x){
return new Wrapping(x){
@Override
public int value(){
return super.value()*47;
}
};
}
public static void main(String[] args) {
Parcel8 p = new Parcel8();
Wrapping w = p.wrapping(20);
System.out.println(w.value());
}
}
package net.mindview.innerclasses;
public class Wrapping {
private int i;
public Wrapping(int x){
this.i = x;
}
public int value(){
return i;
}
}
package net.mindview.innerclasses;
public class Parcel9 {
public Destination destination(final String x){
final String m = null;
//匿名内部类
return new Destination(){
//在内部类中使用到了外部的对象
private String label = m;
private String abc = x;
@Override
public String readLabel() {
return label;
}
};
}
public static void main(String[] args) {
Parcel9 p = new Parcel9();
System.out.println(p.destination("20").readLabel());
}
/**
* 定义一个匿名内部类, 如果希望在匿名内部类中使用外部的对象,
* 那么编译器要求外部类的这个参数必须是final的. 如果你没有将这个类定义为final
* 编译器会报错
*/
}
package net.mindview.innerclasses;
abstract class Base{
public Base(int i){
System.out.println("Base构造器, i=" + i);
}
public abstract void f();
}
public class AnonymousConstructor {
/**
* 此处的i不要求是final的,因为这个i只是传递给了匿名
* 变量的构造器,并没有在匿名内部类内部使用.
*/
public static Base getBase(int i){
return new Base(i){
{
System.out.println("这里可以类似构造器进行初始化");
}
@Override
public void f() {
System.out.println("匿名函数f()");
}
};
}
public static void main(String[] args) {
AnonymousConstructor amc = new AnonymousConstructor();
amc.getBase(100).f();
}
}
7. 再谈工厂设计模式.
再上一章中说了工厂设计模式. 本书中的案例是对象工厂设计模式. 来看看是怎么写的
package net.mindview.innerclasses.factory.MethodFactory;
/**
* 服务类
*/
interface Service{
void method1();
void method2();
}
/**
* 工厂类
*/
interface ServiceFactory {
Service getService();
}
class Implementation1 implements Service{
@Override
public void method1() {
System.out.println("Implementation1 method1");
}
@Override
public void method2() {
System.out.println("Implementation1 method1");
}
}
class Implementation2 implements Service{
@Override
public void method1() {
System.out.println("Implementation2 method1");
}
@Override
public void method2() {
System.out.println("Implementation2 method1");
}
}
class ImplementationFactory1 implements ServiceFactory{
@Override
public Service getService() {
// TODO Auto-generated method stub
return new Implementation1();
}
}
class ImplementationFactory2 implements ServiceFactory{
@Override
public Service getService() {
// TODO Auto-generated method stub
return new Implementation2();
}
}
public class Factories {
public static void serviceSustomer(ServiceFactory factory){
factory.getService().method1();
factory.getService().method2();
}
public static void main(String[] args) {
serviceSustomer(new ImplementationFactory1());
serviceSustomer(new ImplementationFactory2());
}
}
我们还可以将这种方式以内部类的方式来实现
package net.mindview.innerclasses.factory.methodFactoryInnerClass;
/**
* 服务类
*/
interface Service{
void method1();
void method2();
}
/**
* 工厂类
*/
interface ServiceFactory {
Service getService();
}
/**
* 每一种服务,提供一种工厂. 这种工厂专门创建这种服务.
*/
class Implementation1 implements Service{
private Implementation1(){}
@Override
public void method1() {
System.out.println("Implementation1 method1");
}
@Override
public void method2() {
System.out.println("Implementation1 method1");
}
//让工厂在内部实现
public static ServiceFactory factory = new ServiceFactory(){
@Override
public Service getService() {
return new Implementation1();
}
};
}
class Implementation2 implements Service{
@Override
public void method1() {
System.out.println("Implementation2 method1");
}
@Override
public void method2() {
System.out.println("Implementation2 method1");
}
//让工厂在内部实现
public static ServiceFactory factory = new ServiceFactory(){
@Override
public Service getService() {
return new Implementation2();
}
};
}
public class Factories {
public static void serviceSustomer(ServiceFactory factory){
factory.getService().method1();
factory.getService().method2();
}
public static void main(String[] args) {
serviceSustomer(Implementation1.factory);
serviceSustomer(Implementation2.factory);
}
}
我的理解是: 第一种工厂的含义是: 我现在需要一种服务, 然后我找到对应的工厂, 为我提供服务. 工厂再去找对应这种服务的实体.
而第二种方式呢, 我现在需要服务. 直接就去找这种服务对应的工厂就可以了. 也就是工厂内置了. 就像大公司下有多个子公司, 每个子公司都有自己的工厂.我要那种类型的东西,他就自己给生产了
8. 嵌套类
如果内部类对象预期外围类对象之间不需要有联系, 那么可以将内部类声明为static的。这种累通常称为嵌套类。要想理解static定义的内部类的含义,就必须记住,普通的内部类对象隐式地保存了一个引用,指向创建他的外围类对象。然而,当内部类是static的时候, 就不是这样了。嵌套类意味着:
package net.mindview.innerclasses;
public class Parcel11 {
//ParcelContent被定义为static的,所以是一个嵌套类
private static class ParcelContent implements Contents {
int i = 11;
@Override
public int value() {
return i;
}
}
//ParcelDestination 是一个嵌套类. 被定义为static的.所以,它内部可以的有static的成员和方法
private static class ParcelDestination implements Destination {
private String label;
private ParcelDestination(String label){
this.label = label;
}
@Override
public String readLabel() {
return label;
}
public static void f(){
System.out.println(AnotherLabel.x);
//b()方法是访问不到的
//AnotherLabel.b();
}
static int x = 10;
//他是嵌套在嵌套类内部的嵌套类.
static class AnotherLabel{
//内部定义了两个和外部一样的成员方法, 竟然不冲突
public static void f(){
System.out.println("这时静态类的静态方法");
}
static int x = 10;
//嵌套类中的非静态方法不能被外部访问到. 所以这个方法没有任何意义
public void b(){
System.out.println("这时静态类中的静态方法b()");
}
}
}
public static Contents getContents(){
return new ParcelContent();
}
public static Destination getDestination(String s){
return new ParcelDestination(s);
}
public static void main(String[] args) {
System.out.println(getContents().value());
System.out.println(getDestination("北京").readLabel());
//嵌套类中,不能访问非静态方法
//ParcelDestination.readLabel();
//但是可以方法静态方法
ParcelDestination.f();
//嵌套类的使用方式是,类似于静态方法. 将其作为静态方法来使用.
ParcelDestination.AnotherLabel.x=1;
System.out.println(ParcelDestination.AnotherLabel.x);
ParcelDestination.AnotherLabel.f();
//下面这样写是不对的
//ParcelDestination.AnotherLabel.b();
}
}
9. 接口内部的类
package net.mindview.innerclasses;
/**
* 定义在接口内部的类
*/
public interface ClassInInterface {
void howdy();
//这里static必须是显示标记出来的,否则这个类将不能运行
static class Test implements ClassInInterface {
@Override
public void howdy() {
System.out.println("Howdy!");
}
public static void main(String[] args) {
Test t = new Test();
t.howdy();
}
}
}
package net.mindview.innerclasses;
/**
* 定义在接口内部的类
*/
public interface ClassInInterface {
void howdy();
//这里static必须是显示标记出来的,否则这个类将不能运行
static class Test implements ClassInInterface {
@Override
public void howdy() {
System.out.println("Howdy!");
}
public static void main(String[] args) {
//测试第一个实现了接口的类
ClassInInterface t = new Test();
t.howdy();
//测试第二个实现了接口的类
ClassInInterface t1 = new ClassInInterfaceTest();
t1.howdy();
}
}
}
package net.mindview.innerclasses;
public class TestBed {
public void f(){
System.out.println("f()");
}
static class Testers{
public static void main(String[] args) {
TestBed t = new TestBedTest();
t.f();
}
}
}
package net.mindview.innerclasses.test20;
interface InnerInInterface{
static class Inner{
public Inner(){
System.out.println("嵌套类构造方法");
}
}
}
public class Test20 {
public static void main(String[] args) {
//直接使用 new 外围类.嵌套类
InnerInInterface.Inner inner = new InnerInInterface.Inner();
}
}
10. 从多层嵌套类中访问外部类的成员
注意下面这个例子是如何创建内部类的。
package net.mindview.innerclasses;
/**
* 多层嵌套类访问外部成员
*/
class A{
private void f(){};
class B{
private void b(){};
class C {
void c(){
f();
b();
};
}
}
}
public class MutliNestingAccess {
public static void main(String[] args) {
A a = new A();
A.B b = a.new B();
A.B.C c = b.new C();
c.c();
}
}
iooo