2020.8.26收获

学习内容

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 }
复制代码

 

原文地址:https://www.cnblogs.com/ltw222/p/14151465.html