接口隔离原则

1.学习

1.违反接口隔离原则

代码行74(ctrl+alt+L)

public class Isolation {
    public static void main(String[] args) {
        A a=new A();
        a.method1();
        a.method2();
        B b=new B();
        b.method3();
        b.method4();
        b.method5();
    }
}
//A类只需要实现1,2方法
class A implements Method{

    public void method1() {
        System.out.println("1");
    }

    public void method2() {
        System.out.println("2");
    }

    public void method3() {
        System.out.println("3");
    }

    public void method4() {
        System.out.println("4");
    }

    public void method5() {
        System.out.println("5");
    }
}
//B类只需要实现3,4,5方法
class B implements Method{
    public void method1() {
        System.out.println("1");
    }

    public void method2() {
        System.out.println("2");
    }

    public void method3() {
        System.out.println("3");
    }

    public void method4() {
        System.out.println("4");
    }

    public void method5() {
        System.out.println("5");
    }
}

//接口实现的方法
interface Method{
    void method1();
    void method2();
    void method3();
    void method4();
    void method5();
}

上面中:A主要实现method1,method2,但结果实现了全部方法,B也是
用接口隔离原则:代码行56(ctrl+alt+L)

package com.example.sunweihao.InterfaceIsolationPrinciple.OK;

public class Isolation {
    public static void main(String[] args) {
        A a = new A();
        a.method1();
        a.method2();
        B b = new B();
        b.method3();
        b.method4();
        b.method5();
    }
}

//A类只实现1,2方法
class A implements MethodA {
    public void method1() {
        System.out.println("1");
    }

    public void method2() {
        System.out.println("2");
    }
}

//B类只实现3,4,5方法
class B implements MethodB {

    public void method3() {
        System.out.println("3");
    }

    public void method4() {
        System.out.println("4");
    }

    public void method5() {
        System.out.println("5");
    }
}

//接口实现的方法
interface MethodA {
    void method1();

    void method2();
}

interface MethodB {

    void method3();

    void method4();

    void method5();
}

2.实践

3.掌握

4.教授

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值