【问题标题】:Access property after inheriting from an abstract class Java从抽象类Java继承后访问属性
【发布时间】:2017-07-09 06:35:48
【问题描述】:

我无法访问作为从抽象类继承的具体类型的类的字段。

在 Java 中,我创建了一个扩展 Student 的 External student 类

 */
public class ExternalStudent extends Student  {
String currentSchool;

  public ExternalStudent(String name, Integer age, String studentIdentifier, String currentSchool) {
    super(name, age, studentIdentifier);
    this.currentSchool = currentSchool; 
  }
}

学生在哪里

public abstract class Student {

    //Attributes
    String studentIdentifier;
    Integer age;
    String name;

    //Associations
    List<Subject> subject =  new ArrayList<Subject>();
    PersonalDetails personaldetails;

    //Methods
    public void setSubject () {
        this.subject.add(new Subject("Name"));
    }

    //Constructors
    public Student(String name, Integer age, String studentIdentifier){
       this.age = age;
       this.name = name;
       this.studentIdentifier = studentIdentifier;
    }
}

外部学生由我的班级应用程序设置

public class ApplicationC {
    //Attributes
    private String personalStatement;
    private String applicationForm;

    //Associations
    Registration registration;
    Student student;
    ApplicationTest applicationtest;

    //Methods
    public void setApplicationResult(String result){
        this.applicationtest = new ApplicationTest(result);
    }

    //Constructor
    public ApplicationC (String personalStatement, String name){
        this.registration = new Registration();
        this.student = new ExternalStudent("Tom",16,"78954","DHS");
    }
}

我已经建立了一个简单的测试类

public void testPostCondition() throws ParseException{   
 ApplicationC instance = new ApplicationC("test statement","test name");
    instance.setApplicationResult("pass");    
    assertEquals("pass",instance.applicationtest.result);
         instance.student.age = 16;
     instance.student.studentIdentifier = "78954";
     instance.student.name = "Tom";
     instance.student.currentSchool = "test"; //Error as field does not exist
}

但我无法访问学生实例(必须是 externalStudent)的当前学校。如何访问此字段以测试我的代码?

【问题讨论】:

    标签: java inheritance


    【解决方案1】:

    ApplicationC 中,student 字段用Student 类声明:

    Student student;
    

    对象上可用的方法依赖于声明的类型,而不是真正实例化的对象。
    currentSchool 仅在子类ExternalStudent 中声明。
    所以,你不能通过这种方式访问​​它。

    一种解决方法是将Student 向下转换为ExternalStudent

     ((ExternalStudent)instance.student).studentIdentifier = "78954";
    

    一般来说,最好在执行之前检查实例的类型:

     if (instance.student instanceof ExternalStudent){
        ((ExternalStudent)instance.student).studentIdentifier = "78954";
     }
    

    作为一般建议,在 Java 中,您应该支持字段的 private 修饰符,如果您需要操作基类并访问特定于子类的某些字段,您可以在基类中定义一个方法返回 nullOptional 并在子类中用字段的返回覆盖它。
    它避免了可能容易出错并且通常是受孕问题症状的演员表。

    【讨论】:

    • 完美,是的,向下转换对我来说很有意义。我认为这样做没有缺点?
    • 谢谢,不胜感激。
    【解决方案2】:

    您的实例是一个 ApplicationC,
    因此,“instance.student”是“学生”。
    “学生”没有“当前学校”属性。
    到达它
    * 将“currentSchool”属性添加到“Student”

    * 将你的“instance.student”转换为“ExternalStudent”

    注意:您需要处理所有异常和强制转换等开销

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2019-01-09
      • 1970-01-01
      • 2013-11-05
      • 1970-01-01
      • 2011-03-15
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      • 1970-01-01
      相关资源
      最近更新 更多