1.USB
package com.yfs.javase; public interface USB { //定义规范 public void read(); public void write(); }
2.UDisk读写
package com.yfs.javase; public class UDisk implements USB { @Override public void read() { System.out.println("从U盘读取数据..."); } @Override public void write() { System.out.println("向U盘写入数据..."); } }
3.只读
package com.yfs.javase; public class OnlyRead implements USB { @Override public void read() { System.out.println("读取数据..."); } //空实现 @Override public void write() { //return; } }
4.Mp3读写
package com.yfs.javase; public class Mp3 implements USB { @Override public void read() { System.out.println("从Mp3读取数据..."); } @Override public void write() { System.out.println("向Mp3写入数据..."); } }
5.Computer
package com.yfs.javase; public class Computer { private USB usb; //添加usb设备 public void addUsb(USB u) { usb = u; u.read(); u.write(); } //删除usb设备 public void deleteUsb() { if(usb != null) { usb = null; System.out.println("设备已删除..."); } else { System.out.println("没有usb设备..."); } } }
6.测试
package com.yfs.javase; public class Test { public static void main(String[] args) { Computer pc = new Computer(); pc.deleteUsb(); pc.addUsb(new Mp3()); pc.deleteUsb(); pc.addUsb(new UDisk()); pc.deleteUsb(); } }
7.内部类
package com.yfs.javase; public class OutClass { private int a = 10; private void privateMethod() { } public void method1() { System.out.println("call method1()..."); //创建内部类对象 InnerClass inn = new InnerClass(); inn.iMethod1(); } public void method2() { System.out.println("call method2() ...."); } //内部类 为外部类提供服务 class InnerClass { public void iMethod1() { System.out.println("inner method()"); //外部类的属性方法 当自己的使用 method2(); System.out.println("a = " + a ); privateMethod(); } } }
8.方法中声明类
package com.yfs.javase; public class OutMethodClass { public void method() { //方法中声明类 只能在方法中使用 class Inner { public void innMe() { System.out.println("call InnMe()..."); } } Inner inn = new Inner(); inn.innMe(); } public void method2() { //Inner inn = new Inner(); } }
9.测试内部类
package com.yfs.javase; public class Test1 { public static void main(String[] args) { OutClass out = new OutClass(); out.method1(); //InnerClass inn = new InnerClass(); } }
10.letter模板
package com.yfs.javase; //信模板 public abstract class Templater { public abstract String toName() ; public abstract String fromName(); public abstract String getContent(); public void show() { System.out.println("尊敬的" + toName()); System.out.println("\t您好!"); System.out.println("\t\t" + getContent()); System.out.println("\t\t\t 此致 \n\t\t\t\t 敬礼" ); System.out.println("\t\t\t\t" + fromName()); System.out.println("\t\t\t\t 2015.7.25" ); } }
11.模板测试( 匿名类)
package com.yfs.javase.letter; import com.yfs.javase.Person; public class LetterTest { public static void main(String[] args) { Person p1 = new Person("张三"); p1.speak(); // 匿名类 new Person("李四").speak(); //创建对象 实现抽象方法 匿名类 new Templater() { @Override public String toName() { return "领导"; } @Override public String fromName() { return "员工"; } @Override public String getContent() { return "涨点工资吧..."; } }.show(); } }
时间: 2024-10-12 04:38:15