题解 | #重写计算逻辑#

重写计算逻辑

http://www.nowcoder.com/practice/af4fd657b63241a3a0613d5c487a252f

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    while (scanner.hasNextInt()) {
        int x = scanner.nextInt();
        int y = scanner.nextInt();
        Sub sub = new Sub(x,y);
        sub.calculate();
    }
}

}

class Base {

protected int x;
protected int y;

public Base(int x, int y) {
    this.x = x;
    this.y = y;
}

public int getX() {
    return x;
}

public int getY() {
    return y;
}

public void calculate() {
    System.out.println(getX() * getY());
}

}

class Sub extends Base {

//write your code here......
public Sub(int x,int y){
    super(x,y);
}

public void calculate(){
    if(y == 0){
        System.out.println("Error");
    }else{
        System.out.println(x/y);
    }
}

}

全部评论
https://blog.csdn.net/dlf123321/article/details/53411539
1 回复 分享
发布于 2022-01-25 00:58
父类的private下的私有成员变量不能被子类直接调用,改为protected可以保证在一个继承树内使用。
点赞 回复 分享
发布于 2022-01-25 01:00

相关推荐

点赞 评论 收藏
分享
Twilight_mu:经典我朋友XXXX起手,这是那种经典的不知道目前行情搁那儿胡编乱造瞎指导的中年人,不用理这种**
点赞 评论 收藏
分享
墨西哥大灰狼:如果你的校友卤馆还在的话,他肯定会给你建议的,可是卤馆注销了@ 程序员卤馆
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务