黑马程序员技术交流社区
标题:
新手求助,万分感激
[打印本页]
作者:
邝雄伟
时间:
2012-12-1 13:41
标题:
新手求助,万分感激
我看到毕老师第08天09(Object类toString)视频,里面讲到Object类中getClass方法中里面有一个getMethod方法,返回Method[],就是别人发给我一个.class类型文件,我怎么用这个方法查看里面的内容呢,最好有代码例子,万分感谢
我自己试了几次,都报错,class Demo2
{
public static void main(String[] args)
{
Demo d1 = new Demo (4);
//Demo d2 = new Demo (4);
//People p = new People ();
//System.out.println(d1.equals(p));
//Class c = d1.getClass();
String[] m = new String[10];
m[1]=c.getMethod();
System.out.println(m[1]);
//System.out.println(d1.toString());
//System.out.println (d1.hashCode());
//System.out.println (Integer.toHexString(d1.hashCode()));
}
}
class Demo
{
private int num;
Demo(int num)
{
this.num = num;
}
public boolean equals(Object obj)
{
if (!(obj instanceof Demo))
{
return false;
}
Demo d = (Demo) obj;
return this.num==d.num;
}
}
作者:
冯盼
时间:
2012-12-1 16:16
//打印Demo类中字段、构造方法、方法的定义
private static void getClassContent()
{
Class<Demo> demo = Demo.class;
Field[] fields = demo.getDeclaredFields();
Constructor<?>[] constructors = demo.getDeclaredConstructors();
Method[] methods =demo.getDeclaredMethods();
System.out.println("Demo中所有字段:");
for(Field field:fields)
{
System.out.println(field);
}
System.out.println("Demo中所有构造方法:");
for(Constructor<?> constructor:constructors)
{
System.out.println(constructor);
}
System.out.println("Demo中所有方法:");
for (Method method : methods)
{
System.out.println(method);
}
}
作者:
赵学刚
时间:
2012-12-1 20:36
getClass方法中里面有一个getMethod方法 为什么我看了api里面没有这个方法呢
作者:
赵学刚
时间:
2012-12-1 21:00
getMethod是Class类的,下面是个小程序,当方法私有会输出时会有异常,希望能解决你的问题:
import java.lang.reflect.Method;
import java.util.Date;
public class Program
{
public static void main(String[] args)
{
// 取得本类对象对应的类名
Program program = new Program();
Class progClass = program.getClass();
try
{
// 取得sayHello方法
Method helloMethod = progClass.getMethod(
"sayHello", null);
System.out.println("Public method found: " +
helloMethod.toString());
}
catch (NoSuchMethodException ex)
{
System.out.println("Method either doesn't exist " +
"or is not public: " + ex.toString());
}
try
{
// 取得setStr方法
Class[] args1 = new Class[1];
args1[0] = String.class;
Method strMethod = progClass.getMethod(
"setStr", args1);
System.out.println("Public method found: " +
strMethod.toString());
}
catch (NoSuchMethodException ex)
{
System.out.println("Method either doesn't exist " +
"or is not public: " + ex.toString());
}
try
{
//取得setDate方法
Class[] args2 = new Class[1];
args2[0] = Date.class;
Method dateMethod = progClass.getMethod(
"setDate", args2);
System.out.println("Public method found: " +
dateMethod.toString());
}
catch (NoSuchMethodException ex)
{
System.out.println("Method either doesn't exist " +
"or is not public: " + ex.toString());
}
try
{
// 取得 setI方法.
Class[] args3 = new Class[1];
args3[0] = Integer.TYPE;
Method iMethod = progClass.getMethod(
"setI", args3);
System.out.println("Public method found: " +
iMethod.toString());
}
catch (NoSuchMethodException ex)
{
System.out.println("Method either doesn't exist " +
"or is not public: " + ex.toString());
}
}
public String sayHello()
{
return "Hello!";
}
public void setStr(String str)
{
this.str = str;
}
private void setDate(Date date)
{
this.date = date;
}
private void setI(int i)
{
this.i = i;
}
public String str = "Hello";
private Date date = new Date();
protected int i = 0;
}
作者:
郑枫
时间:
2012-12-1 21:57
getClass() 是获取字节码。
getMethod()是获取某个类的公共成员方法
具体实现你可以看张孝祥老师讲的反射。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2