8 回答
TA贡献1796条经验 获得超10个赞
@窗外雨,夜微寒。: 简单点你把那两个字段public出来,要么去掉那两个字段,降到方法上
class Number
{
void multiplication(float num1,float num2){
float multiplication=num1*num2;
System.out.println("N1*N2结果为:" + multiplication);
}
//..依次类推
}
外边直接当入参传入就可以了,new那么多Number实例没用。
TA贡献1895条经验 获得超7个赞
根据面向对象的设计原则,你可以这样写:
public class Number {
float value;
Number addition(Number num){
double addition=this.value+num.value;
System.out.println("N1+N2结果为:" + addition);
return this;
}
Number subtration(Number num){
double subtration=this.value-num.value;
System.out.println("N1-N2结果为:" + subtration);
return this;
}
Number multiplication(Number num){
float multiplication=this.value*num.value;
System.out.println("N1*N2结果为:" + multiplication);
return this;
}
Number division(Number num){
float division=this.value/num.value;
System.out.println("N1/N2结果为:" + division);
return this;
}
Number(float f){
this.value = f ;
}
public static void main(String[] args) {
Number number1 = new Number(4);
Number number2 = new Number(5);
number1.addition(number2)
.subtration(number2)
.division(number2)
.multiplication(number2);
}
}
添加回答
举报