我想在我的Java程序中列出Vector
类的所有实例方法。我想知道在Java中是否有与Ruby的instance_methods
等效的方法。
如果有其他方法,请告诉我。
答案 0 :(得分:4)
要获取类的所有公共非静态方法(包括从基类继承的方法),请执行以下操作:
for (Method method : Vector.class.getMethods())
if ((method.getModifiers() & Modifier.STATIC) == 0)
System.out.println(method);
输出(Java 1.8.0_65)
public synchronized boolean java.util.Vector.add(java.lang.Object)
public void java.util.Vector.add(int,java.lang.Object)
public synchronized java.lang.Object java.util.Vector.remove(int)
public boolean java.util.Vector.remove(java.lang.Object)
public synchronized java.lang.Object java.util.Vector.get(int)
public synchronized boolean java.util.Vector.equals(java.lang.Object)
public synchronized java.lang.String java.util.Vector.toString()
public synchronized int java.util.Vector.hashCode()
public synchronized java.lang.Object java.util.Vector.clone()
public synchronized int java.util.Vector.indexOf(java.lang.Object,int)
public int java.util.Vector.indexOf(java.lang.Object)
public void java.util.Vector.clear()
public boolean java.util.Vector.contains(java.lang.Object)
public synchronized boolean java.util.Vector.isEmpty()
public synchronized java.util.Iterator java.util.Vector.iterator()
public synchronized int java.util.Vector.lastIndexOf(java.lang.Object,int)
public synchronized int java.util.Vector.lastIndexOf(java.lang.Object)
public synchronized void java.util.Vector.replaceAll(java.util.function.UnaryOperator)
public synchronized int java.util.Vector.size()
public synchronized java.util.List java.util.Vector.subList(int,int)
public synchronized java.lang.Object[] java.util.Vector.toArray()
public synchronized java.lang.Object[] java.util.Vector.toArray(java.lang.Object[])
public java.util.Spliterator java.util.Vector.spliterator()
public synchronized boolean java.util.Vector.addAll(int,java.util.Collection)
public synchronized boolean java.util.Vector.addAll(java.util.Collection)
public synchronized void java.util.Vector.addElement(java.lang.Object)
public synchronized java.lang.Object java.util.Vector.elementAt(int)
public java.util.Enumeration java.util.Vector.elements()
public synchronized void java.util.Vector.forEach(java.util.function.Consumer)
public synchronized java.lang.Object java.util.Vector.set(int,java.lang.Object)
public synchronized int java.util.Vector.capacity()
public synchronized void java.util.Vector.ensureCapacity(int)
public synchronized void java.util.Vector.trimToSize()
public synchronized void java.util.Vector.copyInto(java.lang.Object[])
public synchronized boolean java.util.Vector.containsAll(java.util.Collection)
public synchronized java.lang.Object java.util.Vector.firstElement()
public synchronized void java.util.Vector.insertElementAt(java.lang.Object,int)
public synchronized java.lang.Object java.util.Vector.lastElement()
public synchronized java.util.ListIterator java.util.Vector.listIterator()
public synchronized java.util.ListIterator java.util.Vector.listIterator(int)
public synchronized boolean java.util.Vector.removeAll(java.util.Collection)
public synchronized void java.util.Vector.removeAllElements()
public synchronized boolean java.util.Vector.removeElement(java.lang.Object)
public synchronized void java.util.Vector.removeElementAt(int)
public synchronized boolean java.util.Vector.removeIf(java.util.function.Predicate)
public synchronized boolean java.util.Vector.retainAll(java.util.Collection)
public synchronized void java.util.Vector.setElementAt(java.lang.Object,int)
public synchronized void java.util.Vector.setSize(int)
public synchronized void java.util.Vector.sort(java.util.Comparator)
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
public default java.util.stream.Stream java.util.Collection.stream()
public default java.util.stream.Stream java.util.Collection.parallelStream()
答案 1 :(得分:2)
如果我理解正确,你想得到一个班级中所有方法的清单吗?
Class getMethodsFromClass = ob.getClass();
for (Method method : getMethodsFromClass.getDeclaredMethods()) {
if (method.getAnnotation(PostConstruct.class) != null) {
System.out.println(method.getName());
}
}