//GetTime b = new GetTime();
//Single2 a=new Single2();
}
}
Person.java:
public class Person {
//字段(成员变量)
private int ageOf;//年龄
private String name;//姓名
private String address;//住址
private Pet pet;//组合关系
//构造方法(构造器)
public Person(){
}
public int getAge(){//取变量的值
return ageOf;
}
public void setAge(int a){//给变量赋值
if(a<0||a>150){
System.out.println("Error Age!");
System.exit(-1);
}
this.ageOf=a;
}
public void setAddress(String address) {
this.address = address;
}
public String getAddress() {
return address;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
//方法:
public String eat(String address,double price,String tools){
System.out.println("eating... at "+address+" with "+tools+", price is "+price);
return "good";
}
public void run(){
System.out.println("runing...");
}
public Car buyCar(Car car){//依赖
return car;
}
}
class Car
{
}
class Pet
{
}
Phone.java:
public class Phone {
//成员变量
private String brand;
private int money;
private int size;
//封装
public void setBrand(String brand) {
this.brand = brand;
}
public String getBrand() {
return brand;
}
public void setSize(int size) {
this.size = size;
}
public int getSize() {
return size;
}
public void setMoney(int money) {
this.money = money;
}
public int getMoney() {
return money;
}
//构造方法
public Phone(String x,int a,int b){
setBrand(x);
setMoney(a);
setSize(b);
}
//方法
public void Open(){
System.out.println("Open");
}
public void Connect(){
System.out.println("Connect to Internet");
}
public String Tellmebrand(String brand){
System.out.println("Your Phone brand is "+brand);
return "htc";
}
}
Single.java://饿汉式
class Single {
private Single () {}
private static Single s = new Single();
public static Single getInstance (){
System.out.println("Single1");
return s;
}
}
Single2.java://懒汉式,延迟加载方式
public class Single2 {
private Single2 () {}
private static Single2 s = null;
public static Single2 getInstance (){
if(s == null) s = new Single2();
System.out.println("Single2");
return s;
}
}
GetTime.java://抽象类模板
abstract class GetTime {
public final void getTime(){
long start = System.currentTimeMillis();
code();//抽象实现不确定的部分
long end = System.currentTimeMillis();
System.out.println("毫米是"+(end-start));
}
public abstract void code();//子类复写抽象
}
class SubDemo extends GetTime{
public void code(){
for(int y=0;y<1000;y++){
System.out.println(y);
}
}
}