Java自学笔记4
前言
第四次学习笔记,加油加油!
第九章 接口
一、抽象
1、抽象类
2、判断真假
3、常量
4、代码实现
package bianliang;
public class SingleModel {
private static SingleModel pi = null;
public int count = 0;
private SingleModel() {
count++;
}
public static SingleModel getInstance() {
if(null==pi)
pi = new SingleModel();
return pi;
}
}
package bianliang;
public class SingleModelTest {
public static void main(String[] args) {
SingleModel single1 = SingleModel.getInstance();
System.out.println(single1.count);
single1.count++;
SingleModel single2 = SingleModel.getInstance();
System.out.println(single2.count);
System.out.println(single1 == single2);
}
}
package bianliang;
import java.util.Calendar;
public class CalendarDemo {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH)+1;
int day = cal.get(Calendar.DATE);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
int milliSecond = cal.get(Calendar.MILLISECOND);
System.out.println("当前时间:");
String strTime = String.format("%d-%d-%d %d-%d-%d:%s",
year,month,day,hour,minute,second,milliSecond);
System.out.println(strTime);
}
}
二、接口
1、初识接口
2、比较
3、代码实现
package bianliang;
public class IInkBox {
public String getColor();
}
package bianliang;
public class IPaper {
String getSize();
}
package bianliang;
public class Printer {
private IInkBox intbox = null;
private IPaper paper = null;
public void print() {
if(null == inkbox || null == paper) {
System.out.println("墨盒和纸张出现错误,请安装正确后重试!");
return;
}
String color = inkbox.getColor();
String size = paper.getSize();
System.out.println("以下文字是"+color+"颜色的!");
System.out.println("使用纸张:"+type);
System.out.println("打印内容:"+content);
}
public IInkBox getIntbox() {
return intbox;
}
public void setIntbox(IInkBox intbox) {
this.intbox = intbox;
}
public IPaper getPaper() {
return paper;
}
public void setPaper(IPaper paper) {
this.paper = paper;
}
}
package bianliang;
public class PrinterTest {
public static void main(String[] args) {
Printer printer = new Printer();
printer.print("测试内容");
}
}
package bianliang;
import bianliang.IInkBox;
public class ColorInkBoxImpl {
@Override
public String getColor() {
return "红色";
}
}
三、抽象与接口总结