使用反射生成類的set方法

其中GetSet爲類名

public class CreateGetSetFromClass
{
	public static void main(String args[])
	{
		try
		{
			Class<GetSet> cls=GetSet.class;
			Method[] fs=cls.getMethods();
			for (Method f : fs)
			{
				String mn=f.getName();
				if(mn.startsWith("set"))
				{
					System.out.println("obj."+mn+"(s."+mn.replace("set", "get")+"());");
					//System.out.println("info."+mn+"(\"\");");
				}
			}
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章