java创建Shape类,求子类circle,圆形rectangle矩形,rhombus菱形的周长和面积


测试类:主要实现创建新对象,并初始化,然后输出结果。
package lesson2;


public class Demo1 {


public static void main(String[] args) {
Circle c =new Circle(new Point(5,5),5);
c.printLocation();
   System.out.println(c.getArea());
   Rectangle d=new Rectangle(new Point(3,3),new Point(3,6),new Point(6,3));
   d.printLocation();
   System.out.println(d.getArea());
   System.out.println(d.getLength());
   Rhombus e=new Rhombus(new Point(6,6),new Point(4,8),new Point(6,10),new Point(8,8));
   System.out.println(e.getArea());
   System.out.println(e.getLength());
}


}


菱形Rhombus类的实现代码:


//求菱形面积
public class Rhombus extends Shape {
Point c=new Point();
Point d=new Point();

public Rhombus(Point p,Point q,Point c,Point d){
super(p,q);
this.c=c;
this.d=d; //super(location);
}
public double getArea(){
return this.getP().getDistance(c)*this.getQ().getDistance(d);
}
}

矩形Rectangle面积的实现方法

package lesson2;
//自己写个矩形求面积。
public class Rectangle extends Shape {
// private Point b,c;
Point b=new Point();
Point c=new Point();
public  Rectangle(Point location, Point b, Point c){
super(location);
this.b=b;
this.c=c;
}
 
public double getArea(){
 
return this.getLocation().getDistance(b)*this.getLocation().getDistance(c);
}
public double getLength(){
return 2*(this.getLocation().getDistance(b)+this.getLocation().getDistance(c));
}


}

圆Circle面积周长实现的代码。


public class Circle extends Shape{
private double r;
public Circle(Point Location,double r) {
super(Location);/*创建子类的构造函数,必须调用父类的构造函数*/
this.r =r;
 }//圆作为图形的子列。继承点的位置,还有自己半径的属性
public double getArea(){
return 3.14*r*r;
}//yuan自己的行为方法
public double getlength(){
return 2*3.14*r;
}
//java中的继承是全部继承,但是private继承过来后
//不能直接访问,只能通过继承过来的父类的非private函数访问。
public boolean contains(Point p){
return this.getLocation().getDistance(p)<=r;
}
}
/*public void printData(){
super.printData();
System.out.print("半径是"+r);
}//为什么写打印函数,shape 类里需要生成打印方法
*/其他图形你也可以这样写打出边长等


这些图形都是Shape类的子类

public class Shape {
//画图形,必须有一个点,根据此点绘制图形。
  private Point location;
  private Point p,q;
  public Shape(){}
  public Shape(Point location){
 this.location=location;
  }//构造函数初始化。
  
public Point getLocation() {
return location;}
public void setLocation(Point location) {
this.location = location; }


 public Shape(Point p,Point q){
this.p=p;
this.q=q;
 }
  
  public Point getP() {
return p;
}
public void setP(Point p) {
this.p = p;
}
public Point getQ() {
return q;
}
public void setQ(Point q) {
this.q = q;
}
//私有属性不能直接访问,只能用Set,get方法实现调用
//set()是给属性赋值的,get()是取得属性值的被设置和存取的属性一般是私有 
  //主要是起到封装的作用,不允许直接对属性操作 ,它必须返回属性类型的值
  public double getArea(){return 0;}//获得面积的方法
  public double getLength(){return 0;}//获得周长
  public void printLocation(){
 //打印方法
 System.out.println("图形位置"+location.getX()+","+location.getY());
  }
/*public void printData() {//为什么需要这个函数,测试用
System.out.println(location.getX()+location.getY());
}*/
public void draw() {
}
}

Shape类的方法构造,必须使用点,所以需要构造一个点类,供Shape调用。两者之间是依赖关系不是继承关系。继承是重写父类的行为。依赖关系只存在属性和方法的调用,

Point 类的属性和方法,比如点的位置,点到点的距离,



public class Point  {


private double x,y;
    public Point(){}//无参构造函数
public Point(double x, double y) {
//super();
this.x = x;
this.y = y;
}
public double getX() {
return x;//同理,私有属性用get set 实现被调用

}public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getDistance(){
return Math.sqrt(x*x+y*y);
}//点的行为无参函数
public double getDistance(Point p){
return Math.sqrt((this.x-p.x)*(this.x-p.x)+(this.y-p.y)*(this.y-p.y));
  
}//构成方法的重载把求两点之间的距离写出来,
  public double getDistance(Point p,Point q){
return Math.sqrt((p.x-q.x)*(p.x-q.x)+(p.y-q.y)*(p.y-q.y));
     }
}

不足之处,请多多指教。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值