创建时间time类,定义构造函数

package 七周;
import javax.swing.JOptionPane;
public class time2 {
    private int Hour;
    private int Minute;
    private int Second;
    public time2(int myhour,int myminute,int mysecond) {
        this.Hour = myhour  ;
        this.Minute = myminute ;
        this.Second = mysecond ;
    }
    private void sethour(int newhour) {
        if(newhour >= 0 && newhour <= 24) {
        this.Hour = newhour;
        }
        else
            this.Hour =0;
    }
    private int gethour() {
        return this.Hour;
    }
    private void setminute(int newminute) {
        if(this.Minute >= 0 && this.Minute <= 60) {
        this.Minute = newminute;
        }
        else
            this.Minute = 0;
    }
    private int getminute() {
        return Minute;
    }
    private void setsecond(int newsecond) {
        if(newsecond >= 0 && newsecond <= 60) {
        this.Second = newsecond;
        }
        else
            this.Second = 0;
    }
    private int getsecond() {
        return Second;
    }
    private String print() {
        return this.Hour+":"+this.Minute+":"+this.Second;
    }
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        String output;
        time2 t = new time2(40,39,49);
        output = t.print();
        JOptionPane.showMessageDialog(null, output);
    }
}
 

time2 t = new time2();t.set second(50);这样

创建时间time类并定义一构造函数,它含有3个整型参数myhour,myminute,mysecond,并用它们设置时间,属性Hour、Minute、Second的定义包括数据检查,并将时间完整显示于消息框

你方法要设置为public,你仅仅是通过构造方法初始化数据并没有对数据进行处理,不符合的肯定可以输出,你可以考虑提供空构造,调用对应的set方法来设置对应的数据