上QQ阅读APP看本书,新人免费读10天
设备和账号都新为新人
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); } }