当前位置:网站首页>2. interface (calculator)
2. interface (calculator)
2022-06-22 23:17:00 【Xiaomurong】
2 Interface
Using the interface as a parameter , Complete a calculator , Can complete addition, subtraction, multiplication and division .
(1) Define an interface Compute It contains a method int computer(int n, int m).
(2) Design four classes to implement this interface , Complete addition, subtraction, multiplication and division .
(3) Design a class UseCompute, Class contains methods :public void useCom(Compute com, int one, int two), This method can be called with the passed object computer Method to complete the operation , And output the result of the operation .
(4) Design a main class Test, call UseCompute The method in useCom To complete the addition, subtraction, multiplication, and division operations .
interface Compute {
int computer(int n, int m);
}
class Add implements Compute {
@Override
public int computer(int n, int m) {
// TODO Auto-generated method stub
return n + m;
}
}
class Sub implements Compute {
@Override
public int computer(int n, int m) {
// TODO Auto-generated method stub
return n - m;
}
}
class Mul implements Compute {
@Override
public int computer(int n, int m) {
// TODO Auto-generated method stub
return n * m;
}
}
class Div implements Compute {
@Override
public int computer(int n, int m) {
// TODO Auto-generated method stub
return n / m;
}
}
class UseCompute {
public void useCom(Compute com, int one, int two) {
System.out.println(com.computer(one, two));
}
}
public class Test {
public static void main(String[] args) {
UseCompute uc = new UseCompute();
uc.useCom(new Add(), 4, 2);
uc.useCom(new Sub(), 4, 2);
uc.useCom(new Mul(), 4, 2);
uc.useCom(new Div(), 4, 2);
}
}
边栏推荐
- eslint 简单配置
- JSBridge
- One case of SQL performance degradation caused by modifying implicit parameters
- 2020-12-20
- Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
- three.js模拟驾驶游览艺术展厅---打造超级相机控制器
- Reddit's discussion on lamda model: it is not stateless. It adopts a dual process. Compared with the way it edits Wikipedia, it doesn't matter whether it has feelings or not
- 2021-04-16
- 剑指 Offer 05. 替换空格
- 【Kubernetes 系列】Kubernetes 概述
猜你喜欢

2021-03-06

2021-08-21

What are the indicators, dimensions and models in Business Intelligence BI data warehouse?

ArcGIS应用(二十)Arcgis 栅格图像符号系统提示“This dataset does not have valid histogram required for classificati…”

2021-05-02

2021-04-05

MySQL master-slave synchronization and its basic process of database and table division

Plan and change of continuous repair
![A group of K overturned linked lists [disassembly / overturning / assembly of linked lists]](/img/70/fb783172fa65763f031e6bd945cbd9.png)
A group of K overturned linked lists [disassembly / overturning / assembly of linked lists]

Why is yuancosmos so popular? Is the 10trillion yuan shouted by the market boasting or the truth?
随机推荐
2021-04-05
node-fetch下载文件
Case 2 of SQL performance degradation caused by modifying implicit parameters
2020-12-20
C sqlsugar, hisql, FreeSQL ORM framework all-round performance test vs. sqlserver performance test
剑指 Offer 07. 重建二叉树
[redisson] source code analysis of multilock
【Kubernetes 系列】Kubernetes 概述
2021-03-06
14. 最长公共前缀
2021-01-29
保证数据库和缓存的一致性
Spark SQL Generic Load/Save Functions(2.4.3)
c语言---17 函数简介
为 localStorage 添加过期时间
MySQL multi table operation
A group of K overturned linked lists [disassembly / overturning / assembly of linked lists]
js图片分辨率压缩
Zynq ultrascale + rfsoc zcu111 RF clock tree learning 1
A case of misuse of append