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();
}
857

被折叠的 条评论
为什么被折叠?



