1、Pg168-1
package org.hanqi.pn0120; public class Computer { private double neicundx=10; public double getNeicundx() { return neicundx; } public void setNeicundx(double neicundx) { this.neicundx = neicundx; } public Computer() { } public static void main(String[] args) { Computer myComputer=new Computer(); myComputer.setNeicundx(4); System.out.println("内存大小=" +myComputer.getNeicundx()); } }
package org.hanqi.pn0120; public class Text3 { public static void main(String[] args) { Computer myComputer1=new Computer(); System.out.println("myComputer1内存大小=" +myComputer1.getNeicundx()); } }
2、Pg167 单例模式的应用
package org.hanqi.pn0120; public class Emperor { private static Emperor emperor=null; private Emperor() { } public static Emperor getInstance() { if(emperor==null) { emperor=new Emperor(); } return emperor; } public void getName() { System.out.println("我是皇帝:明日科技"); } }
package org.hanqi.pn0120; public class Test { public static void main(String[] args) { System.out.println("创建皇帝1对象:"); Emperor emperor1=Emperor.getInstance(); emperor1.getName(); System.out.println("创建皇帝2对象:"); Emperor emperor2=Emperor.getInstance(); emperor2.getName(); System.out.println("创建皇帝3对象:"); Emperor emperor3=Emperor.getInstance(); emperor3.getName(); } }