第二周 对象交互

2.1 对象交互


2.1.1 对象的识别

数字钟

public class Display {
	private int value = 0;
	private int limit = 0;
	
	public Display(int limit) {
		this.limit = limit;
	}
	
	public void increase() {
		value ++;
		if ( value == limit ) {
			value = 0;
		}
	}
	
	public int getValue() {
		return value;
	}
	public static void main(String[] args) {
		Display d = new Display(24);
		for (;;) {
			d.increase();
			System.out.println(d.getValue());
		}
	}

}

2.1.2 对象交互

public class Clock {
	private Display hour = new Display(24);
	private Display minute = new Display(60);
	
	public void start() {
		while ( true ) {
			minute.increase();
			if ( minute.getValue() == 0 ) {
				hour.increase();
			}
			System.out.printf("%02d:%02d\n",hour.getValue(),minute.getValue());
		}
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Clock clock = new Clock();
		clock.start();
	}

}

2.2 访问属性


2.2.1 封闭的访问属性

private

  • 只有这个类内部可以访问
  • 类内部指类的成员函数和定义初始化
  • 这个限制是对类的而不是对对象的

2.2.2 开放的访问属性

public

  • 任何人都可以访问
  • 任何人指的是在任何类的函数或定义初始化中可以使用
  • 使用指的是调用、访问或定义变量

2.3 包


2.3.1 包


2.4 类变量


2.4.1 类变量

在这里插入图片描述


2.4.2 类函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值