如何将一个带有构造函数的UML类转换为java对象?

9rbhqvlz  于 2022-11-20  发布在  Java
关注(0)|答案(2)|浏览(143)

我试图创建一个构造函数,创建一个空的家庭作业分数数组(每个学生6个)和一个空的考试分数数组(每个学生3个)。
getHomeworkAverage()方法会传回学生家庭作业的平均分数。getFinalScore()方法会使用下列评分,传回每个学生的最终分数:(考试1为15%,考试2为25%,考试3为30%,家庭作业平均为30%)。
UML Diagram Here

public class Student {
    private String name;
    private int[] homeworks;
    private int[] exams;
    
    private int[] homeworkScores;
    private int[] examScores;
    
    private double homeworkAvergae;
    private int finalScore;

    public Student(String name, int[] homeworks, int[] exams, int[] homeworkScores, int[] examScores, double homeworkAvergae, int finalScore) {
        this.name = name;
        this.homeworks = homeworks;
        this.exams = exams;
        this.homeworkScores = homeworkScores;
        this.examScores = examScores;
        this.homeworkAvergae = homeworkAvergae;
        this.finalScore = finalScore;
    }

    public int[] getHomeworkScores() {
        return homeworkScores;
    }

    public void setHomeworkScores(int[] homeworkScores) {
        this.homeworkScores = homeworkScores;
    }
    
    public double getHomeworkAvergae() {
        return homeworkAvergae;
    }
    
    public int[] getExamScores() {
        return examScores;
    }

    public void setExamScores(int[] examScores) {
        this.examScores = examScores;
    }
    
    public int getFinalScore() {
        return finalScore;
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int[] getHomeworks() {
        return homeworks;
    }

    public void setHomeworks(int[] homeworks) {
        this.homeworks = homeworks;
    }

    public int[] getExams() {
        return exams;
    }

    public void setExams(int[] exams) {
        this.exams = exams;
    }
}
cunj1qz1

cunj1qz11#

图表中是否有建构函式?

如果按照书中的说法,你的UML类不包含任何构造函数。UML中的构造函数是一个带有构造型«Create»的操作,并返回一个Student

+ «Create» Student(name:String):Student

当然,一些主流的OOP语言使用了构造函数的命名约定,类似于类,并且没有指定返回类型。这就是为什么许多人会将Student(name:String)理解为构造函数,尽管UML不一致。

图中的构造函数签名是什么?

假设Student(name:String),您将需要按照设计中的预见实现此类:

public class Student {
    ...
    public Student(String name) {
    ...
    }
}

其他备注

如果你在初始化数组时遇到困难,你可能会发现很多answers on SO。我的第一个猜测是:

homeworks = new int[6];
...

我不会回顾代码,也不会完成缺失的部分。我唯一想强调的是,数组字段的getter返回对对象数组的引用,这允许在没有控制的情况下更新数组的内容。同样,设置数组时,重用另一个数组,并且不会覆盖当前数组的内容。看看here on SO如何克隆数组以避免这些风险

9jyewag0

9jyewag02#

你 的 问题 是 如何 构造 构造 函数 ?

int[] homeworks = new int[]{ 1,2,3,4,5,6,7,8,9,10 };
    int[] exams = new int[]{ 1,2,3,4,5,6,7,8,9,10 };
    int[] homeworkScores = new int[]{ 1,2,3,4,5,6,7,8,9,10 };
    int[] examScores = new int[]{ 1,2,3,4,5,6,7,8,9,10 };
    Student student = new Student(
            "Name",
            homeworks,
            exams,
            homeworkScores,
            examScores,
            6.0,
            6
    );

中 的 每 一 个

相关问题