封装类-时钟

 1 package com.hanqi.z1p1;
 2 
 3 public class TestClass {
 4 
 5     public static void main(String[] args) {
 6         // TODO 自动生成的方法存根
 7 
 8         Clock cl1=new Clock(12,45,26);
 9         cl1.show();
10         
11         Clock cl2=new Clock(3,23,06);
12         cl2.show();
13         
14         
15     }
16 
17 }
 1 package com.hanqi.z1p1;
 2 
 3 public class Clock {
 4 
 5     int Hour;
 6     int Minute;
 7     int Second;
 8     
 9     Clock(int h,int m,int s)
10     {
11         Hour=h;
12         Minute=m;
13         Second=s;
14     }
15     
16     void show()
17     {
18

        if(Hour>=0&&Hour<=24&&Minute>=0&&Minute<=60&&Second>=0&&Second<=60)
        {
               System.out.println("当前的时间是:"+Hour+"点"+Minute+"分"+Second+"秒");
        }else
         {
         System.out.println("时间输入有误!");
         }

19     }
20     
21     
22     
23 }

运行结果:

原文地址:https://www.cnblogs.com/miss123/p/5500210.html