集册 Java实例教程 无法访问类Time1的私有成员。

无法访问类Time1的私有成员。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

469
无法访问类Time1的私有成员。

public class Main 

{/** 来自 nowjava - 时代Java**/

   public static void main(String[] args)

   {

      Time1 time = new Time1(); // create and initialize Time1 object


      time.hour = 7; // error: hour has private access in Time1

      time.minute = 15; // error: minute has private access in Time1

      time.second = 30; // error: second has private access in Time1

   } 

}

class Time1  

{

   private int hour; // 0 - 23

   private int minute; // 0 - 59

   private int second; // 0 - 59
   /**来自 
    N  o w  J a v a . c o m**/


   // set a new time value using universal time; throw an 

   // exception if the hour, minute or second is invalid

   public void setTime(int h, int m, int s)

   {

      // validate hour, minute and second

      if ((h >= 0 && h < 24) && (m >= 0 && m < 60) && 

         (s >= 0 && s < 60)) 

      {

         hour = h;

         minute = m;

         second = s;

      } 

      else

         throw new IllegalArgumentException(

            "hour, minute and/or second was out of range");

   } 


   // convert to String in universal-time format (HH:MM:SS)

   public String toUniversalString()

   {

      return String.format("%02d:%02d:%02d", hour, minute, second);

   } 


   
展开阅读全文