前言
最近期末复习压力太大了,
用脚写一下简单题放松一下(希望宁皇不要向我喷垃圾话)
题目
定义一个父类Base中的方法calculate(),该方法用于计算两个数的乘积(X*Y)。定义一个Base类的子类Sub,在Sub中重写calculate()方法,将计算逻辑由乘法改为除法(X/Y)。注意,当分母为0时输出“Error”。
示例1
输入:
6 2
输出:
3 12
示例2
输入:
1 0
输出: Error 0
解题思路
方法一:
- 通过父类构造方法初始化x,y的值。
- 如果y为0,直接输出"Error",并返回。否则输出x除以y的商。
package 力扣测试;
import java.util.Scanner;
public class Test1 {
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();
}
}
}
class Base {
private int x;
private 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 {
public Sub(int x, int y) {
//通过父类构造方法赋初值
super(x,y);
}
public void calculate() {
//如果除数为0,直接输出“Error”,并返回
if(super.getY()==0){
System.out.print("Error ");
return;
}
//输出x除以y的商
System.out.print(super.getX() / super.getY()+" ");
}
}
注意事项
-
this 和super关键字只能出现在方法首行
-
子类在继承父类后会默认继承父类的构造方法
- 一个类中会有一个默认的无参构造器,子类在继承父类时会默认调用父类构造器
- 当父类定义了有参构造器,子类继承时会调用父类构造方法,于是必须重写构造方法
-
当父类中同时定义了有参和无参构造器,子类无论是否定义构造器,在创建的时候都会自动调用父类无参构造器的方法
方法二:
- 不依赖于给定的部分代码
- 通过在类中定义get/set 方法初始化x,y的值
- 如果y为0,直接输出"Error",并返回。否则输出x除以y的商。
public class Test1 {
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();
Base base = new Base();
sub.setX(x);
sub.setY(y);
base.setX(x);
base.setY(y);
sub.calculate();
base.calculate();
}
}
}
class Base {
private int x;
private int y;
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
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 {
public void calculate() {
//如果除数为0,直接输出“Error”,并返回
if(super.getY()==0){
System.out.print("Error ");
return;
}
//输出x除以y的商
System.out.print(super.getX() / super.getY()+" ");
}
}
方法三:
- 不依赖于给定的部分被代码
- 通过反射获取实例对初始化x,y的值,并调用公共的public方法
- 获取当前类的Class对象(三种方式)
- 通过Class对象获取到构造器
- 通过反射获得的构造器创建实例对象
- 通过反射类获得公有或私有方法,最后调用invoke执行
public class Test1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
int x = scanner.nextInt();
int y = scanner.nextInt();
Class subClass = Sub.class;
Class baseClass = Base.class;
try {
Constructor baseconstructor = baseClass.getConstructor(int.class,int.class);
Base base = (Base) baseconstructor.newInstance(x,y);
Method declaredMethod1 = subClass.getDeclaredMethod("calculate");
Constructor constructor = subClass.getConstructor(int.class,int.class);
Sub sub1 = (Sub)constructor.newInstance(x, y);
Method declaredMethod2 = baseClass.getDeclaredMethod("calculate");
declaredMethod1.invoke(sub1);
declaredMethod2.invoke(base);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
class Base {
private int x;
private 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 {
public Sub(int x, int y) {
super(x,y);
//通过父类构造方法赋初值
}
public void calculate() {
//如果除数为0,直接输出“Error”,并返回
if(super.getY()==0){
System.out.print("Error ");
return;
}
//输出x除以y的商
System.out.print(super.getX() / super.getY()+" ");
}
}
拓展: 如果我们定义的calculate()是private的,那么我们就可以通过设置设置Accessible属性来实现对私有方法的访问
public class Test1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
int x = scanner.nextInt();
int y = scanner.nextInt();
Class subClass = Sub.class;
Class baseClass = Base.class;
try {
Constructor baseconstructor = baseClass.getConstructor(int.class,int.class);
Base base = (Base) baseconstructor.newInstance(x,y);
Method declaredMethod1 = subClass.getDeclaredMethod("calculate");
Constructor constructor = subClass.getConstructor(int.class,int.class);
Sub sub1 = (Sub)constructor.newInstance(x, y);
Method declaredMethod2 = baseClass.getDeclaredMethod("calculate");
// 设置访问的权限
declaredMethod1.setAccessible(true);
declaredMethod1.invoke(sub1);
declaredMethod2.setAccessible(true);
declaredMethod2.invoke(base);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
class Base {
private int x;
private int y;
public Base(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
private void calculate() {
System.out.println(getX() * getY());
}
}
class Sub extends Base {
public Sub(int x, int y) {
super(x,y);
//通过父类构造方法赋初值
}
private void calculate() {
//如果除数为0,直接输出“Error”,并返回
if(super.getY()==0){
System.out.print("Error ");
return;
}
//输出x除以y的商
System.out.print(super.getX() / super.getY()+" ");
}
}