java 不知道哪里错了 返回的值 总是false
publicclassPoint{intx;inty;publicPoint(){}publicPoint(intx,inty){this.x=x;this.y=y;}/...
public class Point { int x; int y; public Point(){} public Point(int x,int y){ this.x = x; this.y = y; } /**计算当前点到一个坐标的距离**/ public double distance(int x,int y){ return Math.sqrt(this.x-x)*(this.x-x)+(this.y-y)* (this.y-y); } /**计算当前点到原点的距离**/ public double distance(){//重载上个方法 return this.distance(0,0); } /**计算当前点到另一点的距离**/ public double distance(Point other){ return this.distance(other.x,other.y); }}
-------------------------------------------------------------------------------------------------------------------------------------------
public abstract class Shape { protected Point location; public abstract double area();//计算面积的抽象方法 public abstract boolean contains(int x,int y);//计算是否包含点的抽象方法 public boolean contains(Point p){//重写上面抽象的contains方法 return this.contains(p.x,p.y); }}------------------------------------------------------------------------------------------------------------------------------------------
public class Circle extends Shape { private int r; public Circle(int x,int y,int r){ this(new Point(x,y),r); } public Circle(Point center,int r){ super.location=center; this.r=r; } /**实现父类的抽象方法*/ public double area(){ return Math.PI*r*r; } public boolean contains(int x,int y){ return super.location.distance(x,y)<=r; }}
------------------------------------------------------------------------------------------------------------------------------------------
public class ShapeDemo { public static void main(String[] args) { Shape s = new Circle(3,4,5); System.out.println(s.area()); Point p1 = new Point(6,8); System.out.println(s.contains(p1)); }
} 展开
-------------------------------------------------------------------------------------------------------------------------------------------
public abstract class Shape { protected Point location; public abstract double area();//计算面积的抽象方法 public abstract boolean contains(int x,int y);//计算是否包含点的抽象方法 public boolean contains(Point p){//重写上面抽象的contains方法 return this.contains(p.x,p.y); }}------------------------------------------------------------------------------------------------------------------------------------------
public class Circle extends Shape { private int r; public Circle(int x,int y,int r){ this(new Point(x,y),r); } public Circle(Point center,int r){ super.location=center; this.r=r; } /**实现父类的抽象方法*/ public double area(){ return Math.PI*r*r; } public boolean contains(int x,int y){ return super.location.distance(x,y)<=r; }}
------------------------------------------------------------------------------------------------------------------------------------------
public class ShapeDemo { public static void main(String[] args) { Shape s = new Circle(3,4,5); System.out.println(s.area()); Point p1 = new Point(6,8); System.out.println(s.contains(p1)); }
} 展开
展开全部
return Math.sqrt ‘( ’ (this.x-x)*(this.x-x)+(this.y-y)* (this.y-y) ‘ )’ ;
这句话少了括号。
要注意方法的优先级是要高于算术运算符的
这句话少了括号。
要注意方法的优先级是要高于算术运算符的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
distance()在哪里
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询