『百日百题 · 基础篇』备战面试,坚持刷题 第九话——接口与Java关键字!

本专栏『百日百题』长期更新,一起加入本刷题计划,一起成长吧!


前言

怎么样刷题?

有些同学喜欢上来就是干,上来就是终极难度的题目,觉得自己只要做出最难的,其它的就迎刃而解了。这种急于求成的思想要不得。

算法训练是一个系统工程,需要循序渐进,太过于急功近利,反而容易因做不出难题而产生挫败感,带来反效果。

合理的做法是循序渐进。

我的建议是先刷一遍基础语法题,打好基础在进一步刷算法题,这样首先对这个语言的基础语法和常用API是绝对没有问题的,在后面刷算法题的时候也肯定会事半功倍的!

本专栏文章即将带你从基础语法到高级算法,循序渐进持续练习,加入刷题计划一起加油吧!


JAVA25 实现抽象方法【接口】

题目:
在这里插入图片描述
题解:

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 {
    
    
    public int sum() {
    
    
       return super.getX() + super.getY();
    }
    
    public int avg() {
    
    
        return sum() / 2;
    }

}

JAVA26 实现接口【接口】

题目:
在这里插入图片描述
题解:

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

class ComparatorImpl implements Comparator {
    
    
    @Override
    public int max(int x,int y){
    
    
        return x > y ? x : y;
    }
}

JAVA27 重写父类方法【Java关键字】

题目:
在这里插入图片描述

题解:

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

    public int getX(){
    
     //子类重写getX
        return super.getX() * 10;
    }    
}

JAVA28 创建单例对象【Java关键字】

题目:
在这里插入图片描述
题解:

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 = new Singleton();
    
    private Singleton() {
    
    
    }
    public static Singleton getInstance(){
    
    
        return instance;
    }

}

结语

刷题这件事,大家一定要认真起来,不可懈怠!

日积月累,方成大器!

猜你喜欢

转载自blog.csdn.net/apple_51673523/article/details/126732284