抽象类
抽象类不能被实例化
但可以创建一个引用变量,其类型是一个抽象类,指向非抽象的子类实例
抽象方法没有方法体
抽象类与抽象方法的使用
(1)抽象类中可以没有抽象方法,但包含了抽象方法的类必须被定义为抽象类
(2)如果子类没有实现父类的所有抽象方法,子类必须被定义为抽象类
(3)没有抽象构造方法,也没有抽象静态方法
(4)抽象类中可以有非抽象的构造方法,创建子类的实例时可能调用
接口
接口:
定义接口使用interface关键字
实现接口使用implements关键字
接口可以多实现(一个类可以实现多个接口),用逗号分隔
接口不能被实例化
接口中的所有方法默认都是公共抽象方法
接口的目的之一是订立一个标准
实现类必须实现接口总的所有方法,除非这个类是抽象类
接口的使用:
接口中的成员变量:
默认都是public static final的,必须显式初始化
接口中的方法:
默认都是public abstract的
接口没有构造方法,不能被实例化
一个接口不能实现另一个接口,但可以继承多个其他接口
一个类必须实现接口抽象方法(implements),除非这个类也是抽象类
抽象类和接口的比较
相同点:
代表系统的抽象层
都不能被实例化
都能包含抽象方法
用于描述系统提供的服务,不必提供具体实现
不同点:
在抽象类中可以为部分方法提供默认实现,而接口中只能包含抽象方法
抽象类便于复用,接口便于代码维护
一个类只能继承一个直接的父类,但可以实现多个接口
使用原则:
接口做系统与外界交互的窗口
接口提供服务
接口本身一旦制定,就不允许随意修改
抽象类可完成部分功能实现,还有部分功能可作为系统的扩展点
面向对象设计原则
设计原则:
多用组合,少用继承
针对接口编程
针对扩展开放,针对改变关闭
总结
Java中的接口
属性全都是全局静态常量
方法都是全局抽象方法
无构造方法
一个类可以实现多个接口,非抽象类实现接口时必须实现接口中的全部方法
抽象类利于代码复用,接口利于代码维护
授课代码
例1:门功能的实现和锁功能的实现
抽象Door类:
public abstract class Door {
public abstract void open();
public abstract void close();
}
Lock接口:
public interface Lock {//interface关键字声明接口
//在接口中所有的方法都是公共抽象方法
public abstract void lockUp();
void lockDown();
}
Camera接口:
public interface Camera {
void takePhoto();
}
DefenderDoor类:
public class DefenderDoor extends Door implements Lock,Camera {
@Override
public void open() {
System.out.println("门开了!");
takePhoto();
}
@Override
public void close() {
System.out.println("门关了!");
takePhoto();
}
@Override
public void lockUp() {
System.out.println("锁开了!");
}
@Override
public void lockDown() {
System.out.println("锁上了!");
}
@Override
public void takePhoto() {
System.out.println("拍照");
}
}
Master类:
public class Master {
String name;
DefenderDoor door;
public void getIn(){
System.out.println(name + "掏出一串钥匙,插入锁孔,开始转动");
door.lockUp();
door.open();
System.out.println("进入房间");
}
public void getOut(){
System.out.println("走出房门");
door.close();
System.out.println("掏出一串钥匙,插入锁孔,开始转动");
door.lockDown();
}
}
TestMaster类:
public class TestMaster {
public static void main(String[] args) {
Master m = new Master();
m.name = "包租婆";
m.door = new DefenderDoor();
m.getIn();
m.getOut();
Door r = new DefenderDoor();
Lock l = new DefenderDoor();
}
}
例2:手机不通实现功能不通
抽象Handset类:
public abstract class Handset {
String brand;
String type;
public Handset() {
}
public Handset(String brand, String type) {
this.brand = brand;
this.type = type;
}
public void sendInfo(){
System.out.println( "发送短信");
}
public void call(){
System.out.println("打电话");
}
public void info(){
System.out.println("读短信");
}
}
NetWork接口:
public interface NetWork {
void network();
}
PlayWiring接口:
public interface PlayWiring {
void play();
}
AptitudeHandset类:
public class AptitudeHandset extends Handset implements Camera, NetWork,PlayWiring {
public AptitudeHandset() {
}
public AptitudeHandset(String brand, String type) {
super(brand, type);
}
@Override
public void takePhoto() {
System.out.println("用" + this.brand + this.type +"拍照片");
}
@Override
public void network() {
System.out.println(this.brand + this.type +"上网");
}
@Override
public void play() {
System.out.println(this.brand + this.type +"播放音乐");
}
}
CommonHandset类:
public class CommonHandset extends Handset implements PlayWiring {
public CommonHandset() {
}
public CommonHandset(String brand, String type) {
super(brand, type);
}
@Override
public void play() {
System.out.println(this.brand + this.type +"播放音乐");
}
}
Master类:
public class Master {
public void paly(Handset a){
a.call();
a.info();
a.sendInfo();
if(a instanceof AptitudeHandset){
// a = new AptitudeHandset("huawei","10");
((AptitudeHandset) a).play();
((AptitudeHandset) a).takePhoto();
}
if(a instanceof CommonHandset){
// a = new CommonHandset("laonain","11");
((CommonHandset) a).play();
}
}
}
TestHandset类:
public class TestHandset {
public static void main(String[] args) {
Master m = new Master();
Handset a = new AptitudeHandset("苹果","11");
Handset c = new CommonHandset("vivo","老年机");
m.paly(a);
m.paly(c);
}
}
例3:打印机的墨盒和打印纸
InkBox接口:
public interface InkBox {
String getColor();
}
BlackInkBox类:
public class BlackInkBox implements InkBox {
@Override
public String getColor() {
return "黑白";
}
}
ColorfulInkBox类:
public class ColorfulInkBox implements InkBox {
@Override
public String getColor() {
return "Colorful";
}
}
Paper接口:
public interface Paper {
String getSize();
}
A4Paper类:
public class A4Paper implements Paper {
@Override
public String getSize() {
return "A4";
}
}
B5Paper类:
public class B5Paper implements Paper {
@Override
public String getSize() {
return "B5";
}
}
Printer类:
public class Printer {
public void print(Paper p,InkBox box) {
System.out.println("使用" + box.getColor() + "墨盒,打印" + p.getSize() + "纸张");
}
public static void main(String[] args) {
Printer p = new Printer();
InkBox box = new BlackInkBox();
Paper paper = new A4Paper();
p.print(paper,box);
}
}
例4:USB接口
USB接口:
public interface USB {
public static final int LENGTH = 10;
int WIDTH = 3;
double POWER = 1.5;
void connect();
// void power();
}
USBMouse类:
public class USBMouse implements USB {
@Override
public void connect() {
System.out.println("点击鼠标,啪啪啪,lol玩起来!!");
}
}
Computer类:
public class Computer {
public void control(USB usbDevice){
usbDevice.connect();
}
}
USBFlash类:
public class USBFlash implements USB {
@Override
public void connect() {
System.out.println("50G的种子请拿走!");
}
public static void main(String[] args) {
Computer c = new Computer();
USBMouse m = new USBMouse();
USBFlash f = new USBFlash();
c.control(m);
c.control(f);
}
}
例5:组装电脑
CPU接口:
public interface CPU {
String brand();
double pin();
}
IntelCUP类:
public class IntelCUP implements CPU {
@Override
public String brand() {
return "Intel";
}
@Override
public double pin() {
return 3.8;
}
}
HardDisk接口:
public interface HardDisk {
int amount();
}
WDHardDisk类:
public class WDHardDisk implements HardDisk {
@Override
public int amount() {
return 3000;
}
}
RAM接口:
public interface RAM {
int nei();
}
JSDRAM接口:
public class JSDRAM implements RAM {
@Override
public int nei() {
return 4;
}
}
Computer类:
public class Computer {
public void zz(CPU c,HardDisk h,RAM r){
System.out.println("计算机的信息如下:");
System.out.println("CPU的品牌是" + c.brand() + ",主频是:" + c.pin() + "GHz");
System.out.println("硬盘的容量是: " + h.amount() + "GB");
System.out.println("内存的容量是:" + r.nei() + "G");
}
public static void main(String[] args) {
Computer computer = new Computer();
CPU c = new IntelCUP();
HardDisk h = new WDHardDisk();
RAM r = new JSDRAM();
computer.zz(c,h,r);
}
}