JAVA 入门 28 多态综合案例 内部类
USB 接口
package com.ITheima._polymorphic_test; public interface USB { void connect(); void unconnect(); }
KeyBoard键盘类
package com.ITheima._polymorphic_test; public class KeyBoard implements USB{ private String name; //无参构造器 public KeyBoard() { } //有参构造器 public KeyBoard(String name) { this.name = name; } //独有功能 public void keyDown(){ System.out.println("键盘按下"); } @Override public void connect(){ System.out.println(this.name+"成功链接了电脑"); } @Override public void unconnect(){ System.out.println(this.name+"成功断开了链接 "); } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Mouse类
package com.ITheima._polymorphic_test; public class Mouse implements USB{ private String name; //无参参 public Mouse() { } //有参 public Mouse(String name) { this.name = name; } //独有方法 public void dbClick(){ System.out.println("鼠标点击"); } //get set public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public void connect(){ System.out.println(this.name+"成功链接了电脑"); } @Override public void unconnect(){ System.out.println(this.name+"成功断开了链接 "); } }
Computer类
package com.ITheima._polymorphic_test; public class Computer { private String name; //无参 public Computer() { } //有参 public Computer(String name) { this.name = name; } //独有功能 public void start(){ System.out.println(this.name+"电脑开机了"); } /** * 提供安装USB设备的入口 */ public void installUSB(USB usb) { //多态 //接入 usb.connect(); //独有接口 if (usb instanceof KeyBoard) { KeyBoard k = (KeyBoard) usb; k.keyDown(); } else if (usb instanceof Mouse) { Mouse m=(Mouse)usb; m.dbClick(); } //拔出 usb.connect(); } //set/get public String getName() { return name; } public void setName(String name) { this.name = name; } }
Test类
package com.ITheima._polymorphic_test; /** * USB街交口==规范 * 1 定义USB接口:接入 拔出 * 2 定义2个USB的实现类: 鼠标 键盘 * 3 创建一个电脑对象, 创建USB设备对象 安装启动 */ public class Test { public static void main(String[] args) { //a 创建电脑对象 Computer c=new Computer("外星人"); c.start(); //b 创建鼠标对象 键盘对象 USB u=new KeyBoard("键盘"); USB k=new Mouse("鼠标"); c.installUSB(u); c.installUSB(k); } }
内部类
Outer类 及 Inner静态内部类
package com.ITheima._innerclass_static; /** * 外部内 */ public class Outer { public static int a=199; private String hobby="篮球"; /** * 学习静态成员内部类 */ public static class Inner{ private String name; private int age; public static String schoolName; public Inner() { } public Inner(String name, int age) { this.name = name; this.age = age; } public static String getSchoolName() { return schoolName; } public void show(){ System.out.println("名称"+name); //静态内部类中可以直接访问外部类的静态成员 System.out.println(Outer.a); System.out.println(a); //外部类的实例成员必须用外部类对象访问 //System.out.println(hobby); 报错 Outer o=new Outer(); System.out.println(o.hobby); } public static void setSchoolName(String schoolName) { Inner.schoolName = schoolName; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } }
匿名内部类
package com.ITheima._anonymous; /** * 目标:学习匿名内部类的形式和特点 */ public class Test { public static void main(String[] args) { Animal a=new Animal() { @Override public void run() { System.out.println("老虎跑的快"); } }; a.run(); } } //class Tiger extends Animal{ // @Override // public void run() { // System.out.println("老虎跑的快"); // } //} abstract class Animal{ public abstract void run(); }
package com.ITheima._anonymous; /** * 目标:掌握匿名内部类的使用形式(语法) */ public class Test1 { public static void main(String[] args) { //匿名内部类 Swimming s=new Swimming(){ @Override public void swim(){ System.out.println("学生快乐的游泳"); } }; go(s); System.out.println("-----------------------"); Swimming s1=new Swimming() { @Override public void swim() { System.out.println("老师游泳游泳的很快" ); } }; go(s1); } /** * 学生,老师 运动员可以一起参加比赛 */ public static void go(Swimming s){ System.out.println("开始"); s.swim(); System.out.println("结束"); } } interface Swimming{ void swim(); }