JAVA学习日报 7.27

今天写一个圆锥类

本来原题目中有平面图形类和立体图形类多重继承。但JAVA不能这么搞,所以为了达成相同效果费了点劲

代码如下:

package shikutai;

import java.util.Scanner;

class Point{
    protected double x=0;
    protected double y=0;
    public Point() {}
    public Point(double xv,double yv) {
        x=xv;y=yv;
    }
    public Point(Point p){
        x=p.x;y=p.y;
    }
    public void show(){
        System.out.print("("+x+","+y+")");
    }
    public void setX(double xv) {
        x=xv;
    }
    public void setY(double yv) {
        y=yv;
    }
    public double getX() {
        return x;
    }
    public double getY() {
        return y;
    }
}
class Circle extends Point
{
    private double radius=0;
    protected static final double PI=3.1415;
    public Circle(double xv,double yv,double r)
    {
        super(xv,yv);
        this.radius=r;
    }
    public Circle(Circle c)
    {
        super (c);
        this.radius=c.radius;
    }
    public void setR(double r)
    {
        radius=r;
    }
    public double getR()
    {
        return radius;
    }
    public void show()
    {
        System.out.print("Circle(");
        super.show();
       System.out.print(",Radius="+radius+")");
    }
    public void area()
    {
         System.out.print("Area="+radius*radius*PI+"
");
    }
    public void length()
    {
         System.out.print("Length="+radius*2*PI+"
");
    }
};
class Cylinder extends Circle
{
    private double height;
    public Cylinder(double q,double w,double r,double e)
      {
          super(q,w,r);
          this.height=e;
      }
    public Cylinder(Cylinder c)
      {
          super(c);
          height=c.height;
      }
    public void show()
      {
          System.out.print("Cylinder(");
          super.show();
          System.out.print(",Height="+height+")");
      }
    public void s_Area()
      {
          System.out.print("S_Area="+2*PI*getR()*(height+getR())+"
");
      }
    public void volume()
      {
         System.out.print("Volume="+PI*getR()*getR()*height/3+"
");
      }
    public void setH(double k)
      {
          height=k;
      }
    public double getH()
      {
          return height;
      }
};
public class Class9 {
    public static void main(String[] args) {
        double  h;
        Scanner input=new Scanner(System.in);
        h=input.nextDouble();
        input.close();
        Cylinder cy1=new Cylinder(1,2,3,4),cy2=new Cylinder(cy1);
        cy1.show();
        System.out.print("
");
        cy1.area();
        cy1.length();
        cy1.s_Area();
        cy1.volume();
        cy2.setH(h);
        cy2.show();
        System.out.print("
");
        cy2.area();
        cy2.length();
        cy2.s_Area();
        cy2.volume();
    }
}

运行结果如下:

原文地址:https://www.cnblogs.com/Sakuraba/p/13452107.html