列表

详情


JAVA24. 类型判断

描述

父类Base有两个子类:Sub1、Sub2,现有一个方法getClassName(),要求传入Base类型的实例,在方法内部判定其具体的类型,返回的是类型的简单名字(不含包名),请补全getClassName()方法的判断逻辑。

输入描述

输入要实例化的类名:Base、Sub1、Sub2 三者之一。

输出描述

返回实例的实际类名(简化类名,不包含包的名字)。

示例1

输入:

Sub1

输出:

Sub1

示例2

输入:

Sub2

输出:

Sub2

示例3

输入:

Base

输出:

Base

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Java 解法, 执行用时: 14ms, 内存消耗: 9512KB, 提交时间: 2022-05-20

import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String className = br.readLine();
            Base obj = (Base) Class.forName(className).newInstance();
            System.out.println(getClassName(obj));
    }

    public static String getClassName(Base obj) {
        return obj.getClass().getName();
    }

}

class Base {

}

class Sub1 extends Base {

}

class Sub2 extends Base {

}

Java 解法, 执行用时: 21ms, 内存消耗: 10656KB, 提交时间: 2022-04-27

import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String className = br.readLine();
        Base obj = (Base) Class.forName(className).newInstance();
        System.out.println(getClassName(obj));
    }

    public static String getClassName(Base obj) {

//         return obj.toString().split("@")[0];
        return obj.getClass().getName();
        

    }

}

class Base {

}

class Sub1 extends Base {

}

class Sub2 extends Base {

}

Java 解法, 执行用时: 24ms, 内存消耗: 10656KB, 提交时间: 2022-02-09

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){
            return "Sub1";
        }else if(obj instanceof Sub2){
            return "Sub2";
        }else{
            return "Base";
        }
        

    }

}

class Base {

}

class Sub1 extends Base {

}

class Sub2 extends Base {

}

Java 解法, 执行用时: 25ms, 内存消耗: 10480KB, 提交时间: 2022-02-09

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){
              return "Sub1";
           }else if(obj instanceof Sub2){
               return "Sub2";
           }else{
               return "Base";
           }
        //write your code here......
        

    }

}

class Base {

}

class Sub1 extends Base {

}

class Sub2 extends Base {

}

Java 解法, 执行用时: 27ms, 内存消耗: 10504KB, 提交时间: 2021-10-17

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) {
        return obj.getClass().getName();
    }

}

class Base {

}

class Sub1 extends Base {

}

class Sub2 extends Base {

}

上一题