首页 > 解决方案 > 我怎样才能使用这个变量?

问题描述

我想在主类中使用这个变量(ntime [])我想我必须使用'extends',但我不知道我是如何使用它的。我可以使用什么变量,以及如何使用它?我想知道它正在使用扩展,或者它可以使用其他变量。谢谢

我正在使用 extends ~~ void main(String args[]) extends * throws IOException & ~~ void main extends * (String args[]) *** throws IOException

但这行不通

导入 java.util.Calendar;

class *****
 {
 public void ***** throws IOException
 {
 int [] ntime = new int [6];
 BufferedReader inbr = new BufferedReader (new
 InputStreamReader(System.in));
 Calendar now= Calendar.getInstance();
 ntime[0] = now.get(Calendar.YEAR);
 ntime[1] = now.get(Calendar.MONTH);
....
 ntime[5] = now.get(Calendar.SECOND);
 }
 }


 class *****  {
 public static void main(String args[]) throws IOException
 {

***** objecta = new ***();
   objecta.test01();
  System.out.println("Time is  " + ntime[0] + ... + ntime[5]);
 }
 }

符号:变量 ntime 位置:类 ***

标签: java

解决方案


由于 ntime 是 *** 类的成员。

为了能够访问它,您必须首先创建该类的实例并确保 ntime 不是私有成员。我认为您在创建“objecta”时所做的。然后您将不得不通过该对象访问您的变量 ntime 。

例如你会打电话

objecta.ntime[0]

推荐阅读