Home > Java > javaTutorial > body text

How to identify data types during runtime in Java

PHP中文网
Release: 2017-06-22 11:50:18
Original
1486 people have browsed it

1. The getClasses() method of the Class object obtains all the public internal classes in the class, as well as the internal classes inherited from the parent class and parent interface. The getinterfaces() method returns all interfaces inherited by the class.

import javax.print.attribute.standard.PrinterInfo;

interface  HasBatteries{}
interface Waterproof{}
interface ShootsThings{}

class Toy{
    Toy(){}
    Toy(int i){}
}

class FancyToy extends Toy implements HasBatteries,Waterproof,ShootsThings{
    public FancyToy() {
        // TODO 自动生成的构造函数存根
        super(1);
    }
}

public class ToyTest {
    public static void main(String[] args){
        Class class1=null;
        try{
            class1=class1.forName("FancyToy");
        }catch (ClassNotFoundException e) {
            // TODO: handle exception
        }
        printInfo(class1);
        Class[] faces=class1.getInterfaces();
        for(int i=0;i<faces.length;i++){
            printInfo(faces[i]);
        }
        Class cy=class1.getSuperclass();
        Object cObject=null;
        try{
            cObject=cy.newInstance();
        }catch (Exception e) {
            // TODO: handle exception
        }
        printInfo(cObject.getClass());
        
    }
    
    static void printInfo(Class cc){
        System.out.println("Class name:"+cc.getName()+" is interface? "+cc.isInterface());
    }

}
Copy after login
Copy after login

2. When using forName() of the Class object, the classname parameter passed needs the full name. You cannot just add the class name, such as package name.class name, java.lang.String.

import javax.print.attribute.standard.PrinterInfo;

interface  HasBatteries{}
interface Waterproof{}
interface ShootsThings{}

class Toy{
    Toy(){}
    Toy(int i){}
}

class FancyToy extends Toy implements HasBatteries,Waterproof,ShootsThings{
    public FancyToy() {
        // TODO 自动生成的构造函数存根
        super(1);
    }
}

public class ToyTest {
    public static void main(String[] args){
        Class class1=null;
        try{
            class1=class1.forName("FancyToy");
        }catch (ClassNotFoundException e) {
            // TODO: handle exception
        }
        printInfo(class1);
        Class[] faces=class1.getInterfaces();
        for(int i=0;i<faces.length;i++){
            printInfo(faces[i]);
        }
        Class cy=class1.getSuperclass();
        Object cObject=null;
        try{
            cObject=cy.newInstance();
        }catch (Exception e) {
            // TODO: handle exception
        }
        printInfo(cObject.getClass());
        
    }
    
    static void printInfo(Class cc){
        System.out.println("Class name:"+cc.getName()+" is interface? "+cc.isInterface());
    }

}
Copy after login
Copy after login

The above is the detailed content of How to identify data types during runtime in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template