對(duì)象的識(shí)別
實(shí)例代碼:時(shí)鐘
clock class
package com.company;
public class clock {
private display hour = new display(24);
private display minute = new display(60);
public void start() {
while (ture) {
minute.increase();
if (minute.getValue() == 0) {
hour.increase();
}
//輸出這個(gè)時(shí)鐘的時(shí)間
System.out.printf("%02d:%02d\n", hour.getValue(), minute.getValue());
//printf表示輸出一個(gè)帶有格式的輸出
}
}
public static void main(String[] args) {
// write your code here
clock clo = new clock();
clo.start();
}
}
display class
package com.company;
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;
}
}
訪問屬性
封閉的訪問屬性
private
- 只有這個(gè)類內(nèi)部才可以訪問
- 類內(nèi)部指類的成員函數(shù)和定義初始化
- 這個(gè)限制是對(duì)類而不是對(duì)對(duì)象
包
package
類變量
public static int step = 2;
類變量只存在于類中汇跨,在單獨(dú)的對(duì)象中不存在可以改變的類變量
類函數(shù)
public stati int main() {
}