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

今天咱们来迫害下与类继承实验的第一个。

先搞一个点类,再由点类派生圆类,很简单的实验。

代码如下:

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{
    private 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);
    }
};
public class Class4 {
    public static void main(String[] args) {
        double  r;
        Scanner input=new Scanner(System.in);
        r=input.nextDouble();
        input.close();
        Circle c1=new Circle(1,2,4);
        Circle c2=new Circle();
        Circle c3=new Circle(1,2,4);
        c1.show();
        System.out.print("\nArea="+c1.area()+"\nCircumference="+c1.circumference()+"\n");
        c2.show();
        System.out.print("\nArea="+c2.area()+"\nCircumference="+c2.circumference()+"\n");
        c3.setRadius(r);
        c3.show();
        System.out.print("\nArea="+c3.area()+"\nCircumference="+c3.circumference()+"\n");
        
    }
}

运行结果如下:

 

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