//单一职责原则,根据交通工具运行的方法不同,分解成不同的类
/*public class Main {
public static void main(String[] args) {
RoadVehicle roadVehicle = new RoadVehicle();
roadVehicle.run(“摩托车”);
roadVehicle.run(“汽车”);
AirVehicle airVehicle = new AirVehicle();
airVehicle.run(“飞机”);
WaterVehicle waterVehicle = new WaterVehicle();
waterVehicle.run(“轮船”);
}
}
class RoadVehicle {
public void run(String vehicle) {
System.out.println(vehicle + “在公路上跑”);
}
}
class AirVehicle {
public void run(String vehicle) {
System.out.println(vehicle + “在天空中飞”);
}
}
class WaterVehicle {
public void run(String vehicle) {
System.out.println(vehicle + “在水里游”);
}
}*/
//增加run方法,在类级别虽然没有遵守单一职责原则,但在方法级别上遵守了
/*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 + "在水里游");
}
}
public class Main {
public static void main(String[] args) {
Vehicle2 Vehicle = new Vehicle2();
Vehicle.run(“摩托车”);
Vehicle.run(“汽车”);
Vehicle.runAir(“飞机”);
Vehicle.runWater(“轮船”);
}
}*/
//开放封闭原则 ,违背,新增时需要修改太多
/*class Rectangle {
void draw() {
System.out.println(“矩形”);
}
}
class Circle {
void draw() {
System.out.println(“圆形”);
}
}
class Triangle { // 新增三角形——肯定要改
void draw() {
System.out.println(“三角形”);
}
}
class GraphicDraw {
void drawgraph(int type) {
if (type == 1) {
Rectangle rec = new Rectangle();
rec.draw();
} else if (type == 2) {
Circle c = new Circle();
c.draw();
} else if (type == 3) { // 新增绘制三角形
Triangle t = new Triangle();
t.draw();
}
}
}
public class Main {
public static void main(String[] args) {
GraphicDraw graphicdraw = new GraphicDraw();
graphicdraw.drawgraph(2); // 客户端肯定要改
}
}*/
//符合开放封闭原则,增加了抽象类,便于添加新的类
/*abstract class Shape {
public abstract void draw(); // 抽象方法
}
class GraphicDraw { // 新增绘制图形不需修改此类
void drawgraph(Shape s) {
s.draw();
}
}
class Rectangle extends Shape {
public void draw() {
System.out.println(“矩形”);
}
}
class Circle extends Shape {
public void draw() {
System.out.println(“圆形”);
}
}
class Triangle extends Shape { // 新增三角形
public void draw() {
System.out.println(“三角形”);
}
}
public class Main {
public static void main(String[] args) {
GraphicDraw graphicdraw = new GraphicDraw();
graphicdraw.drawgraph(new Circle());
graphicdraw.drawgraph(new Rectangle());
graphicdraw.drawgraph(new Triangle());
}
}*/
//里氏替换原则,B类重写了父类原有的方法func1,使原有的功能出现错误
/*class A{
public int func1(int num1, int num2){
return num1-num2; }
}
class B extends A{
public int func1(int a,int b){
return a+b;}
public int func2(int a, int b){
return func1(a,b)+9;}
}
public class Main {
public static void main(String[] args) {
A a = new A();
System.out.println("11-3=" +a.func1(11,3));
System.out.println("1-8="+a.func1(1,8));
System.out.println ("-------");
B b = new B();
System.out.println("11-3=" + b.func1(11,3));
//这里本意是求出11-3
System.out.println("1-8="+ b.func1(1,8));
System.out.println("11+3+9="+b.func2(11,3));
}
}/
//里氏替换原则:保证父类方法不被覆盖,可以通过聚合,组合,依赖解决
///创建-个更加基础的基类
class Base {
//把更加基础的方法和成员写到Base类
}
//A类
class A extends Base {
//返回两个数的差
public int func1(int num1, int num2) {
return num1 - num2; }
}
class B extends Base{
A a=new A();
public int func1(int a,int b){
return a+b; }
public int func2(int a, int b){
return func1(a,b)+9; }
public int func3(int a, int b){
return this.a.func1(a,b); }
}
public class Main {
public static void main(String[] args) {
A a = new A();
System.out.println(“11-3=” +a.func1(11,3));
System.out.println(“1-8=”+a.func1(1,8));
System.out.println ("-------");
B b= new B();
//因为B类不再继承A类,因此调用者,不会再func1是求减法
//调用完成的功能就会很明确
System. out.println(“11+3=” + b. func1(11, 3));//这里本意是求出11+3
System. out.println(“1+8=” + b.func1(1, 8));// 1+8
System. out.println(“11+3+9=” + b.func2(11, 3));
//使用组合仍然可以使用到A类相关方法
System. out.println(“11-3=” + b.func3(11, 3));// 这里本意是求出11-3
}
}*/
//依赖倒转原则,依赖抽象或接口
/*public class Main {
public static void main(String[] args) {
Person person = new Person();
person.receive(new Email());
}
}
class Email {
public String getInfo() {
return "电子邮件信息: hello world ";
}
}
// Person 类 接受消息,将Email类 作为参数 产生了依赖 如果 参数发生变化,即接受的是微信 或短信 整个方法需要改动
class Person {
public void receive(Email email) {
System.out.println(email.getInfo());
}
}*/
//增加接口
interface IReciver {
public String getInfo();
}
class Email implements IReciver {
public String getInfo() {
return "电子邮件信息: hello world ";
}
}
class WenXin implements IReciver {
public String getInfo() {
return "微信信息: hello WenXin ";
}
}
/* Person 类 接受消息 将IReciver 接口 作为参数 产生了依赖 */
class Person {
public void receive(IReciver reciver) {
System.out.println(reciver.getInfo());
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person();
person.receive(new Email());
//传入不同实现类 实现不同的接受
person.receive(new WenXin());
}
}