JAVA面试题解惑系列(三)——变量(属性)的覆盖
我们来看看这么一道题:
1. class ParentClass {2. public int i = 10;3. }4.5. public class SubClass extends ParentClass {6. public int i = 30;7.8. public static void main(String[] args) {9. ParentClass parentClass = new SubClass();10. SubClass subClass = new SubClass();11. System.out.println(parentClass.i + subClass.i);12. }13. }
1. class ParentClass {2. private String privateField = "父类变量--private";3.4. /* friendly */String friendlyField = "父类变量--friendly";5.6. protected String protectedField = "父类变量--protected";7.8. public String publicField = "父类变量--public";9.10. // private的变量无法直接访问,因此我们给他增加了一个访问方法11. public String getPrivateFieldValue() {12. return privateField;13. }14. }15.16. public class SubClass extends ParentClass {17. private String privateField = "子类变量--private";18.19. /* friendly */String friendlyField = "子类变量--friendly";20.21. protected String protectedField = "子类变量--protected";22.23. public String publicField = "子类变量--public";24.25. // private的变量无法直接访问,因此我们给他增加了一个访问方法26. public String getPrivateFieldValue() {27. return privateField;28. }29.30. public static void main(String[] args) {31. // 为了便于查阅,我们统一按照private、friendly、protected、public的顺序32. // 输出下列三种情况中变量的值33.34. // ParentClass类型,ParentClass对象35. ParentClass parentClass = new ParentClass();36. System.out.println("ParentClass parentClass = newParentClass();");37. System.out.println(parentClass.getPrivateFieldValue());38. System.out.println(parentClass.friendlyField);39. System.out.println(parentClass.protectedField);40. System.out.println(parentClass.publicField);41.42. System.out.println();43.44. // ParentClass类型,SubClass对象45. ParentClass subClass = new SubClass();46. System.out.println("ParentClass subClass = newSubClass();");47. System.out.println(subClass.getPrivateFieldValue());48. System.out.println(subClass.friendlyField);49. System.out.println(subClass.protectedField);50. System.out.println(subClass.publicField);51.52. System.out.println();53.54. // SubClass类型,SubClass对象55. SubClass subClazz = new SubClass();56. System.out.println("SubClass subClazz = new SubClass();");57. System.out.println(subClazz.getPrivateFieldValue());58. System.out.println(subClazz.friendlyField);59. System.out.println(subClazz.protectedField);60. System.out.println(subClazz.publicField);61. }62. }63.
1. class ParentClass {2. /* friendly */String field = "父类变量";3. }4.5. public class SubClass extends ParentClass {6. protected String field = "子类变量";7.8. public static void main(String[] args) {9. SubClass subClass = new SubClass();10. System.out.println(subClass.field);11. }12. }13. class ParentClass {14. /* friendly */String field = "父类变量";15. }16. public class SubClass extends ParentClass {17. protected String field = "子类变量";18. public static void main(String[] args) {19. SubClass subClass = new SubClass();20. System.out.println(subClass.field);21. }22. }
1. class ParentClass {2. public String field = "父类变量";3. }4.5. public class SubClass extends ParentClass {6. protected String field = "子类变量";7.8. public static void main(String[] args) {9. SubClass subClass = new SubClass();10. System.out.println(subClass.field);11. }12. }13. class ParentClass {14. public String field = "父类变量";15. }16. public class SubClass extends ParentClass {17. protected String field = "子类变量";18. public static void main(String[] args) {19. SubClass subClass = new SubClass();20. System.out.println(subClass.field);21. }22. }
1. class ParentClass {2. public static String staticField = "父类静态变量";3.4. public final String finalField = "父类常量";5.6. public static final String staticFinalField = "父类静态常量";7. }8.9. public class SubClass extends ParentClass {10. public static String staticField = "子类静态变量";11.12. public final String finalField = "子类常量";13.14. public static final String staticFinalField = "子类静态常量";15.16. public static void main(String[] args) {17. SubClass subClass = new SubClass();18. System.out.println(SubClass.staticField);19. // 注意,这里的subClass变量,不是SubClass类20. System.out.println(subClass.finalField);21. System.out.println(SubClass.staticFinalField);22. }23. }