1 课程讲解
2 代码演练
2.1 非面向接口编程
2.2 面向接口编程1 传参
2.3 面向接口编程2 构造函数
2.4 面向接口编程3 set方法
1 课程讲解
2 代码演练
2.1 非面向接口编程
缺点:
测试类:
package com.geely.design.principle.dependencyinversion; public class Test { //v1.1 普通方法调取java类 public static void main(String[] args) { Geely geely = new Geely(); geely.studyJavaClass(); geely.studyPreClass(); } }
实体类:
package com.geely.design.principle.dependencyinversion; public class Geely { public void studyJavaClass(){ System.out.println("Geely Study Java Lesson"); } public void studyPreClass(){ System.out.println("Geely Study Pre Lesson"); } }
打印结果:
Geely Study Java Lesson Geely Study Pre Lesson Process finished with exit code 0
2.2 面向接口编程1 传参
缺点:
测试类:
package com.geely.design.principle.dependencyinversion; public class Test { //v1.1 普通方法调取java类 /*public static void main(String[] args) { Geely geely = new Geely(); geely.studyJavaClass(); geely.studyPreClass(); }*/ //v1.2 面向接口编程 传参 public static void main(String[] args) { Geely geely = new Geely(); geely.studyImoocClass(new JavaCourse()); geely.studyImoocClass(new PreCourse()); } }
实体类:
package com.geely.design.principle.dependencyinversion; public class Geely { public void studyJavaClass(){ System.out.println("Geely Study Java Lesson"); } public void studyPreClass(){ System.out.println("Geely Study Pre Lesson"); } public void studyImoocClass(Icourse icourse){ icourse.studyClass(); } }
课程类1:
package com.geely.design.principle.dependencyinversion; public class JavaCourse implements Icourse{ @Override public void studyClass() { System.out.println("Geely Study Java Lesson"); } }
课程类2:
package com.geely.design.principle.dependencyinversion; public class PreCourse implements Icourse{ @Override public void studyClass() { System.out.println("Geely Study Pre Lesson2"); } }
课程接口:
package com.geely.design.principle.dependencyinversion; public interface Icourse { void studyClass(); }
打印日志:
Geely Study Java Lesson Geely Study Pre Lesson2 Process finished with exit code 0
2.3 面向接口编程2 构造函数
2.4 面向接口编程3 set方法
原文地址:https://www.cnblogs.com/1446358788-qq/p/10962800.html
时间: 2024-10-19 22:24:51