求教:有关javajava封装继承多态例子与多态的问题 定义一组交通工具类,包括汽车类(Auto

java之 - 类的打包、继承和多态(一) - 编程当前位置:& &&&java之 - 类的打包、继承和多态(一)java之 - 类的打包、继承和多态(一)&&网友分享于:&&浏览:0次java之 ------ 类的封装、继承和多态(一)问题:①&完善方法,保证获得正确的日期值合法性;
②&将和方法合并为以下方法,并增加一些方法:
public&int&getWeek()&&&//返回当前日期对应的星期几,范围为~
public&String&toWeekString()&&&//返回当前日期对应星期几的中文字符串
public&boolean&before(MyDate&d)&&//判断是否在指定日期之前
public&int&daysBetween(MyDate&d)&//返回当前日期与日期之间相距的天数
public&boolean&equals(Object&obj)&&//比较两个对象是否相等
import java.util.C
import java.util.S
public class MyDate2 {
int year,month,
boolean isCorrect=
private static int thisY
thisYear=Calendar.getInstance().get(Calendar.YEAR); //获得当前日期对象中的年份值
public MyDate2(int year, int month, int day) {
this.set(year, month, day);
public MyDate2()
//调用本类已声明的其他构造方法
public MyDate2(MyDate2 d)
this.set(d);
public void set(int y, int m, int d) {
System.out.println(&Input Year Is Error !!!&);
this.isCorrect=
}if(m&1||m&12){
System.out.println(&Input Month Is Error !!!&);
this.isCorrect=
if(day&=0 || day&daysOfMonth() ){
System.out.println(&Input Day Is Error !!!&);
this.isCorrect=
this.year =
this.month =
this.day =
public void set(MyDate2 d) {
set(d.year, d.month, d.day);
//调用同名成员方法,不能使用this()
public String toString(){
return this.year+&年&+this.month+&月&+this.day+&日&;
public static int getThisYear(){
return thisY
//访问静态成员变量
public static boolean isLeapYear(int year)
return year%400==0 || year%100!=0 && year%4==0;
public boolean isLeapYear() {
return isLeapYear(this.year);
//调用静态方法
public boolean equals(MyDate2 d)
//this指代调用本
方法的当前对象
return this==d || d!=null && this.year==d.year && this.month==d.month && this.day==d.
public boolean equals(Object obj)
if (this==obj)
//this指代调用当前方法的对象
if (obj instanceof MyDate2) {
MyDate2 d = (MyDate2)
//类型强制转换
return this.year==d.year && this.month==d.month && this.day==d.
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;
return isLeapYear(year)?29:28;
default: return 0;
public int daysOfMonth()
return daysOfMonth(this.year, this.month);
public void tomorrow() {
this.day++;
//通过this改变当前对象的值,没有返回值
if (day&this.daysOfMonth()){
if (month&12)
public MyDate2 yestoday()
MyDate2 yes=new MyDate2(this);
//执行拷贝构造方法,创建新实例,没有改变当前对象
yes.day--;
if (yes.day==0) {
yes.month--;
if (yes.month==0){
yes.month=12;
yes.year--;
yes.day = daysOfMonth(yes.year, yes.month);
//返回对象引用
public MyDate2 dayAfter(int n)
MyDate2 yes=new MyDate2(this);
//执行拷贝构造方法,创建新实例,没有改变当前对象
yes.day+=n;
if (yes.day==0) {
yes.month--;
if (yes.month==0){
yes.month=12;
yes.year--;
yes.day = daysOfMonth(yes.year, yes.month);
//System.out.println(&**&+yes.year+& &+yes.month+& &+yes.day);
if (yes.day&this.daysOfMonth()){
yes.day=1;
yes.month++;
if (yes.month&12)
yes.month=1;
yes.year++;
//返回对象引用
public int getSumDays(MyDate2 d){
for(int i=1;i&d.i++){
sum+=d.isLeapYear(i) ? 366 : 365;
for(int i=1;i&d.i++){
sum+=d.daysOfMonth(d.year,i);
public int getWeek(){
long sum=0;
for(int i=1;i&this.i++){
sum+=this.isLeapYear(i) ? 366 : 365;
for(int i=1;i&this.i++){
sum+=this.daysOfMonth(this.year,i);
sum+=this.
week=(int)sum%7;
public String toWeekString(){
switch (getWeek()){
case 0 : return &Sunday& ;
case 1 : return &Monday& ;
case 2 : return &Tuesay& ;
case 3 : return &Wednesday& ;
case 4 : return &Thursday& ;
case 5 : return &Friday& ;
default : return &Saturday& ;
public boolean before(MyDate2 d){
if(this.year&d.year){
}else if(this.year==d.year){
if(this.month&d.month){
}else if(this.month==d.month){
if(this.day&d.day){
public int daysBetween(MyDate2 d){
int a=getSumDays(d);
int b=getSumDays(this);
return Math.abs(a-b);
class MyDate_ex
public static void main(String args[])
Scanner sc=new Scanner(System.in);
System.out.println(&Please input date : &);
while(sc.hasNext()){
MyDate2 d= new MyDate2(sc.nextInt(),sc.nextInt(),sc.nextInt());
if(d.isCorrect){
System.out.println(&If you want to konw yestoday date,please input -1 &);
System.out.println(&or tomorrow date,please input 1&);
System.out.println(d.dayAfter(sc.nextInt()));
System.out.println(&Week : &+d.getWeek()+& &+d.toWeekString());
System.out.println(&Input a cmparative date : &);
MyDate2 r=new MyDate2(sc.nextInt(),sc.nextInt(),sc.nextInt());
System.out.println(d.before(r));
System.out.println(&Date_d between Date_r have &+d.daysBetween(r)+& days&);
System.out.println();
System.out.println(&Please input date : &);
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 1234567891011 Copyright & &&版权所有(仅供参考)java期末考试样卷答案_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
(仅供参考)java期末考试样卷答案
上传于|0|0|暂无简介
阅读已结束,如果下载本文需要使用0下载券
想免费下载更多文档?
定制HR最喜欢的简历
下载文档到电脑,查找使用更方便
还剩12页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢第七章继承多态练习题_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
第七章继承多态练习题
上传于|0|0|文档简介
&&第七章继承多态练习题
阅读已结束,如果下载本文需要使用0下载券
想免费下载更多文档?
定制HR最喜欢的简历
下载文档到电脑,查找使用更方便
还剩7页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢}

我要回帖

更多关于 java封装继承多态例子 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信