"Hundred Days, Hundred Questions · Basics" prepare for the interview, insist on brushing up the eighth chapter - polymorphism and abstraction!

This column "Hundred Days and Hundred Questions" has been updated for a long time, join this question brushing plan and grow together!


JAVA23 defines the printing method [polymorphism]

topic:
insert image description here

answer:

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) throws Exception {
    
    
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
    
    
            String className = scanner.next();
            // print就是需要你定义的方法
            print(Class.forName(className).newInstance());
        }
    }
    
    public static void print(Object obj){
    
    
        System.out.println(obj.getClass().getName()); //调用getName函数直接输出
    }
}

class First {
    
    
    public String toString() {
    
    
        return "First";
    }
}

class Second {
    
    
    public String toString() {
    
    
        return "Second";
    }
}

class Third {
    
    
    public String toString() {
    
    
        return "Third";
    }
}

JAVA24 type judgment [polymorphism]

topic:
insert image description here

answer:

import java.util.Scanner;

public class Main {
    
    

    public static void main(String[] args) throws Exception {
    
    
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
    
    
            String className = scanner.next();
            Base obj = (Base) Class.forName(className).newInstance();
            System.out.println(getClassName(obj));
        }
    }

    public static String getClassName(Base obj) {
    
    
		if (obj instanceof Sub1) {
    
    
		    Sub1 sub1 = (Sub1) obj;
		    return sub1.getClass().getSimpleName();
		} else if (obj instanceof Sub2) {
    
    
		    Sub2 sub2 = (Sub2) obj;
		    return sub2.getClass().getSimpleName();
		} else {
    
    
		    return obj.getClass().getSimpleName();
		}
    }
}

class Base {
    
    

}

class Sub1 extends Base {
    
    

}

class Sub2 extends Base {
    
    

}

JAVA25 implements abstract methods [abstract classes]

Topic:
insert image description here
Solution:

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 implementation interface [abstract class]

Topic:
insert image description here
Solution:

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

Guess you like

Origin blog.csdn.net/apple_51673523/article/details/126591347