1. 程式人生 > 實用技巧 >Java學習的第五十三天

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.明天繼續寫例題