单一职责:尚硅谷
package com.zlt.principle.singleresponsibility; public class SingleResponsibility2 { public static void main(String[] args) { RoadVehicle roadVehicle = new RoadVehicle(); roadVehicle.run("摩托"); roadVehicle.run("汽车"); WaterVehicle waterVehicle = new WaterVehicle(); waterVehicle.run("轮船"); AirVehicle airVehicle = new AirVehicle(); airVehicle.run("飞机"); } } // class RoadVehicle{ public void run(String vehicle){ System.out.println(vehicle+"在公路上运行"); } } class WaterVehicle{ public void run(String vehicle){ System.out.println(vehicle+"在水上运行"); } } class AirVehicle{ public void run(String vehicle){ System.out.println(vehicle+"在空中运行"); } }
方法二:
package com.zlt.principle.singleresponsibility; public class SingleResponsibility3 { public static void main(String[] args) { Vehicle2 vehicle2 = new Vehicle2(); vehicle2.run("汽车"); vehicle2.runAir("飞机"); vehicle2.runWater("轮船"); } } //方式3分分析 //1.这种修改方法没有对原来的类做大的修改,只是增加方法 //2.这里虽然没有在类这个级别上遵守单一职责原则,但是在方法级别上,仍然是遵守单一职责 class Vehicle2 { public void run(String vehicle) { System.out.println(vehicle + " 在公路上运行~~"); } public void runAir(String vehicle){ System.out.println(vehicle+"在天上飞"); } public void runWater(String vehicle){ System.out.println(vehicle+"在水里游"); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)