java学习54天2020/8/28

定义一个Document类,有数据成员name,从Document派生Book类,增加数据成员pageCount。

     private String name; //书名
     public Document(String name) {
         this.name = name;
     }
     public String getName() {
         return name;
     }
 }

 public class Book extends Document{
     private int pageCount; //页数
     public Book(String name,int pageCount) {
         super(name);
         this.pageCount=pageCount;
     }
     public void display() {
         System.out.println("Name:"+getName());
         System.out.println("PageCount:"+pageCount);
     }
     public static void main(String[] args) {
         Book b=new Book("Java",444);
         b.display();
     }
 }

 

 

 定义一个Object1类,有数据成员weight及相应的操作函数,由此派生出Box类,增加数据成员height和width及相应的操作函数,声明一个Box对象,观察构造函数的调用顺序。

class Object1{
     private float weight;
     public Object1(float weight) {
         this.weight=weight;
         System.out.println("构造Object1类");
     }
     public float getWeight() {
         return weight;
     }
 }
 public class Box extends Object1{
     private float height;
     private float width;
     public Box(float weight,float height,float width) {
         super(weight);
         this.height=height;
         this.width=width;
         System.out.println("构造Box类");
     }
     public void display() {
         System.out.println("The weight of the box is "+getWeight());
         System.out.println("The height of the box is "+height);
         System.out.println("The width of the box is "+width);
     }
     public static void main(String[] args) {
         Box b=new Box(1.5554f,1,1.55f);
         b.display();
     }
 }

 

 

 

三。例题

posted @ 2020-08-28 19:33  小强哥in  阅读(163)  评论(0编辑  收藏  举报