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

练习代码(五)接口与内部类(一)

2015-06-22 21:23 357 查看
盗版的抽象工厂。

interface Factory{

public Product factory(String s);

}

class CreateFactory1 implements Factory{

public Product factory(String s){

if(s=="a1")

return new CreateProductA1();

else if(s=="b1")

return new CreateProductB1();

else

System.out.println("error");

return null;

}

}

class CreateFactory2 implements Factory{

public Product factory(String s){

if(s=="a2")

return new CreateProductA2();

else if(s=="b2")

return new CreateProductB2();

else

System.out.println("empty");

return null;

}

}

interface Product{

public void method();

}

class ProductA implements Product{

public void method(){}

}

class ProductB implements Product{

public void method(){}

}

class CreateProductA1 extends ProductA{

public CreateProductA1(){

System.out.println("CreateProductA1()");

}

}

class CreateProductA2 extends ProductA{

public CreateProductA2(){

System.out.println("CreateProducA2()");

}

}

class CreateProductB1 extends ProductB{

public CreateProductB1(){

System.out.println("CreateProductB1()");

}

}

class CreateProductB2 extends ProductB{

public CreateProductB2(){

System.out.println("CreateProductB2()");

}

}

public class Cilent {

public static void main(String[] args){

Factory factory1 , factory2 ;

ProductA productA1 , productA2 ;

ProductB productB1 , productB2 ;

factory1 = new CreateFactory1();

factory2 = new CreateFactory2();

productA1 = (ProductA) factory1.factory("a1");

productA2 = (ProductA) factory2.factory("a2");

productB1 = (ProductB) factory1.factory("b1");

productB2 = (ProductB) factory2.factory("b2");

}

}

输出结果:

CreateProductA1()

CreateProducA2()

CreateProductB1()

CreateProductB2()

外部类中有一个方法返回指向内部类引用时,静态方法中创建内部类对象的情况。

public class Parce {

class Contents{

Contents(){

System.out.println("Contents()");

}

private int i =11;

public int value(){ return i; }

}

class Destination{

private String label;

Destination(String whereTo){

label = whereTo;

}

String readlable(){ return label; }

}

public Contents cont(){

return new Contents();

}

public void ship(String s){

Contents c = new Contents();

Destination d = new Destination(s);

System.out.println(d.readlable());

}

public static void main(String[] args){

Parce p = new Parce();

Parce.Contents a = p.cont();

System.out.println(a.value());

p.ship("Hello World");

}

}

输出结果:

Contents()

11

Contents()

Hello World

当内部类被定义为private时,该怎么获得此类的引用,进而使用其中元素。

interface Destination{

String readLabel();

}

interface Contents{

int value();

}

class Parce11{

private class PContents implements Contents{

private int i = 11;

public int value(){ return i ;}

}

protected class PDestination implements Destination{

private String label ;

PDestination(String whereTo){

label = whereTo;

}

public String readLabel(){ return label;}

}

public PContents cont(){

return new PContents();

}

public PDestination dest(String a){

return new PDestination(a);

}

}

public class TestParcel {

public static void main(String[] args){

Parce11 p = new Parce11();

Contents cc = p.cont();

System.out.println(cc.value());

//! Parce.PContents c = p.cont();
private!

Parce11.PDestination d = p.dest("PDestination()");

Destination dd = p.dest("PDestination()");

System.out.println(dd.readLabel());

System.out.println(d.readLabel());

}

}

输出结果:

11

PDestination()

PDestination()

封装

public class Man {

private String name;

private int age;

private Women wife;

public void setName(String name){

this.name = name;

}

public void setAge(int age){

this.age = age;

}

public void setWife(Women wife){

this.wife = wife;

}

public String getName(){

return name;

}

public int getAge(){

return age;

}

}

class Women{

private String name;

private int age;

private Man husband;

public void setName(String name){

this.name = name;

}

public String getName(){

return name;

}

public void setAge(int age){

this.age = age;

}

public int getAge(){

return age;

}

public void setHusband(Man husband){

this.husband = husband;

}

public Man getHusband(){

return husband;

}

}

局部内部类

public class Parce14 {

public Destination dest(String s){

class PDestination implements Destination{

private String label;

private PDestination(String whereTo){

label = whereTo;

}

public String readLabel(){ return label; }

}

return new PDestination(s);

}

public static void main(String[] args){

Parce14 p = new Parce14();

Destination d = p.dest("Tanzania");

}

}

如何在任意的作用域内嵌入一个内部类。

public class Parce15 {

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("slip");

String s = ts.getSlip();

}

}

public void track(){ internalTracking(true); }

public static void main(String[] args){

Parce15 p =new Parce15();

p.track();

}

}

通过嵌入if语句的作用域内,实现在任意的作用域可用。并不是说该类的创建是需要条件的,其实它与别的类一起编译过了。然而,在定义TrackingSlip的作用域之外,它是不可用的;除此之外,它和普通类一样。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: