• 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();
        }
    }

    运行结果如下:

  • 相关阅读:
    HDFS常用命令总结
    mac 安装zmap
    使用scrapy-redis搭建分布式爬虫环境
    xpath选择兄弟节点、返回上一级和选择多个属性
    什么是套接字(Socket)
    socket编程——一个简单的例子
    python之 __getattr__、__getattr__、__getitem__、__setitem__ 使用
    如何快速转载文章
    如何让类也变成可以迭代的对象
    leecode 第二题 简单
  • 原文地址:https://www.cnblogs.com/Sakuraba/p/13452107.html
Copyright © 2020-2023  润新知