【3.1】声明日期类及使用日期对象
public class MyDate{ //类声明
int year,month,day; //成员变量,表示年月日
void set(int y,int m,int d) //成员方法、设计日期值
{
year=y;
month=m;
day=d;
}
void set(MyDate d) //将当前对象值设置为参数值,重载
{
set(d.year,d.month,d.day); //调用重载的同名成员方法
}
public String toString()
{
return year+“年”+month+“月”+day+“日”;
}
public static void main(String args[])
{
MyDate d1 = new MyDate(); //声明对象、创建实例、引用赋值
d1.set(2012,1,1); //调用类的成员方法
MyDate d2=d1; //对象引用赋值
System.out.println("d1: “+d1.toString()+”,d2: "+d2.toString());
d2.month=10; //修改示例成员变量值
System.out.println("d1: “+d1+”,d2: "+d2); //输出对象字符串描述,默认调用d1.toString()
d2=new MyDate(); //创建另一个示例
d2.set(d1);
System.out.println("d1: “+d1+”,d2: "+d2);
}
}
【3.2】封装的日期类
public class MyDate{ //共有的类,与源程序文件同名
private int year,month,day; //私有成员变量
private static int thisYear; //当前年份,私有静态成员变量
static
{
thisYear = 2012; //静态成员变量初始化
}
public MyDate(int year,int month,int day) //构造方法、指定日期
{
this.set(year,month,day); //调用本类的成员方法
}
public MyDate() //无参数构造方法,制定默认日期,重载
{
this(1970,1,1); //调用本类已声明的其他构造方法
}
public MyDate(MyDate d) //拷贝构造方法,日期同参数,重载
{
this.set(d);
}
public void set(int year,int month,int day) //设置日期值,算法不全
{
this.year=year; //this.year值当前对象的成员变量,year值参数
this.month=(month>=1&&month<=12)?month:1;
this.day=(day>=1&&day<=31)?day:1; //this引用不能省略
}
public void set(MyDate d) //设置日期值,重载
{
set(d.year,d.month,d.day); //调用同名成员方法,不能使用this()
}
public int getYear() //获得年份
{
return this.year;
}
public int getMonth() {return this.month;} //获得月份
public int getDay() {return this.day;} //获得当月日期
public String toString() //返回中文日期字符串,月日占两位
{
return year+“年”+String.format("%02d",month)+“月”+String.format("%02d",day)+“日”;
}
public static int getThisYear() //获得今年年份,静态方法
{
return thisYear;
}
public static boolean isLeapYear(int year) //判断指定年份是否闰年,静态方法
{
return year%4000||year%100!=0&&year%40;
}
public boolean isLeapYear() //判断当前日期的年份是否闰年,重载
{
return isLeapYear(this.year);
}
public boolean equals(MyDate d) //比较当前日期值与d是否相等
{
return thisd||d!=null&&this.yeard.year&&this.monthd.month&&this.dayd.day;
}
public static int daysOfMonth(int year,int month) //返回指定年月的天数,静态方法
{
switch(month) //计算每月的天数
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12:return 31;
case 4:case 6:case 9:case 11:return 30;
case 2:return MyDate.isLeapYear(year)?29:28;
default:return 0;
}
}
public int daysOfMonth() //返回当月天数
{
return daysOfMonth(this.year,this.month);
}
public void tomorrow() //当前日期改为之后一天日期
{
this.day++; //改变this引用的实例值。没有返回值
if(this.day>this.daysOfMonth())
{
this.day=1;
this.month++;
if(this.month>12)
{
this.month=1;
this.year++;
}
}
}
public MyDate yesterday() //当前日期改为之前一天日期
{
MyDate date= new MyDate(this); //执行拷贝构造方法,创建实例,没有改变this
date.day–;
if(date.day0)
{
date.month–;
if(date.month0)
{
date.month=12;
date.year–;
}
date.day=daysOfMonth(date.year,date.month);
}
return date;
}
}
class MyDate_ex {
public static void main(String args[])
{
System.out.println(“今年是”+MyDate.getThisYear()+",闰年?"+MyDate.isLeapYear(MyDate.getThisYear()));
MyDate d1=new MyDate(2012,12,31);
MyDate d2=new MyDate(d1); //调用拷贝构造方法复制实例
System.out.println("d1: “+d1+”,d2: “+d2+“d1d2?"+(d1d2)+”,d1.equals(d2)?”+d1.equals(d2));
System.out.print(d1+“的明天是 “);
d1.tomorrow();
System.out.println(d1+”\n”+d1+"的昨天是 "+(d2=d1.yesterday()));
}
}
【3.3】Person类,使用对象作为成员变量并实现深拷贝
public class Person {
public String name; //姓名,实例成员变量,保护成员
public MyDate birthday; //生日
public String sex,province,city; //姓名,所在省份,城市
private static int count=0; //静态成员变量,本类及子类实例计数
public Person(String name,MyDate birthday,String sex,String province,String city) //构造方法
{
this.set(name,birthday,sex,province,city); //调用本类声明的成员方法
count++;
}
public Person(String name,MyDate birthday) //构造方法,重载
{
this(name, birthday,"","",""); //调用本类已声明的构造方法
}
public Person() {this("",null); } //构造方法,重载
public Person(Person p) //拷贝构造方法,重载,复制对象
{
this(p.name,new MyDate(p.birthday),p.sex,p.province,p.city); //深拷贝,创建日期实例
}
public void finalize() //析构方法
{
System.out.println(“释放对象(”+this.toString()+")");
Person.count–;
}
//显示对象数,静态成员方法;只能访问静态成员变量,不能访问实例成员,也不能使用this
public static void howMany() { System.out.print(Person.count+“个Person对象,”);}
public void set(String name,MyDate birthday,String sex,String province,String city) //设置属性值
{
this.name=namenull?"":name;
this.birthday=birthday; //引用赋值
this.sex=sexnull?"":sex;
this.province=provincenull?"":province;
this.city=citynull?"":city;
}
//对象的字符串描述,成员变量之间以逗号分隔,若字符串成员变量为null,输出“null”
public String toString()
{
return name+","+(birthday.toString())+","+sex+","+province+","+city;
}
public static void main(String args[]) //main方法也是静态成员方法
{
Person p1=new Person(“李小明”,new MyDate(1997,11,25));
Person p2=new Person(p1); //拷贝构造方法
Person.howMany(); //通过类名调用类成员方法
System.out.println(“p1: “+p1+”; p2: “+p2+”\np1p2? "+(p1p2)+”; p1.namep2.name? "
+(p1.namep2.name)+",p1.birthdayp2.birthday? "+(p1.birthdayp2.birthday));
//显示引用关系
//以下修改p2的姓名和生日
p2.name=“张”+p2.name.substring(1); //改名,一个汉字长度为一个字符
MyDate d=p2.birthday; //获得日期,传递日期对象引用
d.set(d.getYear()+2,d.getMonth(),d.getDay()); //改变日期,设置p2年龄小两岁
System.out.println("p1: “+p1+”; p2: "+p2);
p1.finalize(); //调用析构方法,释放对象
Person.howMany(); //通过类名调用静态成员方法
}
}
【3.4】Student继承Person类
public class Student extends Person { //STudent类继承Person类
private String speciality; //专业,子类增加的成员变量
public static void main(String args[])
{
Person p1=new Person(“李小明”,new MyDate(1992,3,15));
Student s1=new Student(); //默认构造方法,执行父类构造方法Person()
Student.howMany(); //继承父类静态方法,执行Person.howMany
System.out.println(“p1: “+p1.toString()+”;s1: “+s1.toString());
s1.set(“王江”,new MyDate(1987,2,27),””,"",""); //s1调用父类的成员方法
s1.finalize();
Student.howMany();
}
}
【3.5】Student类重定义父类成员
public class Student extends Person { //STudent类继承Person类
private String department,speciality,number; //系,专业,学号,子类增加的成员变量
public boolean member; //团员
private static int count=0; //Student类对象计数
public Student(String name,MyDate birthday,String sex,String province,String city,
String department,String speciality,String number,boolean member) //构造方法
{
super(name,birthday,sex,province,city); //调用父类同参数的构造方法,Person.count++
this.set(department,speciality,number,member);
count++;
}
public Student()
{
this("",new MyDate(),"","","","","","",false);
}
//构造方法,由父类Person实例提供初值,深拷贝p,且父类name等成员变量可见
public Student(Person p,String department,String speciality,String number,boolean member)
{
this(p.name,new MyDate(p.birthday),p.sex,p.province,p.city,department,speciality,number,member);
}
public Student(Student s) //拷贝构造方法,深拷贝
{
this(s.name,new MyDate(s.birthday),s.sex,s.province,s.city,s.department,s.speciality,s.number,s.member);
}
public void finalize() //析构方法,覆盖父类的析构方法
{
super.finalize(); //调用父类析构方法,Person.count–
Student.count–;
}
//显示父类和子类的对象数,覆盖父类同名静态成员方法,不能使用super
public static void howMany()
{
Person.howMany();
System.out.println(Student.count+“个Student对象”);
}
//设置各属性值;重载父类同名成员方法,参数列表不同
public void set(String department,String speciality,String number,boolean member)
{
this.department=departmentnull?"":department;
this.speciality=specialitynull?"":speciality;
this.number=number==null?"":number;
this.member=member;
}
//对象的字符串描述,成员变量之间以逗号分隔,若字符串成员变量为null,则输出“null”
//覆盖父类的toString()方法
public String toString()
{
return super.toString()+","+department+","+speciality+","+number+(member?",团员":"");
}
public static void main(String args[])
{Person p1=new Person("李小明",new MyDate(1992,3,15),"男","湖南省","长沙市");Student s1=new Student(p1,"计算机系","计算机科学与技术专业","001",true);Student s2=new Student(s1); //拷贝构造方法s2.set("张小莉",new MyDate(1992,4,3),"女","湖北省","武汉市"); //调用父类的成员方法s2.set("经济管理系", "信息管理专业", "003", true); //调用子类重载的成员方法Student.howMany(); //继承父类静态方法,执行Person.howManySystem.out.println("p1: "+p1.toString()+"\ns1: "+s1.toString()+"\ns2: "+s2);s2.finalize();Student.howMany();}
}
【3.6】对象数组的输出、查找算法和合并(有错,课本P88)
public class ObjectArray
{
public static void print(Object value[]) //输出对象数组
{
if(value!=null)
for(int i=0;i<value.length;i++)
if(value[i]!=null)
System.out.println(value[i].toString()); //toString()方法运行时多态
}
public static Object[] concat(Object value1[],Object value2[],Object value3[]) //返回合并的对象数组
{if(value1==null&&value3==null)return value2;if(value2==null&&value3==null)return value1;Object temp[]=new Object[value1.length+value2.length+value3.length];int i=0,j=0;for(j=0;j<value1.length;j++)temp[i++]=value1[j]; //对象引用赋值for(j=0;j<value2.length;j++)temp[i++]=value2[j];for(j=0;j<value3.length;j++)temp[i++]=value3[j];return temp;
}
//输出value对象数组中所有与key相等的元素,顺序查找算法;equals()方法应用
public static void print(Object value[],Object key)
{if(value!=null&&key!=null)for(int i=0;i<value.length;i++){System.out.println(key.equals(value[i]));System.out.println("value[i]: "+value[i]);if(value[i]!=null&&key.equals(value[i])) //equals()运行时多态,执行key比较规则System.out.println(value[i].toString()); //toString()方法运行时多态}}
public static void main(String args[])
{Person p=new Person("李小明",new MyDate(1992,3,15),"男","湖南","长沙");Person pvalue[]= {p,new Student(p,"计算机","计算机科学与技术","001",true)};Student svalue1[]= {new Student("张莉",new MyDate(1992,4,3),"女","湖北","武汉","经济管理","信息管理","031",true)};Student svalue2[]= {new Student("朱小红",new MyDate(1991,3,12),"女","广东","广州","通信","通信工程","014",true)};Object value[]=concat(pvalue,svalue1,svalue2); //返回合并的对象数组//svalue1[1].name=svalue1[1].name.substring(0,2); //影响value数组元素,同时改名//svalue2[1].name=svalue2[1].name.substring(0,2); //影响value数组元素,同时改名print(value);Person key = new Person(p); //深拷贝System.out.println("查找: "+key.toString()+", 结果: ");print(value,key); //查找执行Person对象比较规则key=new Student((Student)pvalue[1]); //key引用子类实例System.out.println("查找: "+key.toString()+", 结果: ");print(value,key); //查找执行student对象比较规则Student.howMany();
}
}