第一题
(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 Test {
public static void main(String[] args){
Cylinder c = new Cylinder();
c.setColor("蓝色");
c.setHeight(2.0);
c.setRadius(10.0);
System.out.println("面积是:" + c.area() + ",体积是:" + c.volume());
}
}
接口A
// 接口A
public interface A {
public static final double PI = 3.14;
public abstract double area();
}
接口B
// 接口B
public interface B {
public abstract void setColor(String c);
}
接口C
// 接口C
public interface C extends A,B {
public abstract double volume();
}
圆柱体类
// 圆柱体类
public class Cylinder implements C{
// 属性
private double radius;
private double height;
private String color;
//构造方法
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 this.color;
}
// 覆盖
@Override
public double area() {
return radius * radius * PI + 2 * PI * radius * height;
}
@Override
public void setColor(String c) {
this.color = c;
}
@Override
public double volume() {
return 2 * PI * radius * height;
}
}
测试结果
面积是:439.6体积是:125.60000000000001
第二题
利用接口做参数,写个计算器,能完成加减乘除运算。
(1)定义一个接口Compute含有一个方法int computer(int n, int m)。
(2)设计四个类分别实现此接口,完成加、减、乘、除运算。
(3)设计一个类UseCompute,类中含有方法:public void useCom(Compute com, int one, int two),
此方法能够用传递过来的对象调用computer方法完成运算,并输出运算的结果。
(4)设计一个主类Test,调用UseCompute中的方法useCom来完成加减乘除运算。
主类
public class Test {
public static void main(String[] args){
int i = 100;
int j = 5;
UseCompute u = new UseCompute();
Compute a = new Add();
Compute s = new Subtraction();
Compute m = new Multiplication();
Compute d = new Division();
u.useCom(a,i,j);
u.useCom(s,i,j);
u.useCom(m,i,j);
u.useCom(d,i,j);
}
}
Compute接口
// 运算器接口
public interface Compute {
public abstract int computer(int i, int j);
}
加法类
// 加法
public class Add implements Compute{
@Override
public int computer(int i, int j) {
return i + j;
}
}
减法类
public class Subtraction implements Compute{
@Override
public int computer(int i, int j) {
return i - j;
}
}
乘法类
public class Multiplication implements Compute{
@Override
public int computer(int i, int j) {
return i * j;
}
}
除法类
public class Division implements Compute{
@Override
public int computer(int i, int j) {
if(j == 0){
System.out.println("被除数不能为0!");
return 0;
}else{
return i / j;
}
}
}
使用计算器类
public class UseCompute {
private Compute com;
public UseCompute(){
}
public UseCompute(Compute com) {
this.com = com;
}
public Compute getCom() {
return com;
}
public void setCom(Compute com) {
this.com = com;
}
public void useCom(Compute com, int one, int two){
System.out.println("结果为:" + com.computer(one, two));
}
}
结果测试
结果为:105
结果为:95
结果为:500
结果为:20