今天因为打美赛的原因,全天都比较忙,只能抽出晚上的一点点时间随便听一听课。(美赛第一天进度还算客观)
包(package)
当要用到类的时候,需要给出全名(即package.class)
举个例子 :
其中包clock中包含Clock类,包display中包含display类
1.在package中的class开头必须写
package name;
2.从一个class中调用另外一个包里的class,需要写出所调用的class的全部名字(eg: package.class)
代码1 :
package clock;import display.display;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){Clock clock = new Clock();clock.start();}
}
代码2:
package clock;//import display.display;public class Clock {
// 这里有变化private display.display hour = new display.display(24);private display.display minute = new display.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){Clock clock = new Clock();clock.start();}
}
代码3 :(用improt package.*将包中的所有类引用过来)
package clock;import display.*;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){Clock clock = new Clock();clock.start();}
}
包的名字里可以有.出现
eg:
package clock;import display.led.display;