官术网_书友最值得收藏!

2.11 Sample Program Practice

Example 2.1 Static Demo

//對靜態修飾符修飾的變量,可以直接用類名來調用
class StaticDemo{
     static int a = 23;
     static int b = 23;
     static void callme(){
             System.out.println("a = " + a);
     }
}
class Static{
     public static void main(String args[]){
             staticDemo.callme();
             System.out.println("b = " + staticDemo.b);
     }
}

Example 2.2 Inheritance

//實現繼承機制
class Box{
    double width;
    double height;
    double depth;
    Box(){//構造函數1
            width = height = depth = -1;
    }
    Box(double len) {//構造函數2
            width = height = depth = len;
    }
    Box(double w, double h, double d) { //構造函數3
            width = w;
            height = h;
            depth = d;
    }
    double volume(){
            return width*height*depth;
    }
}
class boxWeight extends Box { //繼承了父類
    double weight;
    boxWeight(double w, double h, double d, double wh){
            width = w;
            height = h;
            depth = d;
            weight = wh;
    }
    double volume(){
            return width*height*depth*weight;
    }
}
class Sample{
    public static void main(String args[]){
            boxWeight mybox1 = new boxWeight(10,11,12,13);
            double vol;
            vol = mybox1.volume();
            System.out.println(vol);
    }
}
主站蜘蛛池模板: 梓潼县| 洮南市| 澄迈县| 大英县| 上饶县| 嘉禾县| 抚远县| 朝阳县| 昭平县| 铁力市| 吴忠市| 莱芜市| 闻喜县| 新津县| 娱乐| 鹰潭市| 双柏县| 左云县| 宜宾县| 清水县| 河曲县| 遂平县| 甘肃省| 黔南| 吉水县| 玛多县| 巴东县| 冷水江市| 腾冲县| 灯塔市| 长丰县| 日照市| 新野县| 商河县| 崇义县| 白玉县| 西平县| 浦城县| 营口市| 琼海市| 咸丰县|