今天学了什么
以圆类 Circle 设计球类 Sphere
源代码:
package q2;
import java.util.Scanner;
public class Circle {
final double PI=3.14159;
protected double x,y,radius;
Circle(double xv,double yv,double r){
x=xv;
y=yv;
radius=r;
}
public void setx(double xv) {
x=xv;
}
public void sety(double yv) {
y=yv;
}
public double getx() {
return x;
}
public double gety() {
return y;
}
public void setradius(double r) {
radius=r;
}
public double getradius() {
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+",Area="+area());
}
class Sphere extends Circle{
Sphere(double xv,double yv,double r){
super(xv, yv, r);
}
public double volume() {
return 4*PI*radius*radius*radius/3;
}
public double area() {
return 4*PI*radius*radius;
}
}
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
double r=s.nextDouble();
Circle ci=new Circle(1.0,2.0,r);
Sphere sp=ci.new Sphere(1.0,2.0,r);
ci.show();
System.out.println("area="+sp.area());
System.out.println("volume="+sp.volume());
}
}
运行截图:
遇到的问题
JAVA中没有多继承,需要将圆类和点类合并为圆类,然后球类再继承圆类。
明天计划
学习接口