黑马程序员---java学习笔记之交通灯

1、面向对象设计的一个重要经验是,谁拥有数据,谁就提供操作这些数据的方法

2、小球沿绳子移动,采用面向对象的分析

class Rope
{
	private int startPoint;
	private int endPoint;
	private int currentPoint;
	
	public int nextPoint()
	{
		return currentPoint + 1;
	}
	
}


class Ball
{
	private Rope rope;
	private int currentPoint;

	public Ball(Rope rope, int currentPoint) {
		super();
		this.rope = rope;
		this.currentPoint = currentPoint;
	}
	
	public void Move()
	{
		currentPoint = rope.nextPoint();
		System.out.println("小球移动到了"+currentPoint);
	}
}

3、枚举灯的对象:

public enum Lamp {
	S2N("N2S","S2W",false),S2W("N2E","E2W",false),E2W("W2E","E2S",false),E2S("W2N","S2N",false),
	
	N2S(null,null,false),N2E(null,null,false),W2E(null,null,false),W2N(null,null,false),
	
	S2E(null,null,true),E2N(null,null,true),N2W(null,null,true),W2S(null,null,true);
	
	private Lamp(String opposite,String next,boolean lighted){
		this.opposite = opposite;
		this.next = next;
		this.lighted = lighted;
	}


	private boolean lighted;
	private String opposite;
	private String next;
	public boolean isLighted(){
		return lighted;
	}
		
	public void light(){
		this.lighted = true;
		if(opposite != null){
			Lamp.valueOf(opposite).light();
		}
		System.out.println(name() + " lamp is green);
		
	}
	
	public Lamp blackOut(){
		this.lighted = false;
		if(opposite != null){
			Lamp.valueOf(opposite).blackOut();
		}		
		
		Lamp nextLamp= null;
		if(next != null){
			nextLamp = Lamp.valueOf(next);
			System.out.println(name() + next);			
			nextLamp.light();
		}
		return nextLamp;
	}
}

-----------------------------------------------------------------------

以下为复习后笔记:

-----------------------------------------------------------------------

1、面向对象设计的一个重要经验:谁拥有数据,谁就提供对这些数据的操作方法。










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值