第四条,重写toString方法,而不是重载。望采纳
成都创新互联是专业的坡头网站建设公司,坡头接单;提供成都网站制作、做网站、外贸营销网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行坡头网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
public class Time{
//时分秒属性
private int hour;
private int minute;
private int second;
//构造方法
public Time(int hour,int minute,int second){
this.hour = hour;
this.minute = minute;
this.second = second;
resetTime();
}
//增改方法
public void addHour(int hour){
this.hour = this.hour + hour;
}
public void addMinute(int minute){
this.minute = this.minute + minute;
resetTime();
}
public void addSecond(int second){
this.second = this.second + second;
resetTime();
}
//防止数据错误,对各项数据进行重新计算。这里能优化,不是最优方法
public void resetTime(){
int count = hour*60*60+minute*60+second;
hour = count/3600;
count -= hour*3600;
minute = count/60;
second = count%60;
}
//重写toString方法
public String toString(){
return hour+"时 "+minute+"分 "+second+"秒";
}
//测试
public static void main(String[] args){
Time time1 = new Time(2,45,32);
Time time2 = new Time(1,75,-32);
System.out.println(time1.toString());//输出2时 45分 32秒
System.out.println(time2.toString());//输出2时 14分 28秒
}
}
java程序读取系统时间,可以使用Date时间类,使用格式化类simpleDateFormat类,实例如下:
package com.qiu.lin.he;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CeShi {
public static void main(String[] args) {
Date date = new Date();//获取此时的系统时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH点mm分ss秒");
System.out.println("中文现实的时间是" + sdf.format(date));
}
}
运行结果如下:
class MyDate{
public int year,month,day;
public MyDate(MyDate date){
year=date.year;
month=date.month;
day=date.day;
}
public boolean isLeapYear(){
return (((year%4==0)(year%100!=0))||(year%400==0));
}
public MyDate(int y,int m,int d){
year=y;
month=m;
day=d;
if(((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))((day1)||(day31))) System.out.println("wrong date");
if(((month==4)||(month==6)||(month==9)||(month==11))((day1)||(day30))) System.out.println("wrong date");
if(month==2){
if(isLeapYear()((day1)||(day29))) System.out.println("wrong date");
if(!isLeapYear()((day1)||(day28))) System.out.println("wrong date");
}
}
public boolean equal(MyDate date){
return (year==date.year month==date.month day==date.day);
}
public void incrementDay(){
if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)){
int flag=0;
if(day==31){month+=1;flag=1;}
if((day=30)(day=1)){day+=1;}
if(flag==1) day=1;
flag=0;
}
if((month==4)||(month==6)||(month==9)||(month==11)){
int flag=0;
if(day==30){month+=1;flag=1;}
if((day=29)(day=1)){day+=1;}
if(flag==1) day=1;
flag=0;
}
if(month==12){
int flag=0;
if(day==31){year+=1;month=1;flag=1;}
if((day=1)(day=30)){day+=1;}
if(flag==1) day=1;
flag=0;
}
if(month==2){
int flag=0;
if(isLeapYear()){
if(day==29){month+=1;flag=1;}
if((day=28)(day=1)){day+=1;}
if(flag==1) day=1;
flag=0;
}
else{
if(day==28){month+=1;flag=1;}
if((day=27)(day=1)){day+=1;}
if(flag==1) day=1;
flag=0;
}
}
}
public int getYear(){
return year;
}
public int getMonth(){
return month;
}
public int getDay(){
return day;
}
public String toString(){
return (year+"-"+month+"-"+day);
}
}
public class Mytest{
public static void main(String[] test){
MyDate date1=new MyDate(2000,2,28);
MyDate date2=new MyDate(2006,12,31);
MyDate date3=new MyDate(date2);
System.out.println(date3.equal(date2));
date1.incrementDay();
System.out.println(date1.getYear()+" "+date1.getMonth()+" "+date1.getDay());
System.out.println(date1.toString());
date2.incrementDay();
System.out.println(date2.getYear()+" "+date2.getMonth()+" "+date2.getDay());
System.out.println(date2.toString());
}
}
售后响应及时
7×24小时客服热线数据备份
更安全、更高效、更稳定价格公道精准
项目经理精准报价不弄虚作假合作无风险
重合同讲信誉,无效全额退款