forked from FIT-DNU/Object-Oriented-Programming-with-Java
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOverloadSum.java
More file actions
27 lines (27 loc) · 756 Bytes
/
OverloadSum.java
File metadata and controls
27 lines (27 loc) · 756 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package dahinh;
class MathUtil {
// Cộng 2 số nguyên
public int sum(int a, int b) {
return a + b;
}
// Cộng 2 số thực
public double sum(double a, double b) {
return a + b;
}
// Cộng nhiều số nguyên (varargs)
public int sum(int... values) {
int s = 0;
for (int v : values) {
s += v;
}
return s;
}
}
public class OverloadSum {
public static void main(String[] args) {
MathUtil m = new MathUtil();
System.out.println("sum(int, int): " + m.sum(3, 4));
System.out.println("sum(double, double): " + m.sum(2.5, 3.5));
System.out.println("sum(int...): " + m.sum(1, 2, 3, 4, 5));
}
}