面向对象作业
因为要求类中属性私有,用get()函数获取私有变量。
public class Base {
private final int x;
private final 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());
}
}
public class Sub extends Base {
public Sub (int x, int y) {
super(x, y);
}
@Override
public void calculate ()
{
if (super.getY()==0)
System.out.print("Error");
else
System.out.print(super.getX()/super.getY());
System.out.print(" ");
}
}
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);
Base base = new Base(x,y);
sub.calculate();
base.calculate();
}
}
}