日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java学习:类的封装、继承和多态

發布時間:2023/12/2 java 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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?",團員":"");
}

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(); }

}

總結

以上是生活随笔為你收集整理的Java学习:类的封装、继承和多态的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。