0

I have a private class with 2 variables that I need to use in a for loop in my main function to output. I can't figure out how to use those 2 variables.

this is a for loop in my main function

for(String str : uaCount.keySet())
    {
        String [] arr = str.split("|", 2);
        String s1 = arr[0];
        String s2 = arr[1];


        //what I want to do here is something like :
            // average = keySet.sumTime / keySet.occurences;

        System.out.println(s1 + "--->" + s2 + "Average = " + average + "seconds" );
    }

here's my private function

private class NumberHolder
{
    public int occurences;
    public int sumTime;

}
4

1 に答える 1

0

フィールドの発生と sumTime は公開されているため、 NumberHolder クラスの外部から直接アクセスできますが、静的ではないため、そのクラスのインスタンスが必要になります。

NumberHolder nh = new NumberHolder();
nh.occurances = 20;
nh.sumTime = 30;

//etc...

これらのクラスはプライベートであるため、含まれているクラスからのみアクセスできます。

public class ContainingClass {

     private class OtherClass { //can be seen from ContainingClass but nowhere else
         public int otherField; 
     }
}
于 2013-03-20T14:03:32.627 に答える