Ⅰ 用java怎么编写一个类调用另一个类的私有方法
私有属性可以通过get方法调用,但是私有方法不能被另一个类调用。。而且,正常情况下,都是数据私有化,行为公开化,没有人会写私有方法的
Ⅱ java中, 怎么调用别的类的私有方法
利用反射即可调用;
简易实例代码如下:
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @author thomaslwq
* @version 创建时间:Sep 4, 2012 9:53:49 PM
* 类说明
*/
public class ReflectionTest {
public static void setObjectColor(Object obj) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAcces***ception, InvocationTargetException{
Class cls = obj.getClass();
//获得类的私有方法
Method method = cls.getDeclaredMethod("privateMethod", null);
method.setAccessible(true); //没有设置就会报错
//调用该方法
method.invoke(obj, null);
}
public static void main(String args[]) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAcces***ception, InvocationTargetException{
setObjectColor(new MyTest());
}
}
//测试类
class MyTest{
public void setMyTest(){
System.out.println("setMyTest");
}
/**
类的私有方法
**/
private void privateMethod(){
System.out.println("调用了 private Method");
}
}
Ⅲ java中,怎么调用别的类的私有方法
反射(reflection)
[java] view plain
public static void main(String[] args) throws Exception {
Constructor<?> constructor = SecretTool.class.getDeclaredConstructors()[0];
constructor.setAccessible(true);
SecretTool tool = (SecretTool) constructor.newInstance(); // 得到它的一个实例
for(Method method : SecretTool.class.getDeclaredMethods()) {
method.setAccessible(true);
if(method.getName().equals("myMotto")) {
method.invoke(tool); // 调用没有返回值,无参的私有方法
} else if(method.getName().equals("calculate")) {
Integer result = (Integer)method.invoke(tool, 1,2);
System.out.println("1 + 2 = " + result.toString()); // 调用返回值为整数,且带参的私有方法
}
}
}
输出结果:
[plain] view plain
I like potato
1 + 2 = 3
Ⅳ java private方法子类可以调用吗
定义一个class,定义一个内部private方法:
public class PrivateTest {
private void print() {
System.out.println("this is a private method");
}
}
再定义一个class去访问刚才定义的private方法,也就是print()如下
import java.lang.reflect.Method;
public class PrivateTest2 {
public static void main(String[] args) {
try {
Method method = PrivateTest.class.getDeclaredMethod("print", new Class[]{});
method.setAccessible(true);
Method.invoke(new PrivateTest(), new Object[] {});
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
}
利用java的反射机制,即使是private方法,也可以被调用使用。
Ⅳ java 编程父类不能调用自己的私有方法
可以的,父类所有的东西子类都能继承,但是私有方法就是继承了,子类也不能调用。
Ⅵ java 如何调用一个私有方法里的私有成员变量
package reflection;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/*
* 该类主要练习使用java的反射机制调用其他类的
* private方法和变量;
*/
public class MethodTest
{
public static void main(String[] args) throws Exception
{
Class<?> classType = People.class;
People p1 =(People) classType.newInstance();
// 获取指定的方法,调用People类的私有方法;
Method method = classType.getDeclaredMethod("sayHello",
new Class[] { String.class });
method.setAccessible(true);//压制java的访问修饰符;
method.invoke(p1, new Object[]{"Mr zhou"});
//获取People类的私有属性;
Field field = classType.getDeclaredField("age");
field.setAccessible(true);
field.set(p1, 12);
System.out.println(field.get(p1));
}
}
class People
{
private int age;
private String name;
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
private void sayHello(String str)
{
System.out.println("Hello: " + str);
}
}
Ⅶ JAVA中重写父类方法后,这个方法如何调用父类私有属性方法
父类的私有属性和方法是不被子类继承调用的,如果你非要这么做可以
1、在父类中另外定义protected
的方法操作私有方法,子类调用该方法
2、将私有方法改为protected
3、使用反射,获取父类所有方法,根据名称调用
Ⅷ java类中方法private怎么调用
私有方法,类外部是无法调用的,解决方法,一,修改为public方法,而,在类内部再添加一个public方法,该方法去调用私有,这样就不用改动到原方法了
Ⅸ java反射机制怎样调用类的私有方法
为了一看就懂,请看下面的示例(假设调用 MyTest类的私有方法 privateMethod()):
publicclassReflectionTest{
(Objectobj)throwsSecurityException,NoSuchMethodException,IllegalArgumentException,IllegalAcces***ception,InvocationTargetException{//核心代码加粗
Classcls=obj.getClass();
//获得类的私有方法
Methodmethod=cls.getDeclaredMethod("privateMethod",null);
method.setAccessible(true);//没有设置就会报错
//调用该方法
method.invoke(obj,null);
}
publicstaticvoidmain(Stringargs[])throwsSecurityException,IllegalArgumentException,NoSuchMethodException,IllegalAcces***ception,InvocationTargetException{
setObjectColor(newMyTest());
}
}
//测试类
classMyTest{
publicvoidsetMyTest(){
System.out.println("setMyTest");
}
/**
类的私有方法
**/
privatevoidprivateMethod(){
System.out.println("调用了privateMethod");
}
}
Ⅹ java中如何调用私有变量或方法
private关键字
(1)私有的意义,可以修饰成员变量和成员方法
(2)特点:
被private修饰的后的成员只能在本类中被访问
(3)private的应用:
以后再写一个类的时候:
把所有的成员变量给private了
提供对应的getXxx()/setXxx()方法
/*
封装和private的应用:
A:把成员变量用private修饰
B:提高对应的getXxx()和setXxx()方法
*/
//定义学生类
class Student {
//姓名
private String name;
//年龄
private int age;
//姓名获取值
public String getName() {
return name;
}
//姓名设置值
public void setName(String n) {
name = n;
}
//年龄获取值
public int getAge() {
return age;
}
//年龄赋值
public void setAge(int a) {
age = a;
}
}
//测试类
class StudentTest {
public static void main(String[] args) {
//创建学生对象
Student s = new Student();
//使用成员变量
//错误:被私有修饰了,外界不能直接访问了
//System.out.println(s.name+"---"+s.age);
System.out.println(s.getName()+"---"+s.getAge());
//给成员变量赋值
//s.name = "林青霞";
//s.age = 27;
//通过方法给赋值
s.setName("林青霞");
s.setAge(27);
System.out.println(s.getName()+"---"+s.getAge());
}
}