这个问题我用反射来证明下首先来一段子父类代码
- package Try;
- public class Student extends Person {
- String name = "aa";
- }
- class Person {
-
- private int age =5;
-
- }
复制代码 然后用反射来看看Student里有没有被private修饰的age,以下代码有用到junit来调试。顺便就是练习反射了:lol
- package Try;
- import java.lang.reflect.Field;
- import org.junit.Test;
- public class Try {
- @Test
- public void test() throws Exception{
-
- Student st = new Student();
-
- Class clazz = Class.forName("Try.Student");
-
- Field fi =clazz.getDeclaredField("age");
-
- fi.setAccessible(true);
-
- int age = (Integer)fi.get(st);
-
- System.out.println(age);
-
- }
- }
复制代码 运行的时候就会抛出异常:java.lang.NoSuchFieldException: age
这里就可以看出子类没有继承父类的age
|