1 package test_7_4; 2 3 public class Arithmetic { 4 5 /** 6 * 自定义四则运算 7 */ 8 9 private double x; 10 private double y; 11 12 public Arithmetic(double x, double y) { 13 14 this.x = x; 15 this.y = y; 16 } 17 18 public void plus() { 19 20 System.out.println(this.x + " + " + this.y + " = " + (this.x + this.y)); 21 } 22 23 public void subtract() { 24 25 System.out.println(this.x + " - " + this.y + " = " + (this.x - this.y)); 26 } 27 28 public void multipy() { 29 30 System.out.println(this.x + " * " + this.y + " = " + (this.x * this.y)); 31 } 32 33 public void divide() { 34 35 System.out.println(this.x + " / " + this.y + " = " + (this.x / this.y)); 36 } 37 38 }
1 package test_7_4; 2 3 public class Test { 4 5 public static void main(String[] args) { 6 7 Arithmetic arithmetic = new Arithmetic(10, 4); 8 9 arithmetic.plus(); 10 arithmetic.subtract(); 11 arithmetic.multipy(); 12 arithmetic.divide(); 13 14 } 15 16 }
结果如下:
10.0 + 4.0 = 14.0
10.0 - 4.0 = 6.0
10.0 * 4.0 = 40.0
10.0 / 4.0 = 2.5
[20-05-02][Self-test 34]Java Arithmetic
原文:https://www.cnblogs.com/mirai3usi9/p/12818994.html