# 重要概念

## 有理数类的代码。

package number;

public class Rationalnumber {
private double number = 0;
private long denominator = 1;

public double getNumber() {
return number;
}

public void setNumber(long number) {
this.number = number;
}

public void setDenominator(long denominator) {
this.denominator = denominator;
}

public double getNumerator() {
return number;
}

public double getDenominator() {
return denominator;
}

//字符串输出
@Override
public String toString() {
return "Rationalnumber [number=" + number + "]";
}

//对象单个加法
this.number += number;
}

//对象单个减法
public void subtract(double number) {
this.number -= number;
}

//对象单个乘法
public void multiply(double number) {
this.number *= number;
}

//对象单个除法
public void divide(double number) {
if (number == 0) {
System.out.println("false,you can't divide by 0");
} else {
this.number /= number;
}
}

//类两个相加方法
public static Rationalnumber twoadd(long v1, long v2) {
Rationalnumber b1 = new Rationalnumber();
b1.setNumber(v1);
Rationalnumber b2 = new Rationalnumber();
b2.setNumber(v2);
return b1;
}

//类两个相减方法
public static Rationalnumber twosubtract(long v1, long v2) {
Rationalnumber b1 = new Rationalnumber();
b1.setNumber(v1);
Rationalnumber b2 = new Rationalnumber();
b2.setNumber(v2);
b1.subtract(b2.number);
return b1;
}

//类两个相乘方法
public static Rationalnumber twomultiply(long v1, long v2) {
Rationalnumber b1 = new Rationalnumber();
b1.setNumber(v1);
Rationalnumber b2 = new Rationalnumber();
b2.setNumber(v2);
b1.multiply(b2.number);
return b1;
}

//类两个相除方法
public static Rationalnumber twodivide(long v1, long v2) {
Rationalnumber b1 = new Rationalnumber();
b1.setNumber(v1);
Rationalnumber b2 = new Rationalnumber();
b2.setNumber(v2);
b1.divide(b2.number);
return b1;
}

//对象属性int型转化
public int intValue() {
return (int) doubleValue();
}

//对象属性long型转化
public long longValue() {
return (long) doubleValue();
}

//对象属性float型转化
public float floatValue() {
return (float) doubleValue();
}

//对象属性double型转化
public double doubleValue() {
return number * 1.0 / denominator;
}

//按一定的小数点位数输出对象属性
public void round(int dec) {
System.out.println(String.format("%."+dec+"f", number));
}

//判断对象属性与其他数值是否相等
public boolean equals(double number1) {
if ((number - number1) == 0)
return true;
else
return false;
}

//类内部求最小公约数
private static long gcd(double n, double d) {
double n1 = Math.abs(n);
double n2 = Math.abs(d);
int gcd = 1;

for (int k = 1; k <= n1 && k <= n2; k++) {
if (n1 % k == 0 && n2 % k == 0)
gcd = k;
}
return gcd;
}
}


### 给出你的测试代码。要在与有理数类不同包的其他类中调用有理数类

package java201921123090;

import number.Rationalnumber;

public class Main {

public static void main(String[] args) {
Rationalnumber number1 = new Rationalnumber();

number1.setNumber(2);
System.out.println(number1);

System.out.println(number1);

number1.subtract(4);
System.out.println(number1);

number1.multiply(3);
System.out.println(number1);

number1.divide(2);
System.out.println(number1);
System.out.println(number1.intValue());
System.out.println(number1.longValue());
System.out.println(number1.floatValue());
System.out.println(number1.doubleValue());

System.out.println(number1);

number1=Rationalnumber.twosubtract(10, 1);
System.out.println(number1);

number1=Rationalnumber.twomultiply(12, 15);
System.out.println(number1);

number1=Rationalnumber.twodivide(3, 2);
System.out.println(number1);

number1.round(2);
System.out.println(number1.equals(2));

}

}


# 疑难问题及解决方案

### 尝试描述怎么与c语言的有理数代码相比较,为什么你设计的类更加面向对象?

1. C语言有理数代码采用函数（或过程）来描述对数据的操作，但又将函数与其操作的数据分离开来；而java设计的类是将数据和对数据的操作封装在一起，作为一个整体来处理。 可以更好的使用对象的属性和方法，不易出错。
2. C语言是将函数分开来，而类是将函数与属性放在一起，面向对象，使得更加具有稳定性，更易于维护。
3. 面向过程程序的控制流程由程序中预定顺序来决定；面向对象程序的控制流程由运行时各种事件的实际发生来触发，而不再由预定顺序来决定，更加准确。

### 有理数类的public方法是否设置合适?为什么有的方法设置为private?

posted @ 2020-10-04 07:49  违久（丁鸿沛）  阅读(164)  评论(0编辑  收藏  举报