1. 程式人生 > 其它 >java基礎題(4)

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;
    } 

}