代码如下,为什么通过super(name, age);方式为子类初始化之后,调用子类get方法name为null,age为0;不是已经初始化了吗?糊涂了!
- package com.heisejiuhuche.OOP;
- public class OOPTest {
- public static void main(String[] args) {
- Father f = new Father("lisi", 12);
- System.out.println(f.getName());
- Sub s = new Sub("zhangsan", 19);
- System.out.println(s.getAge());
- }
- }
- class Father {
- private String name;
- private int age;
- Father(String name, int age) {
- this.name = name;
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- }
- class Sub extends Father {
- private String name;
- private int age;
- Sub(String name, int age) {
- super(name, age);
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String toString() {
- return this.name + ":" + this.age;
- }
- }
复制代码
|