哈利路亚
Let's take one million years picnic
欢迎来到Sean Cleveland的博客
JAVA学习日报 7.19

迫害作业,迫害作业……

今天要在前一个作业的基础上再派生一个球类

代码如下:

package natusyasumi;
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{
    public double radius=0;
    public double PI = 3.1415;
    public double  circumference() {
        return radius*2*PI;
    }
    public double  area() {
        return radius*radius*PI;
    }   
    public void  setRadius(double r) {
        radius=r;
    }
    public double  getRadius() {
        return radius;
    }
    public Circle(double x1, double y1, double radius1){
        this.radius=radius1;
        this.x=x1;this.y=y1;
           }
    public Circle() {}
    public Circle(Circle c)
           {
               super();
               this.x=c.getX();
               this.y=c.getY();
               this.radius=c.getRadius();
           }
    public void show() {
        System.out.print("("+x+","+y+"),Radius="+radius);
    }
}
class Sphere extends Circle{
    public Sphere(double x1,double y1,double r1){
               this.x=x1;
               this.y=y1;
               this.radius=r1;
            }
    public Sphere(Sphere a){
                super();
                this.x=a.x;
                this.y=a.y;
                this.radius=a.radius;
            }
    public double volume() {
                return (4*PI*radius*radius*radius)/3;
            }
    public double area() {
                return 4*PI*radius*radius;
            }
}
public class Class5 {
    public static void main(String[] args) {
        double  r;
        Scanner input=new Scanner(System.in);
        r=input.nextDouble();
        input.close();
        Sphere s1=new Sphere(1,2,4);
        Sphere s2=new Sphere(s1);
        s1.show();
        System.out.print("\nArea="+s1.area()+"\nVolume="+s1.volume()+"\n");
        s2.setRadius(r);
        s2.show();
        System.out.print("\nArea="+s2.area()+"\nVolume="+s1.circumference()+"\n");
    }
}

在此特别提及一下,我原本以为包是个类似文件夹的玩意,结果发现这东西里所有的类都是共同的,再定义新类会造成冲突,因此重新分包着实花了点时间

运行结果如下:

 

posted on 2020-07-19 20:10  哈利路亚#0207  阅读(148)  评论(0编辑  收藏  举报
Live2D