1.抽象类(注:前5个类的主函数没有测试,直接在最后一个测试类中测试的)
(1)package com.drawing;
public abstract class Shape {
abstract void draw();
}
(2)package com.drawing;
public class Point extends四六级考试网 Shape{
private int x;
private int y;
public Point(int x,int y)
{
super();
this.x=x;
this.y=y;
}
public Point()
{
this.x=0;
this.y=0;
}
public int getX() {
return x;
}
public void tX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void tY(int y) {
this.y = y;
}
public void draw()
{
System.out.println("Point is drawing!");
先定论 }
public void print()
{
System.out.println("x="+x+" y="+y);
}
public static void main(String[] args) {
}
}
(3)package com.drawing;
public class Line extends Shape{
private Point pStart=null;
private Point pEnd=null;
public Line(Point pStart,Point pEnd)
{
super();
this.pStart=pStart;
this.pEnd=pEnd;
}
public Line()
{
super();
}
public Point getpStart() {
return pStart;
}基本医疗
public晨跑多长时间为宜 void tpStart(Point pStart) {
this.pStart = pStart;
}
public Point getpEnd() {
return pEnd;
}
public void tpEnd(Point pEnd) {
this.pEnd = pEnd;
}
public String toString()
{
return "Line:"+pStart.toString()+pEnd.toString();
}
public void draw()
{
System.out.println("Line is drawing!");
}
public void print()
{
System.out.println("pStart:"+pStart.getX()+","+pStart.getY()+";pEnd:"+pEnd.getX()+","+pEnd.getY());
}
public boolean equals(Object o)
{
if(this==o)
return true;
if(o==null)
return fal;
if(o.getClass()!=this.getClass())
return fal;
el
{
Line l=(Line)o;
if(l.pStart==this.pStart&&l.pEnd==this.pEnd)
{
return true;
}
el
return fal;
}
}
public double distance()
{
return Math.sqrt((pStart.getX()-pEnd.getX())*(pStart.getX()-pEnd.getX())+(pStart.getY()-pEnd.getY())*(pStart.getY()-pEnd.getY()));
}
public static void main(String[] args)
{
}}
(4)package com.drawing;
public class Circle extends Shape{
private Point center=null;
private double radius=0;
public Circle(Point center,double radius)
{
super();
this.center=center;
this.radius=radius;
}
public Circle()
{
super();
}
public void draw()
{
System.out.println("Center is drawing!");
}
public String toString()
{
return "center:"+"("+center.getX()+","+center.getY()+")"+","+"radius:"+radius;
}
public void print()
{
System.out.println("center is "+"("+center.getX()+","+center丢失证明.getY()+")"+";"+"radius is:"+radius);
}
public double area()
{
return Math.PI*radius*radius;
}
public double circurmference()
{
return Math.PI*radius*2;
}
public static void main(String[] args)
{
}}
(5)package com.drawing;
public class Rectangle extends Shape {
private Point pLefttop=null;
private Point pRightbottom=null;
public Rectangle(Point pLefttop, Point pRightbottom) {
super();
this.pLefttop = pLefttop;
this.pRightbottom = pRightbottom;
}
public Rectangle() {
super();
}
public Point getpLefttop() {
return pLefttop;
}
public void tpLefttop(Point pLefttop) {
this.pLefttop = pLefttop;
}
public Point getpRightbottom() {
return pRightbottom;
}
public void tpRightbottom(Point pRightbottom) {
this.pRightbottom = pRightbottom;
}
public void draw()
{
System.out.println("Rectangle is drawing!");
}
public boolean equals(Object o)
{
if(this==o)
return true;
if(o==null)
return fal;
if(o.getClass()!=this.getClass())
return fal;
医疗保险卡el
{
Rectangle e=(Rectangle)o;
if(e.getpLefttop().equals(this.pLefttop) && e.getpRightbottom().equals(this.pRightbottom))
{
return true;
}
el
return fal;
}
}
public double area()
{
return Math.sqrt
(Math.pow(pLefttop.getX()-pRightbottom.getX(),2))
*Math.sqrt(Math.pow(pLefttop.getY()-pRightbottom.getY(),2));
}
二十八天public double circurmference()
{
return Math.abs(pLefttop.getX()-pRightbottom.getX())*2+Math.abs(pLefttop.getY()-pRightbottom.getY())*2;
gentlely }
public static void main(String[] args)
{
}
}
(6)package com.drawing;
public class Test {
public static void main(String[] args) {
Point p=new Point(1,2);
p.print();
p.draw();
Point p4=new Point(5,3);
Point p5=new Point(3,4);
Line line=new Line(p4,p5);
Line l1=new Line(new Point(5,3),new Point(3,4));
line.draw();
line.print();
Shape s=line;
s=new Point(5,9);
((Point)s).print();
System.out.println(line.equals(l1));
System.out.println("Diatance is :"+line.distance());
Point p1=new Point(1,1);
Circle c1=new Circle(p1,2);
Circle c2=new Circle(new Point(0,1),3);
c1.print();
System.out.println(c1);
c1.draw();
System.out.println("area is :"+c1.area());