Java学习:类的封装、继承和多态
【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?",團員":"");
}
}
【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()方法運行時多態
}
}
總結
以上是生活随笔為你收集整理的Java学习:类的封装、继承和多态的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: wpsexcl设置筛选(使用WPS的EX
- 下一篇: Java语言学习概述