关键字: java 面试题 继承 变量的覆盖 属性
class ParentClass { public int i = 10; } public class SubClass extends ParentClass { public int i = 30; public static void main(String[] args) { ParentClass parentClass = new SubClass(); SubClass subClass = new SubClass(); System.out.println(parentClass.i + subClass.i); } }
class ParentClass { private String privateField = "父类变量--private"; /* friendly */String friendlyField = "父类变量--friendly"; protected String protectedField = "父类变量--protected"; public String publicField = "父类变量--public"; // private的变量无法直接访问,因此我们给他增加了一个访问方法 public String getPrivateFieldValue() { return privateField; } } public class SubClass extends ParentClass { private String privateField = "子类变量--private"; /* friendly */String friendlyField = "子类变量--friendly"; protected String protectedField = "子类变量--protected"; public String publicField = "子类变量--public"; // private的变量无法直接访问,因此我们给他增加了一个访问方法 public String getPrivateFieldValue() { return privateField; } public static void main(String[] args) { // 为了便于查阅,我们统一按照private、friendly、protected、public的顺序 // 输出下列三种情况中变量的值 // ParentClass类型,ParentClass对象 ParentClass parentClass = new ParentClass(); System.out.println("ParentClass parentClass = new ParentClass();"); System.out.println(parentClass.getPrivateFieldValue()); System.out.println(parentClass.friendlyField); System.out.println(parentClass.protectedField); System.out.println(parentClass.publicField); System.out.println(); // ParentClass类型,SubClass对象 ParentClass subClass = new SubClass(); System.out.println("ParentClass subClass = new SubClass();"); System.out.println(subClass.getPrivateFieldValue()); System.out.println(subClass.friendlyField); System.out.println(subClass.protectedField); System.out.println(subClass.publicField); System.out.println(); // SubClass类型,SubClass对象 SubClass subClazz = new SubClass(); System.out.println("SubClass subClazz = new SubClass();"); System.out.println(subClazz.getPrivateFieldValue()); System.out.println(subClazz.friendlyField); System.out.println(subClazz.protectedField); System.out.println(subClazz.publicField); } }
class ParentClass { /* friendly */String field = "父类变量"; } public class SubClass extends ParentClass { protected String field = "子类变量"; public static void main(String[] args) { SubClass subClass = new SubClass(); System.out.println(subClass.field); } }
class ParentClass { public String field = "父类变量"; } public class SubClass extends ParentClass { protected String field = "子类变量"; public static void main(String[] args) { SubClass subClass = new SubClass(); System.out.println(subClass.field); } }
class ParentClass { public static String staticField = "父类静态变量"; public final String finalField = "父类常量"; public static final String staticFinalField = "父类静态常量"; } public class SubClass extends ParentClass { public static String staticField = "子类静态变量"; public final String finalField = "子类常量"; public static final String staticFinalField = "子类静态常量"; public static void main(String[] args) { SubClass subClass = new SubClass(); System.out.println(SubClass.staticField); // 注意,这里的subClass变量,不是SubClass类 System.out.println(subClass.finalField); System.out.println(SubClass.staticFinalField); } }