Java Tutorial/Reflection/Super Class

Материал из Java эксперт
Перейти к: навигация, поиск

Get all methods including the inherited method. Using the getMethods(), we can only access public methods.

   <source lang="java">

import java.lang.reflect.Method; class GetMethods {

 public int add(int numberA, int numberB) {
   return numberA + numberB;
 }
 protected int multiply(int numberA, int numberB) {
   return numberA * numberB;
 }
 private double div(int numberA, int numberB) {
   return numberA / numberB;
 }

} public class Main {

 public static void main(String[] args) throws Exception {
   GetMethods object = new GetMethods();
   Class clazz = object.getClass();
   Method[] methods = clazz.getMethods();
   for (Method method : methods) {
     System.out.println("Method name        = " + method.getName());
     System.out.println("Method return type = " + method.getReturnType().getName());
     Class[] paramTypes = method.getParameterTypes();
     for (Class c : paramTypes) {
       System.out.println("Param type         = " + c.getName());
     }
   }
   Method method = clazz.getMethod("add", new Class[] { int.class, int.class });
   System.out.println("Method name: " + method.getName());
 }

}</source>





Get super class of an object

   <source lang="java">

public class Main {

 public static void main(String[] args) {
   Object o = new String("Hello");
   Class clazz = o.getClass().getSuperclass();
   System.out.println("Super Class = " + clazz);
   o = new StringIndexOutOfBoundsException("Error message");
   clazz = o.getClass().getSuperclass();
   System.out.println("Super Class = " + clazz);
 }

}</source>





Getting the Superclass of an Object

   <source lang="java">

public class Main {

 public static void main(String[] argv) throws Exception {
   Object o = new String();
   Class sup = o.getClass().getSuperclass(); // java.lang.Object
 }

}</source>





Is Inheritable

   <source lang="java">

// //$Id: IntrospectionUtil.java 1540 2007-01-19 12:24:10Z janb $ //Copyright 2006 Mort Bay Consulting Pty. Ltd. //------------------------------------------------------------------------ //Licensed under the Apache License, Version 2.0 (the "License"); //you may not use this file except in compliance with the License. //You may obtain a copy of the License at //http://www.apache.org/licenses/LICENSE-2.0 //Unless required by applicable law or agreed to in writing, software //distributed under the License is distributed on an "AS IS" BASIS, //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //See the License for the specific language governing permissions and //limitations under the License. //

import java.lang.reflect.Member; import java.lang.reflect.Modifier; public class Utils {

 public static boolean isInheritable(Package pack, Member member) {
   if (pack == null)
     return false;
   if (member == null)
     return false;
   int modifiers = member.getModifiers();
   if (Modifier.isPublic(modifiers))
     return true;
   if (Modifier.isProtected(modifiers))
     return true;
   if (!Modifier.isPrivate(modifiers) && pack.equals(member.getDeclaringClass().getPackage()))
     return true;
   return false;
 }

}</source>





Superclass of Object is null

   <source lang="java">

public class Main {

 public static void main(String[] argv) throws Exception {
   Object o = new Object();
   Class sup = o.getClass().getSuperclass(); // null
 }

}</source>





The superclass of primitive types is always null

   <source lang="java">

public class Main {

 public static void main(String[] argv) throws Exception {
   Class cls = int.class;
   Class sup = cls.getSuperclass(); // null
 }

}</source>