今天学了什么
完成了题目以点类 Point 为父类设计圆类 Circle
源代码:
package ques;
import java.util.Scanner;
public class Point {
private double x=1,y=2;
Point(double xv,double yv) {
x=xv;
y=yv;
}
public void show() {
System.out.println("("+x+","+y+")");
}
public void setx(double xv) {
x=xv;
}
public void sety(double yv) {
y=yv;
}
public double area() {
return 0;
}
public double getx() {
return x;
}
public double gety() {
return y;
}
final double PI=3.14159;
class Circle extends Point {
private double radius;
Circle(double xv,double yv,double r){
super(xv, yv);
radius=r;
}
public void setradius(double r) {
radius=r;
}
public double radius() {
return radius;
}
public double circumference() {
return PI*radius*2;
}
public double area() {
return PI*radius*radius;
}
public void show() {
System.out.println("("+x+","+y+")"+",Radius="+radius);
}
}
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
double r=s.nextDouble();
Point p=new Point(1.0,2.0);
Circle c=p.new Circle(1.0,2.0,r);
c.show();
System.out.println("area="+c.area());
System.out.println("circumference="+c.circumference());
}
}
运行截图:
遇到的问题
main方法中调用Circle类中有参构造出现问题
直接new会报错,应该先创建一个外部实例p,再通过外部实例p实例化Circle.
明天计划
完成题目:以圆类 Circle 设计球类 Sphere