实验二——以点类 Point 为基类设计圆类 Circle
学习内容:以点类 Point 为基类设计圆类 Circle
示例代码:
package 实验二;
import java.util.Scanner;
class Point{//父类Point
private double x;
private double y;
public Point(double x,double y) {//构造方法
x=0;
y=0;
System.out.println("Point Constructor run");
}
public void setX(double x) {//设置x坐标
this.x=x;
}
public final double getX(){//返回x坐标
return x;
}
public void setY(double y) {//设置y坐标
this.y=y;
}
public final double getY(){//返回y坐标
return y;
}
public void show() {//显示点的坐标
System.out.println("("+x+","+y+")");
}
public double area() {//显示面积
System.out.println("点的面积为0");
return 0;
}
}
public class Circle extends Point {//子类Circle
private double x;
private double y;
private static double radius;
final double PI=3.14159;
public Circle(double x, double y,double radius){//构造方法
super(x,y);
this.x=x;
this.y=y;
this.radius=radius;
System.out.println("Circle Constructorrun");
}
public void steRadius(double radius) {//设置半径
this.radius=radius;
}
public final double getRadius() {//返回半径
return radius;
}
public final double circumference() {//输出圆的周长
return 2*PI*radius;
}
public final double area() {//输出圆的面积
return PI*radius*radius;
}
public final void show() {//显示圆的信息
System.out.println("("+x+","+y+"),"+"Radius="+radius);
}
public static void main(String[] args) {
double r;
Scanner sc=new Scanner(System.in);
r=sc.nextDouble();
Circle c1=new Circle(1, 2, 4);
Circle c2=c1;
c1.show();
System.out.println();//换行
System.out.println("Area="+c1.area());
System.out.println("Circumference="+c1.circumference());
c2.steRadius(r);
c2.show();
System.out.println();//换行
System.out.println("Area="+c2.area());
System.out.println("Circumference="+c2.circumference());
}
}
运行截图:
遇到的问题:无
明天任务:实验二以人类 Person 为基类设计学生类 Student 和教师类 Teacher