# 编写一个圆类Circle，该类拥有： ①一个成员变量 Radius（私有，浮点型）； // 存放圆的半径； ②两个构造方法 ...

2018/04/24 22:02

``//圆package Test;import java.util.Scanner;public class Circle{    private double Radius;    public void setRadius(double r)    {        this.Radius=r;    }    public Circle()//无参构造方法    {        this.Radius=0;//初始化为0        System.out.println("圆!");    }    public Circle(double r)//带参构造方法    {        this.Radius=r;    }    public double getRadius()    {        return Radius;    }    public double getArea()//求圆的面积    {        double r=this.Radius;        return r*r*3.14;    }    public double getPerimeter()//求圆的周长    {        return this.Radius*3.14*2;    }    public void showCircle()    {        Scanner cin=new Scanner(System.in);        System.out.println("请输入半径:");        this.setRadius(cin.nextInt());        System.out.println("半径="+this.getRadius());        System.out.println("周长="+this.getPerimeter());        System.out.println("面积="+this.getArea());    }}//圆柱``
``package Test;import java.util.Scanner;public class Cylinder extends Circle {    private double Height;//圆柱高    public void setHeight(double h) {        this.Height = h;    }    public Cylinder (double r, double  h) //构造方法    {        this.Height=h;        this.setRadius(r);    }    public double getHeight() {        return Height;    }    public double getVolume( ) //求体积    {        return this.getArea()*this.Height;    }    public void showCylinder()    {        Scanner cin=new Scanner(System.in);        System.out.println("请输入底面圆半径:");        this.setRadius(cin.nextInt());        System.out.println("请输入高:");        this.setHeight(cin.nextInt());        System.out.println("底面半径="+this.getRadius());//子类可以通过调用父类的公有成员函数间接访问父类的私有成员变量``
``        System.out.println("高="+this.getHeight());        System.out.println("圆柱的体积="+this.getVolume());    }}//主程序``
``package Test;public class Test {    public static void main(String[] args)    {        Circle c=new Circle();        c.showCircle();        Cylinder cd=new Cylinder(1.0,1.0);//给圆柱的底面圆半径和高初始化        cd.showCylinder();    }}//本题中所有的this.都可以删除``

0
0 收藏

0 评论
0 收藏
0