From 33e853ac336294c70580e6a316e5cbcceb76075c Mon Sep 17 00:00:00 2001 From: "1923365156@qq.com" <$$@@zhaofan666> Date: Mon, 28 Aug 2023 23:10:04 +0800 Subject: [PATCH] =?UTF-8?q?=E7=8A=B6=E6=80=81=E6=A8=A1=E5=BC=8F=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=5F=E5=BA=94=E7=94=A8=E5=AE=9E=E4=BE=8B=5F?= =?UTF-8?q?=E4=BA=A4=E9=80=9A=E7=81=AF=E7=9A=84=E7=8A=B6=E6=80=81=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=5F=E4=BC=A0=E7=BB=9F=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/compiler.xml | 6 +-- .idea/misc.xml | 1 - .../boyunv/state/example02/TrafficLight.java | 45 +++++++++++++++++++ 3 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 byv-state-patttern-16/src/main/java/com/boyunv/state/example02/TrafficLight.java diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 1b749e0..518fae0 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -9,20 +9,20 @@ + - - + - + diff --git a/.idea/misc.xml b/.idea/misc.xml index 965b7c9..189ff21 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/byv-state-patttern-16/src/main/java/com/boyunv/state/example02/TrafficLight.java b/byv-state-patttern-16/src/main/java/com/boyunv/state/example02/TrafficLight.java new file mode 100644 index 0000000..5673faa --- /dev/null +++ b/byv-state-patttern-16/src/main/java/com/boyunv/state/example02/TrafficLight.java @@ -0,0 +1,45 @@ +package com.boyunv.state.example02; +/* + *@description + * 交通灯类 红灯 禁 黄灯 警示 绿灯 通行 + *@author boyunv + *@create 2023/8/28 22:58 + *@version 1.0 + */ + +public class TrafficLight { + //初始化状态 + private String state=" 红色"; + + //切换为绿灯,通行状态 + public void switchToGreen(){ + if ("绿".equals(state)){ + System.out.println("当前为绿灯没不需要切换"); + } else if ("红".equals(state)) { + System.out.println("红灯不能切换为绿灯"); + } else if ("黄".equals(state)) { + state="绿"; + System.out.println("绿灯亮起========> 时长"); + } + } + //切换为黄灯,通行状态 + public void switchToYellow(){ + if ("黄".equals(state)){ + System.out.println("当前为黄灯没不需要切换"); + } else if ("绿".equals(state)||"红".equals(state)) { + state="黄"; + System.out.println("黄灯亮起========> 时长10秒"); + } + } + //切换为红灯,禁止状态 + public void switchToRed (){ + if ("红".equals(state)){ + System.out.println("当前为红灯没不需要切换"); + } else if ("绿".equals(state)) { + System.out.println("绿灯不能切换为红灯"); + } else if ("黄".equals(state)) { + state="红"; + System.out.println("红灯亮起========> 时长"); + } + } +} -- Gitee