首页 > 代码库 > java 初学之面向对象设计 三角形,圆等设计

java 初学之面向对象设计 三角形,圆等设计

首先要对面向对象有一定了解,下面利用继承与派生的方式来演示java的面向对象过程;

创建一个java工程,添加一个新的pakage:Shapec;

添加一个新类,写入代码

/* * To change this template, choose Tools | Templates * and open the template in the editor. */

 

package Shapec;/** * * @author Administrator *///抽象类,图形借口public abstract class Shape {    public static double PI = Math.PI; //π=3.1415926    public abstract double GetArea(); //计算面积    public abstract double GetLong();//计算周长}

第二部,继承与派生,

新建5个新的类,分别命名为:J_Circle, J_FiveS ,J_Ponit,J_Triangle,J_Rectangle

其中 J_Ponit 为坐标点类。下面给出各个代码:

/* * To change this template, choose Tools | Templates * and open the template in the editor. */package Shapec;/** * * @author Administrator */public class J_Circle extends Shape {    public double m_r;    public double m_x , m_y;    public J_Circle(double r)    {        m_x = m_y = 0;        m_r = r;    }    public J_Circle(double m_r, double m_x, double m_y) {        this.m_r = m_r;        this.m_x = m_x;        this.m_y = m_y;    }    public J_Circle(J_Ponit p,double r){        m_x = p.m_x;        m_y = p.m_y;        m_r = r;    }//    计算面积    public double GetArea() {        return (Shape.PI * m_r * m_r);    }//  计算周长    public double GetLong() {       return (2 * PI * m_r);    }    @Override    public String toString() {        return("圆\n"+"面积为:" + GetArea() + ";\n周长为:"+ GetLong());    }}
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package Shapec;/** * * @author Administrator */public class J_FiveS extends Shape {    private double E;    public double m_a;    public J_Ponit a;    public J_Ponit b;    public J_Ponit c;    public J_Ponit d;    public J_Ponit e;    public J_FiveS( J_Ponit a, J_Ponit b, J_Ponit c, J_Ponit d, J_Ponit e) {        this.E = 1.72;        this.m_a = J_Ponit.GetLongC(a, b);        this.a = a;        this.b = b;        this.c = c;        this.d = d;        this.e = e;    }//    五边形一个点坐标和图形中心的坐标    public J_FiveS(J_Ponit a, J_Ponit o){        this.E = 1.72;       double t = J_Ponit.GetLongC(a, o);        m_a = Math.sqrt(2 * t * t * (1 - Math.cos(72 *( Math.PI / 180))));    }    public J_FiveS(double a) {        this.E = 1.72;        this.m_a = a;    }    @Override    public double GetArea() {        return E*m_a*m_a;    }    @Override    public double GetLong() {        return 5*m_a;    }         @Override    public String toString() {        return("五边形 \n"+"面积为:" + GetArea() + ";\n周长为:"+ GetLong());    }}
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package Shapec;/** * * @author Administrator */public class J_Ponit {    public double m_x;    public double m_y;    public J_Ponit(double m_x, double m_y) {        this.m_x = m_x;        this.m_y = m_y;    }    public J_Ponit() {        m_x = 0;        m_y = 0;    }     public J_Ponit(J_Ponit p) {        m_x = p.m_x;        m_y = p.m_y;    }    public void SetX(double x){        m_x = x;    }    public void SetY(double y){        m_y = y;    }    public static double GetLongC(J_Ponit left,J_Ponit right){        return Math.sqrt(Math.pow((left.m_x - right.m_x),2) + Math.pow((left.m_y - right.m_y), 2));    }    @Override    public String toString() {        return ("x="+m_x+",y="+m_y);    }}
J_Ponit

 

/* * To change this template, choose Tools | Templates * and open the template in the editor. */package Shapec;/** * * @author Administrator */public class J_Triangle extends Shape{    public double m_a;    public double m_b;    public double m_c;    private double m_p;    public J_Ponit a,b,c;    private void Culatep(){        m_p = (m_a + m_b + m_c) / 2;    }    public J_Triangle( J_Ponit a, J_Ponit b, J_Ponit c) {           this.a = new J_Ponit(a);           this.b = new J_Ponit(b);           this.c = new J_Ponit(c);            double a1 = J_Ponit.GetLongC(b,c);           double b1 = J_Ponit.GetLongC(a,c);           double c1 = J_Ponit.GetLongC(a,b);           if((a1 + b1) > c1 && (a1 - b1) < c1){               m_a = a1;               m_b = b1;               m_c = c1;           }           else{               m_a = m_b = m_c = 0;           }            Culatep();          }//    正三角型    public J_Triangle(double x) {        m_a = x;        m_b = x;        m_c = x;        Culatep();    }//    等腰三角形    public J_Triangle(double a ,double x) {        if((a + x) >x && (a - x) < x){           m_a = a;           m_b = m_c = x;        }        else{            m_a = m_b = m_c = 0;        }              Culatep();    }  //普通三角形    public J_Triangle(double ma, double mb, double mc) {        if((ma - mb) < mc && (ma + mb) > mc){        this.m_a = ma;        this.m_b = mb;        this.m_c = mc;        }       else{            m_a = m_b = m_c = 0;        }                Culatep();    }        @Override    public double GetArea() {        return Math.sqrt(m_p*(m_p - m_a)*(m_p - m_b)*(m_p - m_c));    }    @Override    public double GetLong() {       return (m_a + m_b  + m_c);    }      @Override    public String toString() {        return("三角形 \n"+"面积为:" + GetArea() + ";\n周长为:"+ GetLong());    }}
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package Shapec;/** * * @author Administrator */public class J_Rectangle extends Shape {    //矩形四个顶点    public J_Ponit a,b,c,d;    double m_a;    double m_b;    public J_Rectangle(J_Ponit a, J_Ponit b, J_Ponit c, J_Ponit d) {        this.a = new J_Ponit(a);        this.b = new J_Ponit(b);        this.c = new J_Ponit(c);        this.d = new J_Ponit(d);        m_a = J_Ponit.GetLongC(a, b);        m_b = J_Ponit.GetLongC(a, d);    }    public J_Rectangle(double a, double b) {        m_a = a;        m_b = b;    }//    获得边长    public double GetSide_one(){        return m_a;    }    public double GetSide_two(){        return m_b;    }//计算面积    @Override    public double GetArea() {        return (m_a * m_b);    }//计算周长    @Override    public double GetLong() {        return (2 * m_a + 2 * m_b);    }      @Override    public String toString() {        return("矩形 \n"+"面积为:" + GetArea() + ";\n周长为:"+ GetLong());    }}
J_Rectangle

最后在mian中调用;

/* * To change this template, choose Tools | Templates * and open the template in the editor. */package shapecu;import Shapec.J_Rectangle;import Shapec.J_Ponit;import Shapec.J_Triangle;import Shapec.J_Circle;import Shapec.Shape;import Shapec.J_FiveS;/** * * @author Administrator */public class Main {    /**     * @param args the command line arguments     */    public static void main(String[] args) {        // TODO code application logic here        Shape shape[] = new Shape[4];//        矩形坐标        J_Ponit b = new J_Ponit(1, 0);        J_Ponit c = new J_Ponit(1, 1);        J_Ponit d = new J_Ponit(0, 1);        J_Ponit a = new J_Ponit(0, 0);       shape[0] = new J_Rectangle(a, b, c, d);//三角形坐标        J_Ponit a1 = new J_Ponit(0, 0);        J_Ponit b1 = new J_Ponit(0, 4);        J_Ponit c1 = new J_Ponit(3, 0);        shape[1] = new J_Triangle(a1, b1, c1);//        圆坐标和半径         J_Ponit o = new J_Ponit(0, 0);         shape[2] = new J_Circle(o, 3);//         五边形,定义边长为3;          shape[3] = new J_FiveS(3);//         输出:          System.out.println(shape[0]);          System.out.println(shape[1]);          System.out.println(shape[2]);          System.out.println(shape[3]);                }}
main

 

java 初学之面向对象设计 三角形,圆等设计