简体   繁体   English

为什么我不能从特定 class 继承特定变量?

[英]Why can't I inherit a specific variable from a specific class?

I want to inherit the variable "remark" from the Student class to put into the Course Class.我想从学生 class 继承变量“remark”以放入课程 Class。 So I basically done the "extends" keyword and the super(remark), but it's still not working.所以我基本上完成了“extends”关键字和super(remark),但它仍然不起作用。 Is is possible to inherit only 1 specific variable or is there another way?是否可以仅继承 1 个特定变量或是否有其他方法?

public class Course extends Student   {
    
    private String[] courseName;
    private String[] courseNo;
    private int courseCredit;
    
    
    Course(String[] courseNo,String[] courseName,int courseCredit,char[] remark) {
        super(remark);
        this.courseNo = courseNo;
        this.courseName = courseName;
        this.courseCredit = courseCredit;
        
    }
    
    public void setCourseInfo(String[] courseNo,String[] courseName, int courseCredit) { 
        this.courseNo = courseNo;
        this.courseName = courseName;
        this.courseCredit = courseCredit;
        
    }
    
    public void setcourseName(String[] courseName) {
        this.courseName = courseName;
    }
    
    public void setcourseNo(String[] courseNo) {
        this.courseNo = courseNo;
    }
    
    public void setcourseCredit(int courseCredit) {
        this.courseCredit = courseCredit;
    }
    
    public String[] getcourseName() {
        return courseName;
    }
    
    public String[] getcourseNo() {
        return courseNo;
    }
    
    public int getcourseCredit() {
        return courseCredit;
    }

public class Student extends Person  {
    
    private int sid;
    private int numberOfCourse;
    private boolean isTuitionPaid;
    private String[] course;
    private char[] remark;
    
    Student(String fname,String lname,int sid,int numberOfCourse,boolean isTuitionPaid,String[] course,char[] remark) {
        
        super (fname,lname);
        this.sid = sid;
        this.numberOfCourse = numberOfCourse;
        this.isTuitionPaid = isTuitionPaid;
        this.course = course;
        this.remark = remark;
        
    }
    
    public void setInfo(String fname,String lname,int sid,int numberOfCourse,boolean isTuitionPaid,String[] course,char[] remark) {
        
        this.getfname();
        this.getlname();
        this.sid = sid;
        this.numberOfCourse = numberOfCourse;
        this.isTuitionPaid = isTuitionPaid;
        this.course = course;
        this.remark = remark;
        
    }
    
    public void setRemark(char[] remark) {
        this.remark = remark;
    }
    
    public char[] getRemark() {
        return remark;
    }
    
    public void setStudentID(int sid) {
        this.sid = sid;
    }
    
    public void setIsTuitionPaid(boolean isTuitionPaid) {
        this.isTuitionPaid = isTuitionPaid;
    }
    
    public void setNumberOfCourses(int numberOfCourse) {
        this.numberOfCourse = numberOfCourse;
    }
    
    public void setCoursesEnrolled(String[] courses,char[] remark) {
        this.course = courses;
        this.remark = remark;
    }
    
    public int getStudentID() {
        return sid;
    }
    
    public int getNumberOfCourses() {
        return numberOfCourse;
    }

If I understand your question, you'd want to restructure your code.如果我理解你的问题,你会想要重组你的代码。

Here is one (traditional) way to do it with pseudocode to help you along but leave the learning to you.这是一种(传统的)使用伪代码的方法来帮助您,但将学习留给您。

The key concept to understand is inheritance.要理解的关键概念是 inheritance。 The typical question you would ask is using "is a."您会问的典型问题是使用“is a”。 Is a Course a Student ? CourseStudent Is a Student a Course ? StudentCourse No. Then don't extend.不,那就不要延长。

public class Course {
   //add getters and setters
   private String name; //multiple names may make your life harder, think it through
   private String[] number;
   private String description;
   private int credit; //are you sure this is an int??
   private Student[] enrolledStudents;
}

public class Student extends Person { //not sure whether Person is really needed, but that's based on your requirements.  Interfaces and composition are preferred.
   //add getters and setters
   private Course[] enrolledCourses;
   //other properties
 }

You would also need to get clearer on the Remark and the relationship to each class.您还需要更清楚地了解备注以及与每个 class 的关系。 Are remarks written about each student for each course?是否为每门课程写了关于每个学生的评论? Do you need to see the remarks for all courses?需要看所有课程的备注吗? Do you need to see all remarks for all students?您需要查看所有学生的所有备注吗?

If the application is involved and uses say, a database, you could have a service class with something like:如果涉及应用程序并使用数据库,您可以使用 class 服务,其内容如下:

public String getRemarks(Student student, Course course) {
    //some implementation.  A Remark could also be a class (bean) depending on your needs
}

Having a separate variable for numberOfCourses is likely not needed.可能不需要为numberOfCourses设置一个单独的变量。 You can just count the size of the enrolledCourses array.您可以只计算enrolledCourses数组的大小。

Using Java's List will also make your life easier in many cases depending on what you need to do.在许多情况下,使用 Java 的List也会使您的生活更轻松,具体取决于您需要做什么。

I will leave the functional style as a learning exercise, but that is where the industry has shifted toward in practice.我将把功能风格作为一种学习练习,但这是行业在实践中转向的地方。 Features of newer Java versions (eg records) can apply here too.较新的 Java 版本(例如记录)的功能也可以在这里应用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM