java中父类的私有方法可以被子类继承吗

这个你可以把private,public,protected和默认这几个修饰的方法和变量搞清楚就可以了,private修饰的是不可以被继承的,只能自己内部使用,当然用反射也可以调用的到。

在一个类中如何调用另一个类的私有方法

java私有,java私有属性

//测试类
class MyTest {

public void publicMethod(Object o) {
System.out.println("调用的公共方法 " + o);
}

/**
* 类的私有方法
*/
private void privateMethod(Object o) {
System.out.println("调用了私有方法 " + o);
}

}

新建main方法

import java.lang.reflect.Method;


public class ReflectionTest {

public static void main(String args[]) throws Exception{

MyTest myTest = new MyTest();

// 调用公共方法
myTest.publicMethod("传入参数");
// 编译报错
// myTest.privateMethod();

// 获得类的私有方法
Method method = MyTest.class.getDeclaredMethod("privateMethod",Object.class);
// 开启私有访问权限
method.setAccessible(true);
method.invoke(myTest,"传入参数");

}
}

只能通过反射才能调用私有方法

定义一个盒子类box,包括三个私有变量

public class Box{ private double width; private double length; private double height; public Box(double width, double length, double height){ this.width = width; this.length = length; this.height = height; } public void showBox(){ System.out.printf("width=%f; length=%f; height=%f", width, length, height); } }