注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

淡忘邻人

二十七,始发愤

 
 
 

日志

 
 
 
 

JAVA Delegate  

2010-11-05 16:56:37|  分类: JAVA |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
// class_getmethod.jsl

import java.lang.reflect.Method;
import java.util.Date;

public class Program
{
public static void main(String[] args)
{
// Get the Class object associated with this class.
Program program = new Program();
Class progClass = program.getClass();

try
{
// Get the method named 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
{
// Get the method named 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
{
// Get the method named 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
{
// Get the method named 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 Program()
{
}

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;
}

/*
Output:
Public method found: public java.lang.String Program.sayHello()
Public method found: public void Program.setStr(java.lang.String)
Method either doesn't exist or is not public: java.lang.NoSuchMethodException: setDate
Method eith
  评论这张
 
阅读(1971)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017