centos防火牆
阿新 • • 發佈:2020-12-17
學習內容
1、定義一個基類Base,有兩個公有成員函式fn1(),fn2(),私有派生出Derived類,如何通過Derived類的物件呼叫基類的函式fn1()
1 //基類: 2 public class Base { 3 public void fn1() { 4 System.out.println("a"); 5 } 6 public void fn2() { 7 System.out.println("b"); 8 } 9 } 10 //子類: 11 public class Derived extends Base { 12 public void fn1() { 13 super.fn1(); 14 } 15 public void fn2() { 16 super.fn2(); 17 } 18 public static void main(String[] args) { 19 Derived m=new Derived(); 20 m.fn1(); 21 m.fn2(); 22 } 23 }
2、定義一個Object類,有資料成員weight及相應的操作函式,由此派生出Box類,增加資料成員height和width及相應的操作函式,宣告一個Box物件,觀察建構函式的呼叫順序。
1 //Object類: 2 public class Object { 3 protected float weight; 4 Object(float w){ 5 weight=w; 6 System.out.println("Object類建構函式"); 7 } 8 } 9 //Box類: 10 public class Box extends Object { 11 private float height,width; 12 Box(float w,float h,float wi){ 13 super(w); 14 height=h; 15 width=wi; 16 System.out.println("Box類建構函式"); 17 } 18 19 public static void main(String[] args) { 20 Box x=new Box(1,2,3); 21 } 22 }