Java-定义圆类

 

目录

Java-定义圆类01: 

Java-定义圆类02:

组合----》圆类+点类 

用点类的对象p,作为圆类的属性。

先定义点类: 

 圆类02:

 圆类03

圆03:

圆怎么形成的?

点和半径

-----------------》》》》》》》

                                               继承点类,创建圆。

点类(父类)

子类:

 测试类:

 运行结果:


Java-定义圆类01: 

package Lian.Xi;
/**
 * @author captain_dong
 * --圆类定义:
 * 属性:圆心坐标-半径---方法:计算周长-面积
 */
public class Yuan01 {
    private double x;
    private double y;
    private double r;

    //无参构造
    public Yuan01() {
    }

    //有参构造
    public Yuan01(double x, double y, double r) {
        this.x = x;
        this.y = y;
        this.r = r;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    public double getR() {
        return r;
    }

    public void setR(double r) {
        this.r = r;
    }

    //计算周长
    public double Zc(){
        return 2*Math.PI*this.r;
    }
    
    //计算面积
    public double Area(){
        return Math.PI*Math.pow(this.r,2);
    }
    
    //获取圆的信息
    @Override
    public String toString() {
        return "Yuan01{" +
                "横坐标:x=" + x +
                ",纵坐标:y=" + y +
                ", 半径r=" + r +
                '}';
    }
}

Java-定义圆类02:

组合----》圆类+点类 

用点类的对象p,作为圆类的属性。

先定义点类: 

package Lian.Xi;

public class Point {
    private double x;
    private double y;

    public Point() {
    }

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    @Override
    public String toString() {
        return "Point{" +
                "x=" + x +
                ", y=" + y +
                '}';
    }

    //获取点信息
    public String getMessage(){
        String PXx;
        return PXx="该圆心的点的坐标为:"+"("+this.x+","+this.y+")";
    }
}

 圆类02:

package Lian.Xi;
/**
 * @author captain_dong
 * --圆类定义02:
 * 属性:圆心坐标-半径---方法:计算周长-面积
 */
public class Yuan02 {
    private Point p;
    private double r;

    //无参构造
    public Yuan02() {
    }

    //有参构造
    public Yuan02(Point p, double r) {
        this.p = p;
        this.r = r;
    }

    //计算周长
    public double Zc(){
        return 2*Math.PI*this.r;
    }

    //计算面积
    public double Area(){
        return Math.PI*Math.pow(this.r,2);
    }

    //获取圆的信息
    public String getYuan(){
        String gy="";
        //gy="("+p.getX()+","+p.getY()+")"+",半径:"+this.r;
        gy=p.getMessage()+",半径:"+this.r;
        return gy;
    }

    @Override
    public String toString() {
        return "Yuan02{" +
                "p=" + p +
                ", r=" + r +
                '}';
    }
}

 圆类03

圆03:

圆怎么形成的?

点和半径

-----------------》》》》》》》

                                               继承点类,创建圆。

点类(父类)

package Lian.Xi;

public class Point {
    private double x;
    private double y;

    public Point() {
    }

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    @Override
    public String toString() {
        return "Point{" +
                "x=" + x +
                ", y=" + y +
                '}';
    }

    //获取点信息
    public String getMessage(){
        String PXx;
        return PXx="该圆心的点的坐标为:"+"("+this.x+","+this.y+")";
    }
}

子类:

package Lian.Xi;

/**
 * 继承点类,创建圆。
 */
public class Yuan03 extends Point{  //父类
    //添加一个新属性
    private double r;

    public Yuan03(){
        super();//父类对象
    }

    //子类(新类)构造方法
    public Yuan03(double x,double y,double r){//3个属性
        super(x,y);//点 x,y  super():父类对象,--》使用父类对象构造方法,形成点对象
        this.r=r;
    }

    //新的构造函数
    public Yuan03(Point p,double r){//2个属性
        super(p.getX(),p.getY());
        this.r=r;
    }

    //添加新方法

    //计算周长
    public double Zc(){
        return 2*Math.PI*this.r;
    }

    //计算面积
    public double Area(){
        return Math.PI*Math.pow(this.r,2);
    }

    //获取圆信息
    public String getMessage(){    //修改功能
        String YXx;
        YXx="圆是:"+super.getMessage()+",半径 r= "+this.r;
        return YXx;
    }

}

 测试类:

package Lian.Xi;

public class TestYuan {
    public static void main(String[] agrs){
        Yuan01 yuan01=new Yuan01(10,-10,100);

        Point point=new Point(20,-20);

        Yuan02 yuan02=new Yuan02(point,100);

        Yuan03 yuan03=new Yuan03(30,-30,100);

        Yuan03 yuan031=new Yuan03(point,100);

        System.out.println("Yuan01'Message is :"+yuan01.toString());

        System.out.println("Yuan02'Message is :"+yuan02.getYuan());

        System.out.println("Yuan03'Message is :"+yuan03.getMessage());

        System.out.println("Yuan03-1'Message is :"+yuan031.getMessage());
    }
}

 运行结果:

Java-定义圆类_第1张图片

你可能感兴趣的:(Java模型,java练习,平时学习,java,开发语言)