1.定义一个基类Shape,在此基础上派生出Rectangle和Circle,二者都有getArea()函数计算对象的面积。使用Rectangle类创建一个派生类Square。
1 //基类Shape 2 class Shape { 3 public double getArea() { 4 return 0; 5 } 6 } 7 //Rectangle类继承Shape类 8 class Rectangle extends Shape { 9 private double len; 10 private double width; 11 public Rectangle(double len, double width) { 12 super(); 13 this.len = len; 14 this.width = width; 15 } 16 public double getArea() { 17 return len * width; 18 } 19 } 20 //Circle类继承Shape类 21 class Circle extends Shape { 22 private double r; 23 public Circle(double r) { 24 super(); 25 this.r = r; 26 } 27 public double getArea() { 28 return 3.14 * r * r; 29 } 30 } 31 //Square类继承Rectangle类 32 public class Square extends Rectangle { 33 public Square(double len, double width) { 34 super(len, width); 35 } 36 public static void main(String[] args) { 37 Shape p = new Rectangle(3, 6); 38 System.out.println("The area of the rectangle is " + p.getArea()); 39 p = new Circle(3); 40 System.out.printf("The area of the circle is %.2f\n", p.getArea()); 41 p = new Square(4, 4); 42 System.out.println("The area of the square is " + p.getArea()); 43 } 44 }
2.定义一个哺乳动物类Mammal,再由此派生出狗类Dog,定义一个Dog类的对象,观察基类和派生类的构造函数的调用顺序。
1 //父类Mammal类 2 class Mammal{ 3 private String name; 4 private int age; 5 public Mammal(String name, int age) { 6 this.name=name; 7 this.age=age; 8 System.out.println("构造哺乳动物类"); 9 } 10 public String getName() { 11 return name; 12 } 13 public int getAge() { 14 return age; 15 } 16 } 17 //Dog类继承Mammal类 18 public class Dog extends Mammal{ 19 private int leg; 20 public Dog(String name, int age, int leg) { 21 super(name, age); 22 this.leg=leg; 23 System.out.println("构造狗类"); 24 } 25 public void display() { 26 System.out.println("Name:"+getName()); 27 System.out.println("Age:"+getAge()); 28 System.out.println("Leg:"+leg); 29 } 30 public static void main(String[] args) { 31 Dog p=new Dog("Sera",5,4); 32 p.display(); 33 } 34 }
3.定义一个基类及其派生类,在构造函数中输出提示提示信息,构造派生类的对象,观察构造函数的执行情况。
1 //父类Base类 2 class Base{ 3 public Base() { 4 System.out.println("构造基类"); 5 } 6 } 7 //Derived类继承Base类 8 class Derived extends Base{ 9 public Derived() { 10 System.out.println("构造派生类"); 11 } 12 13 } 14 public class Test { 15 public static void main(String[] args) { 16 Derived p=new Derived(); 17 } 18 }
4.定义一个Document类,有数据成员name,从Document派生Book类,增加数据成员pageCount。
1 //父类Document类 2 class Document{ 3 private String name; //书名 4 public Document(String name) { 5 this.name = name; 6 } 7 public String getName() { 8 return name; 9 } 10 } 11 //子类Book类 12 public class Book extends Document{ 13 private int pageCount; //页数 14 public Book(String name,int pageCount) { 15 super(name); 16 this.pageCount=pageCount; 17 } 18 public void display() { 19 System.out.println("Name:"+getName()); 20 System.out.println("PageCount:"+pageCount); 21 } 22 public static void main(String[] args) { 23 Book b=new Book("Java程序设计",425); 24 b.display(); 25 } 26 }
5.定义一个Object1类,有数据成员weight及相应的操作函数,由此派生出Box类,增加数据成员height和width及相应的操作函数,声明一个Box对象,观察构造函数的调用顺序。
1 //父类Object1类 2 class Object1{ 3 private float weight; 4 public Object1(float weight) { 5 this.weight=weight; 6 System.out.println("构造Object1类"); 7 } 8 public float getWeight() { 9 return weight; 10 } 11 } 12 //子类Box类 13 public class Box extends Object1{ 14 private float height; 15 private float width; 16 public Box(float weight,float height,float width) { 17 super(weight); 18 this.height=height; 19 this.width=width; 20 System.out.println("构造Box类"); 21 } 22 public void display() { 23 System.out.println("The weight of the box is "+getWeight()); 24 System.out.println("The height of the box is "+height); 25 System.out.println("The width of the box is "+width); 26 } 27 public static void main(String[] args) { 28 Box b=new Box(1.2f,1,0.8f); 29 b.display(); 30 } 31 }
6.定义一个基类BaseClass,从它派生出类DerivedClass。BaseClass有成员函数fn1(),fn2(),DerivedClass也有成员函数fn1(),fn2()。在主函数中声明对象来调用fn1(),fn2(),观察运行结果。
1 //基类BaseClass 2 class BaseClass{ 3 public void fn1() { 4 System.out.println("Base.fn1"); 5 } 6 public void fn2() { 7 System.out.println("Base.fn2"); 8 } 9 } 10 //子类DerivedClass 11 public class DerivedClass extends BaseClass { 12 public void fn1() { 13 System.out.println("Derived.fn1"); 14 } 15 public void fn2() { 16 System.out.println("Derived.fn2"); 17 } 18 public static void main(String[] args) { 19 DerivedClass m=new DerivedClass(); 20 m.fn1(); 21 m.fn2(); 22 BaseClass n=new BaseClass(); 23 n.fn1(); 24 n.fn2(); 25 n=new DerivedClass(); 26 n.fn1(); 27 n.fn2(); 28 } 29 }