1、public abstract class Person Create protected GamePers on pers on=new GamePers on();public abstract void createFace();public abstract void createGe nder();public abstract void createCloth();public GamePers on getPers on()retur n pers on;具体建造者类:public class Person Typel exte nds Person Create public
2、void createFace() person.setFace(” 瓜子脸);public void createGe nder() person.setGender(美女public void createCloth() person.setCloth( 洛丽塔具体建造类:public class Person Type2 exte nds Person Create public void createFace() person.setFace( 国字脸public void createGe nder() person.setGender(” 帅哥 西装革履指挥者类:public cl
3、ass GamePlayer private Person Create pc;public void choseType(Pers on Create pc) this.pc=pc;public GamePers on create() pc.createCloth(); pc.createFace(); pc.createGe nder(); retur n pc.getPers on();测试类:public class Test public static void main( Stri ng args) Pers on Create pc=new Pers on Type1();Ga
4、mePlayer gp=new GamePlayer(); gp.choseType(pc);GamePers on pers on=gp.create();KM 穿着System.out.pri ntln(” 游戏人物特征:”);System.out.println(” 长着一张+person.getFace()+person.getCloth()+ 的+person.getGender();课后第二题:public class Computer private String cpu;private String storage;public String getCpu() return c
5、pu;public void setCpu(Stri ng cpu) this.cpu = cpu;public String getStorage() return storage;public void setStorage(Stri ng storage) this.storage = storage;public abstract class Factory protected Computer c=new Computer();public abstract void in stallCpu();public abstract void in stallStorage();publi
6、c Computer getComputer()return c;public class Desktop exte nds Factory public void in stallCpu() c.setCpu(AMDpublic void in stallStorage() c.setStorage(8G 内存”);public class Laptop exte nds Factory i ntel1G 内存public class User private Factory f;public void buy(Factory f)this.f=f;public Computer con()
7、f.in stallCpu();f.i nstallStorage();return f.getComputer();public static void main(String args) Factory f=new Laptop();User u=new User();u.buy(f);Computer c=u.c on();System.out.println(c.getCpu()+ +c.getStorage();单例模式懒汉式模式:public class SingletonWindow extends JinternalFrame private static SingletonW
8、indow instanee =null ;private Sin gleto nWin dow() super (内部窗口 , true , true , true );System. out.println( ”创建了一个内部窗体 ”);public static Sin glet onWin dow getl nsta nce()if (instanee =null )in sta nee =new Sin glet onWin dow();else System. out .println( ”已经创建了一个内部窗体! ”);return instanee ;public class
9、Main extends JFrame private static final long serialVersionUID = 1L;private JButton btnAdd;private JPanel btnpl ;private JDesktopPane dtp;private JinternalFrame itf ;public Main() this .setSize( new Dimension(600, 700);this .setDefaultCloseOperation( EXIT_ON_CLOSEthis .setResizable( false );this .se
10、tVisible( true );this .setLocationRelativeTo( this );this .setTitle( 实验 2this .setLayout( null );this . dtp = new JDesktopPane();this . dtp .setBounds( new Rectangle(0, 0, 600, 600);this . btnpl =new JPanel();this . btnpl .setBounds( new Rectangle(0, 600, 600, 100);this . btnAdd = new JButton(添加一个内部
11、窗体 ”);this . btnAdd .setBounds( new Rectangle(10, 10, 150, 30);this .add( dtp);this .add( btnpl );this . btnpl .add( btnAdd);public void actionPerformed(ActionEvent argO) itf =Sin glet onWin dow. get In sta nee ();itf .setSize(200, 200);itf .setVisible( true ); dtp .add( itf ););new Mai n();适配器模式课后第
12、一题目标抽象类:public abstract class Robot public abstract void run();public abstract void cry();适配者类:public class Dog public void run()System.out.println(” 狗跑public class Bird public void cry()System.out.println(” 鸟叫适配器类:public class RobotAdapter exte nds Robot private Bird bird;private Dog dog;public RobotAdapter(Bird bird,Dog dog) this.bird=bird;this.dog=dog;public void run() System.out.print( 机器人学”);dog.ru n();public void cry() 机器人
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1