首页 > 编程知识 正文

java的random函数,set get方法怎么用

时间:2023-05-04 12:13:46 阅读:25724 作者:3331

method class.get method (字符串名称,Class. parameterTypes )的作用是获取对象声明的发布方法

方法的第一个参数name获取方法的名称,第二个参数parameterTypes按声明顺序标识方法的形状参数类型。

person.getClass ().getmethod )、null );

获取person对象的Speak方法。 parameterTypes为空,因为Speak方法没有参数

person.getClass ().getmethod )、String.class );

获取人员对象的run方法。 由于run方法的参数是String类型,因此parameterTypes是String.class

如果对象中方法的形状参数为int类型,则parameterTypes为int.class

我写了一个例子来理解这个方法的作用:

人员类:

package fyh.reflectDemo;

公共类人员{

私有字符串名称;

私有Int id;

公共字符串速度;

公共字符串getname

返回名称;

}

公共语音集名称(字符串名称) {

this.name=name;

}

公共插入门tid

返回id;

}

公共语音设置(Intid ) {

ID=iD;

}

公共人员(字符串名称,int ID ) {

this.name=name;

this.ID=ID;

}

公共void speak () }

system.out.println('Hello!' ' My name is ' name;

}

公共语音运行(字符串速度) {

system.out.println (Ican run ' speed ' km! ' );

}

}

testMain类:

package fyh.reflectDemo;

import java.lang.reflect.Method;

公共类测试主{

publicstaticvoidmain (字符串[ ] args ) throws Exception { )。

Person person=new Person ('端庄的石头',10001 );

person.Speak (;

Person.run('10 );

Method m1=person.getClass ().getmethod )、null );

Method m2=person.getClass ().getmethod )、String.class );

system.out.println(m1;

system.out.println(m2;

}

}

正文: https://blog.csdn.net/handsome _ fan/article/details/54846959

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。