代码
/*第一题:
(1)定义接口A,里面包含值为3.14的常量PI和抽象方法double area()。
(2)定义接口B,里面包含抽象方法void setColor(String c)。
(3)定义接口C,该接口继承了接口A和B,里面包含抽象方法void volume()。
(4)定义圆柱体类Cylinder实现接口C,该类中包含三个成员变量:底圆半径radius、
圆柱体的高height、颜色color。
(5)创建主类来测试类Cylinder。*/
public class Homework1 {
public static void main(String[] args) {
C cylinder = new Cylinder(2,2,"黑色");
System.out.println(cylinder);
}
}
//定义接口A,里面包含值为3.14的常量PI和抽象方法double area()
interface A{
double PI = 3.14;
double area();
}
//定义接口B,里面包含抽象方法void setColor(String c)
interface B{
void setColor(String c);
}
//定义接口C,该接口继承了接口A和B,里面包含抽象方法void volume()
interface C extends A, B{
double volume();
}
//定义圆柱体类Cylinder实现接口C,该类中包含三个成员变量:
//底圆半径radius、圆柱体的高height、颜色color
class Cylinder implements C{
//底圆半径
private double radius;
private double height;
private String color;
//constructor
public Cylinder() {
}
public Cylinder(double radius, double height, String color) {
this.radius = radius;
this.height = height;
this.color = color;
}
//setter and getter
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public String getColor() {
return color;
}
@Override
public double area() {
return this.getRadius()*this.getRadius()*PI;
}
@Override
public void setColor(String c) {
this.color = c;
}
@Override
public double volume() {
return this.area()*this.getHeight();
}
@Override
public String toString() {
return "Cylinder{" +
"radius=" + radius +
", height=" + height +
", color='" + color + '\'' +
'}';
}
}