JAVA学习之路_homework_day10
程序员文章站
2022-06-10 16:06:14
...
1.a. 定义一个接口Compute,用来完成计算器的功能,比如最简单的加减乘除功能
interface Compute {double compute(double a, double b) }
b. 定义一个ShowCompute类,里面定义一个工具方法calculate(Compute com,double a, double b)
该方法可以执行,两个double类型数据的加减乘除(也就是需要定义不同的Compute接口的子类,实现具体的加减乘除功能,当执行该方法时,传递不同的子类对象,就可以完成不同的计算)
编写一个测试类,
通过调用ShowCompute类的compute(Compute com,double a, double b)方法来完成加减乘除功能
package com.homework.homework0723;
/**
* @Author kinmon
* @Date 2020/7/23 19:35
* @Version 1.0
*/
public class Problem1 {
public static void main(String[] args) {
double a = 10.0;
double b = 20.0;
double result = 0.0;
result = ShowCompute.calculate(new Compute() {
@Override
public double compute(double a, double b) {
return a + b;
}
},a,b);
System.out.println(result);
result = ShowCompute.calculate(new Compute() {
@Override
public double compute(double a, double b) {
return a - b;
}
},a,b);
System.out.println(result);
result = ShowCompute.calculate(new Compute() {
@Override
public double compute(double a, double b) {
return a * b;
}
},a,b);
System.out.println(result);
result = ShowCompute.calculate(new Compute() {
@Override
public double compute(double a, double b) {
return a / b;
}
},a,b);
System.out.println(result);
}
}
interface Compute {double compute(double a, double b);}
class ShowCompute {
public static double calculate(Compute com,double a, double b) {
return com.compute(a,b);
}
}
-
根据注释填写(1), (2),(3)处的代码
public class Test{
public static void main(String[] args){
// 初始化bean1
(1)
bean1.i++;
// 初始化bean2
(2)
bean2.j++;
//初始化bean3
(3)
bean3.k++;
}
class Bean1{
public int i = 0;
}static class Bean2{
public int j = 0;
}
}
class Bean{
class Bean3{
public int k = 0;
}
}