Ruang penulis
← Kembali ke jurnal
Reflection

How do I check if a class represent an interface type?

You can use the isInterface() method call of the java.lang.Class to identify if a class objects represent an interface type. package org.kodejava.lang.reflect; import java.io.Serializable; public class IsInterfaceDemo { public static void main(String[] args) { IsInterfaceDemo.get(Serializable.class); IsInterfaceDemo.get(Long.class); } private static void get(Class<?> clazz) { if (clazz.isInterface()) { System.out.println(clazz.getName() + " is an interface type."); } else […]

DWayan · 11 Des 2010 · 1 menit baca

Artikel oleh Wayan · Sumber: Kode Java ↗

Terjemahan artikel ini belum tersedia. Isi asli ditampilkan.

You can use the isInterface() method call of the java.lang.Class to identify if a class objects represent an interface type.

package org.kodejava.lang.reflect;

import java.io.Serializable;

public class IsInterfaceDemo {
    public static void main(String[] args) {
        IsInterfaceDemo.get(Serializable.class);
        IsInterfaceDemo.get(Long.class);
    }

    private static void get(Class<?> clazz) {
        if (clazz.isInterface()) {
            System.out.println(clazz.getName() +
                    " is an interface type.");
        } else {
            System.out.println(clazz.getName() +
                    " is not an interface type.");
        }
    }
}

Here is the result of the program:

java.io.Serializable is an interface type.
java.lang.Long is not an interface type.
← Jelajahi catatan lainnya