Java学习的第五十三天

1.例9.5引用静态数据成员

public class Cjava {
public static void main(String[]args) {
Box b[] = {new Box(12,15),new Box(18,20)};
System.out.println("h of b[0]="+b[0].h+"The volume of b[0]="+b[0].volume());
System.out.println("h of b[1]="+b[1].h+"The volume of b[1]="+b[1].volume());
    }
}
class Box{
    static int h=5 ;int w,l;
    Box(int y,int z){
        w=y;l=z;
    }
    int volume(){
        return (h*w*l);
    }
}

例9.6引用静态方法

public class Cjava {
public static void main(String[]args) {
Student s[]= {new Student(1001,18,70),new Student(1002,19,78),new Student(1005,20,98)};
for(int i=0;i<s.length;i++) {
    s[i].total();
}
System.out.println("平均分为:"+Student.average());
    }
}
class Student{
    int num;int age;float score;static float sum;static int count;
    Student(int n,int a,float s){
        num=n;age=a;score=s;
    }
    void total(){
        sum=sum+score;
        count++;
    }
    static float average() {
        return (sum/count);
    }
}

 2.没问题

3.明天继续写例题

原文地址:https://www.cnblogs.com/feng747/p/13576980.html