小编Gil*_*Gil的帖子

可以覆盖引用Java中的数据类型吗?

我相当确定overLY仅指的是方法.它是否也可以引用数据类型?我知道在Derived的一个实例中,x将= 1而不是0.但这是否被认为是最重要的?

class Base {    
    int x = 0 ; 
}

class Derived extends Base {    
    int x = 1 ;     
}
Run Code Online (Sandbox Code Playgroud)

java overriding

2
推荐指数
1
解决办法
47
查看次数

为什么跳过我的while循环?

我认为我的程序正在跳过我的while循环,但老实说我不确定究竟发生了什么.该函数应该通过找到GCD然后将分子和分母除以该数来减少分数.

    class Rational {

private int numerator, denominator;

//Constructor
public Rational (int num, int den) {
    numerator = num;
    denominator = den;
}

//Method for multiplying fractions
public Rational times (Rational that) {
    Rational x = new Rational (this.numerator*that.numerator, this.denominator*that.denominator);
    x = x.reduce();
    return x;
}

//Method for displaying fractions as strings
public String toString() {
    return new String(numerator+"/"+denominator); 
}

//Method for adding fractions
public Rational plus(Rational that) {
    Rational x = new Rational ((this.numerator*that.denominator)+(that.numerator*this.denominator), 
            this.denominator*that.denominator);
    //x = x.reduce();
    return …
Run Code Online (Sandbox Code Playgroud)

java while-loop

0
推荐指数
1
解决办法
500
查看次数

标签 统计

java ×2

overriding ×1

while-loop ×1