Android中抽象方法赋值时出现null.

首先先声明两个类

public class Person {
   .....
}

public class Student {
    public Person A;
    public Student(Person a) {
        A = a;
    }
    .....
}

MainActivity在实现抽象activity时出现空的返回值

public abstract class Main2Activity extends AppCompatActivity{
    Student B = getStudent();
    oncreate(...){
    ......
        getStudent().getA();
        //A不为null
        B.getA();
        //A为null
    }
    public abstract Student getStudent();
}

MainActivity

public class MainActivity extends Main2Activity{
    Person person = new Person(10);
    @Override
    public Student getStudent() {
        Student student = new Student(person);
        return student;
    }
}

为什么会出现成员变量A为null的情况,但是在声明周期中赋值时并不会出现这个问题.两种情况下成员变量B都不会是null,请问是我哪里理解错误了.

查看回复

相关问答