java基础题(4)
5.4接口和抽象类
5.4.1实现抽象方法
描述:
已知抽象类Base中定义了calculate方法,该方法的计算过程依赖于sum()和avg(),而后两个方法均为抽象方法。要求定义Base的子类Sub类,并实现父类的抽象方法,使得main函数中的运算逻辑得以正确执行。
输入描述:
两个整数
输出描述:
两个整数的和除以两个整数的平均值(平均值为int类型,不考虑小数问题)
import java.util.Scanner; public class Main { public static void main(String[] args) { // Sub是需要你定义的子类 Base base = new Sub(); Scanner scanner = new Scanner(System.in); while (scanner.hasNextInt()) { int x = scanner.nextInt(); int y = scanner.nextInt(); base.setX(x); base.setY(y); System.out.println(base.calculate()); } } } abstract class Base { private int x; private int y; public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public int calculate() { if (avg() == 0) { return 0; } else { return sum() / avg(); } } /** * 返回x和y的和 */ public abstract int sum(); /** * 返回x和y的平均值 */ public abstract int avg(); } class Sub extends Base { //write your code here...... public int sum(){ return super.getX() + super.getY(); } public int avg(){ return sum() / 2; } }
5.4.2实现接口
描述
已知接口Comparator,内部定义了max函数,用于返回两个整数中的最大值。请定义该接口的实现类,使得main方法中的比较逻辑可以正确执行,要求实现类的名称为ComparatorImpl。
输入描述:
两个整数
输出描述:
两个整数中的最大值
import java.util.Scanner; public class Main { public static void main(String[] args) { Comparator comparator = new ComparatorImpl(); Scanner scanner = new Scanner(System.in); while (scanner.hasNextInt()) { int x = scanner.nextInt(); int y = scanner.nextInt(); System.out.println(comparator.max(x, y)); } } } interface Comparator { /** * 返回两个整数中的最大值 */ int max(int x, int y); } //write your code here......
//继承关系中,子类中对父类的方法进行了重写@Override
class ComparatorImpl implements Comparator { @Override public int max(int x,int y){ return x > y ? x : y; } }
5.5final和static关键字
5.5.1 重写父类方法
描述
父类Base中定义了若干get方法,以及一个sum方法,sum方法是对一组数字的求和。请在子类 Sub 中重写 getX() 方法,使得 sum 方法返回结果为 x*10+y
输入描述:
整数
输出描述:
整数的和
示例1
输入:1 2;
输出:12;
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); System.out.println(sub.sum()); } } } 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 final int getY() { return y; } public final int sum() { return getX() + getY(); } } class Sub extends Base { public Sub(int x, int y) { super(x, y); } //write your code here...... @Override public int getX(){ return (super.getX())*10; } }
5.5.2创建单例对象
描述
Singleton类是单例的,每次调用该类的getInstance()方法都将得到相同的实例,目前该类中这个方法尚未完成,请将其补充完整,使得main()函数中的判断返回真(不考虑线程安全)。
输入描述:
无
输出描述:
true
public class Main { public static void main(String[] args) { Singleton s1 = Singleton.getInstance(); Singleton s2 = Singleton.getInstance(); System.out.println(s1 == s2); } } class Singleton { private static Singleton instance; private Singleton() { } //write your code here...... public static Singleton getInstance(){ //如果为空,才进行实例化,保证一个类只有一个实例,存在线程安全的问题 if(instance==null){ instance=new Singleton(); } return instance; } }