面向对象03

1.抽象类和抽象方法

/**
 * 抽象类的特点:
 * 01.由abstract修饰的类 ====》抽象类
 * 02.抽象类可以有普通方法和抽象方法
 * 03.不允许被实例化,不能通过new 关键字来创建对象
 * 04.子类必须重写父类中所有的抽象方法,除非子类也是抽象类
 */
public abstract class Animal {
    // 无参构造
    public Animal() {

    }

    /**
     *  抽象方法的特点  
     *  01.由abstract修饰的方法==》抽象方法
     *  02.没有方法体
     *  03.抽象方法必须在抽象类中
     */
    public abstract void gotoHospital();

}
Animal类
public class Dog extends Animal {

    // 子类重写父类的方法
    @Override
    public void gotoHospital() {
        System.out.println("小狗狗看病....");
    }
}
Dog类
public class Cat extends Animal {
    // 子类重写父类的方法
    @Override
    public void gotoHospital() {
        System.out.println("小猫咪看病....");
    }

}
Cat类

2.接口的使用

/**
 *  继承 animal  实现了 MyInterface
 */
public class Bird extends Animal implements MyInterface {

    public void fly() {
        System.out.println("小鸟在 飞翔.....");
    }

    /**
     * 这是MyInterface2中的方法
     */
    public void fly2() {
    }

}
Bird
/**
 * 
 * bird的父类
 */
public class Animal {

}
Animal
/**
 * 接口能解决我们java单根继承的问题!
 * 01.接口中所有的属性 默认都是   public  static  final修饰的    必须 有初始值  (静态常量)
 * 02.接口中所有的方法 默认都是   public  abstract修饰的 
 * 03.接口不能有构造方法,更不允许 实例化
 * 04.实现类必须重写接口中所有的方法!除非实现类也是抽象类或接口!
 * 05.接口可以继承 接口!
 * 
 * 
 * MyInterface  继承了 MyInterface2  
 * 因为自身是一个接口 所以不需要实现MyInterface2中的方法
 */
public interface MyInterface extends MyInterface2 {

    /**
     * 能力
     */
    public abstract void fly();

}
MyInterface
public interface MyInterface2 {

    /**
     * 能力
     */
    public abstract void fly2();

}
MyInterface2

3.usb小例子

/**
 * USB接口
 *
 */
public interface UsbInterface {
    /**
     * Usb接口提供的服务
     */
    void service();

}
UsbInterface
/**
 * U盘 
 */
public class UDisk implements UsbInterface {

    public void service() {
        System.out.println("连接usb接口,开始传输数据......");
    }

}
UDisk
/**
 * usb风扇
 *
 */
public class UsbFan implements UsbInterface {

    public void service() {
        System.out.println("连接usb接口,风扇开始转动.........");
    }

}
UsbFan
public class UsbTest {

    public static void main(String[] args) {

        // 父类的引用指向了 子类的对象
        UsbInterface fan = new UsbFan();
        fan.service();

        System.out.println("*******************");

        UsbInterface ud = new UDisk();
        ud.service();

    }
UsbTest 测试类

4.防盗门小例子

/**
 * 锁的接口
 *
 */
public interface LockInterface {

    /**
     * 开锁
     */
    void openLock();

    /**
     * 锁
     */
    void lockUp();

}
LockInterface
/**
 * 
 * 门
 */
public abstract class Door {
    // 开门
    public abstract void open();

    // 关门
    public abstract void close();

}
Door
/**
 * 飞云牌防盗门    继承了   门   实现了  锁的功能
 *
 */
public class FeiYunDoor extends Door implements LockInterface {

    public void openLock() {
        System.out.println("锁被打开了.....");
    }

    public void lockUp() {
        System.out.println("锁上了门.....");
    }

    public void open() {
        System.out.println("开门.....");
    }

    public void close() {
        System.out.println("关门.....");
    }

}
FeiYunDoor
public class DoorTest {

    public static void main(String[] args) {
        // 创建一个防盗门
        FeiYunDoor door = new FeiYunDoor();
        door.openLock();
        door.open();
        door.close();
        door.lockUp();
    }

}
DoorTest

5.打印机小例子

public interface Paper {
    // 得到具体纸张
    String getPage();

}
Paper纸张接口
public interface InkBox {
    // 得到墨盒颜色
    String getColor();

}
InkBox墨盒接口
public class A4Paper implements Paper {

    public String getPage() {
        return "A4";
    }

}
A4Paper
public class B5Paper implements Paper {

    public String getPage() {
        return "B5";
    }

}
B5Paper
public class Color implements InkBox {

    public String getColor() {
        return "彩色";
    }

}
Color
//打印机
public class Printer {
    /**
     * 打印机的属性
     */
    private Paper paper;
    private InkBox box;

    public Paper getPaper() {
        return paper;
    }

    public void setPaper(Paper paper) {
        this.paper = paper;
    }

    public InkBox getBox() {
        return box;
    }

    public void setBox(InkBox box) {
        this.box = box;
    }

    /**
     * 打印机打印 需要??
     * 纸张
     * 墨盒
     */
    public void print() {
        System.out.println("使用的墨盒颜色:" + box.getColor());
        System.out.println("使用的纸张:" + paper.getPage());

    }

}
Printer打印机实体类
public class TestPrinter {

    public static void main(String[] args) {
        // 实例化我们需要的纸张和墨盒
        Paper paper = new A4Paper();
        InkBox box = new Color();
        // 实例化打印机
        Printer printer = new Printer();
        printer.setBox(box);
        printer.setPaper(paper);
        printer.print();

    }
}
TestPrinter 测试类
原文地址:https://www.cnblogs.com/xtdxs/p/7094068.html