一.实验目的

(1)掌握类的继承

(2)变量的继承和覆盖,方法的继承,重载和覆盖的实现;

二.实验的内容

(1)根据下面的要求实现圆类Circle。

1.圆类Circle的成员变量:radius表示圆的半径。

2.圆类Circle的方法成员:

   Circle():构造方法,将半径置0

   Circle(double r):构造方法,创建Circle对象时将半径初始化为r

   double getRadius():获得圆的半径值

   double getPerimeter():获得圆的周长

   void disp():将圆的半径和圆的周长,圆的面积输出到屏幕
import java.util.*; 
class Circle { 
    double radius;  
    public Circle(){
        this.setRadius(0); 
    } 
     
    public Circle(double r){
        this.setRadius(r); 
    } 
 
    public void setRadius(double r) {
        radius = r; 
    } 
    
    public double getRadius() { 
        return radius; 
    } 
     
    public double getPerimeter(){ 
        return 2*Math.PI*getRadius(); 
    } 
     
    public double getArea(){ 
        return Math.PI*getRadius()*getRadius(); 
    } 
    
    public void disp(){
        System.out.println("圆的半径:"+getRadius()+"   圆的周长:"+getPerimeter()+"   圆的面积:"+getArea()); 
    } 
} 

public class zuoye0601 {
    public static void main(String[] args) {
        Circle yuan = new Circle(); 
        yuan.disp();
        System.out.print("请输入圆的半径:");
        Scanner radius = new Scanner(System.in);
        Double r = radius.nextDouble();
        Circle yuan1 = new Circle(r);
        yuan1.disp();
    }
}

(2)继承第一题中的圆Circle类,派生圆柱体类Cylinder。要求如下:

1.圆柱体类Cylinder的成员变量:height表示圆柱体的高。

2.圆柱体类Cylinder的成员方法:

    Cylinder(double r,double h)构造方法,创建Cylinder对象时将圆半径初始化为r,圆柱体高初始化为h。

    double getHeight():获得圆柱体的高

    double getVol():获得圆柱体的体积

    void dispVol():将圆柱体的体积输出到屏幕
import java.util.*; 
class Circle { 
    double radius;  
    public Circle(){
        this.setRadius(0); 
    } 
     
    public Circle(double r){
        this.setRadius(r); 
    } 
 
    public void setRadius(double r) {
        radius = r; 
    } 
    
    public double getRadius() { 
        return radius; 
    } 
     
    public double getPerimeter(){ 
        return 2*Math.PI*getRadius(); 
    } 
     
    public double getArea(){ 
        return Math.PI*getRadius()*getRadius(); 
    } 
    
    public void disp(){
        System.out.println("圆的半径:"+getRadius()+"   圆的周长:"+getPerimeter()+"   圆的面积:"+getArea()); 
    } 
} 

class Cylinder extends Circle{
    double height; 
    Cylinder(double r,double h){
        super(r);
        this.setHeight(h); 
    } 
    
    public void setHeight(double h) {  
        height = h; 
    } 
    
    public double getHeight() {  
        return height; 
    } 
     
    public double getVol(){  
        return getArea()*getHeight(); 
    } 
     
    public void dispVol(){
        System.out.println("底面圆的半径: "+getRadius()+" 底面圆的周长: "+getPerimeter()+" 底面圆的面积: "+getArea()+" 圆柱体的体积: "+getVol());
    } 
} 

public class zuoye0602 {
    public static void main(String[] args) {
        Cylinder yuan2 = new Cylinder(4,6);
        yuan2.dispVol();

    }
}

3.按照第二题的Cylinder类,接上键盘的输入值作为圆的半径和圆柱体的高,计算圆柱体的体积

import java.util.*;
class Circle {
double radius;
public Circle(){
this.setRadius(0);
}

public Circle(double r){
    this.setRadius(r); 
} 

public void setRadius(double r) {
    radius = r; 
} 

public double getRadius() { 
    return radius; 
} 
 
public double getPerimeter(){ 
    return 2*Math.PI*getRadius(); 
} 
 
public double getArea(){ 
    return Math.PI*getRadius()*getRadius(); 
} 

public void disp(){
    System.out.println("圆的半径:"+getRadius()+"   圆的周长:"+getPerimeter()+"   圆的面积:"+getArea()); 
} 

}

class Cylinder extends Circle{
double height;
Cylinder(double r,double h){
super(r);
this.setHeight(h);
}

public void setHeight(double h) {  
    height = h; 
} 

public double getHeight() {  
    return height; 
} 
 
public double getVol(){  
    return getArea()*getHeight(); 
} 
 
public void dispVol(){
    System.out.println("底面圆的半径: "+getRadius()+" 底面圆的周长: "+getPerimeter()+" 底面圆的面积: "+getArea()+" 圆柱体的体积: "+getVol());
} 

}

public class zuoye0603 {
public static void main(String[] args) {
System.out.print("请输入圆柱体的半径:");
Scanner radius = new Scanner(System.in);
Double r = radius.nextDouble();
System.out.print("请输入圆柱体的高:");
Scanner height = new Scanner(System.in);
Double h = height.nextDouble();
Cylinder yuan2 = new Cylinder(r,h);
yuan2.dispVol();
}
}