继承性练习02

1 package com.yhqtv.testInheritance; 2 //主程序 3 public class CylinderTest { 4 public static void main(String[] args) { 5 6 Cylinder cylinder=new Cylinder(3.6); 7 cylinder.setRadius(2.8); 8 double area = cylinder.findArea(); 9 System.out.println("半径为"+cylinder.getRadius()+"高为"+cylinder.getLength()); 10 System.out.println("圆柱底面圆面积"+area); 11 double volume = cylinder.findVolume(); 12 System.out.println("圆柱体积为"+volume); 13 } 14 }
----------------------------
1 package com.yhqtv.testInheritance; 2 //父类 3 public class Circle { 4 5 private double radius; 6 7 public double findArea() { 8 return radius * radius * Math.PI; 9 } 10 11 public Circle() { 12 radius = 1.0; 13 } 14 15 public Circle(double radius) { 16 this.radius = radius; 17 } 18 19 public double getRadius() { 20 return radius; 21 } 22 23 public void setRadius(double radius) { 24 this.radius = radius; 25 } 26 }
------------------------------
1 package com.yhqtv.testInheritance; 2 //子类 3 public class Cylinder extends Circle { 4 5 private double length; 6 7 public double findVolume() { 8 return length * findArea(); 9 } 10 11 public Cylinder() { 12 13 length = 1.0; 14 } 15 16 public Cylinder(double length) { 17 this.length = length; 18 } 19 20 public double getLength() { 21 return length; 22 } 23 24 public void setLength(double length) { 25 this.length = length; 26 } 27 }
个人官网yhqtv.com

浙公网安备 33010602011771号