[英]How to call a class of instance variables through a method of another class
I am doing a project for school that requires me to build a grade system for a school I am struggling to understand why when I have a class of public variables they are not allowed to be called in the method of another class.我正在为学校做一个项目,需要我为一所学校建立一个等级系统我很难理解为什么当我有一个公共变量的 class 时,不允许在另一个 class 的方法中调用它们。 Can someone give an example or explain how this could work?
有人可以举个例子或解释一下这是如何工作的吗?
public void AddEngGrade(Grades obj)
{
Scanner sc1 = new Scanner(System.in);
System.out.println("Which Grade would you like to enter");
System.out.println("");
System.out.println("Major");
System.out.println("Minor");
System.out.println("Other");
System.out.println("");
String gr = sc1.nextLine();
if(gr.equals("Major"))
{
System.out.print("Please enter the grade here - ");
obj.EngMajor = sc1.nextInt();
obj.EngMajPoints += obj.EngMajor;
obj.EngMajorTot++;
obj.EngMajAvg = (obj.EngMajPoints/2)*(.6);
}
The class with the variables is as follows带变量的class如下
import java.util.*;
public class Grades
{
public int EngMajor = 0;
public int EngMinor = 0;
public int EngOther = 0;
public int EngMajorTot = 0;
public int EngMinorTot = 0;
public int EngOtherTot = 0;
public double EngMajAvg = 0;
public double EngMinAvg = 0;
public double EngOtAvg = 0;
public int EngMajPoints = 0;
public int EngMinPoints = 0;
public int EngOtPoints = 0;
}
I think you approaching the design of your classes the wrong way.我认为你以错误的方式接近你的课程设计。 As this is a homework assignment, I don't want to provide a solution.
由于这是家庭作业,我不想提供解决方案。 The Grade class should not care if it is a major or a minor.
等级 class 应该不在乎它是主要还是次要。 A grade is something a student gets for taking a course.
成绩是学生参加课程所获得的东西。 Whether that course counts as a major or minor is an attribute of the student's curriculum.
该课程是主修课程还是副修课程是学生课程的一个属性。 A course does not have a major/minor attribute.
课程没有主要/次要属性。
To compute the average, sum the points, etc for the major, you want to iterate over the grades from the courses in the student's curriculum that are part of the major.要计算该专业的平均分、总分等,您需要迭代学生课程中属于该专业的课程的成绩。 A student class could have a method called computeMajorGPA() or perhaps computeGPA(some enum).
学生 class 可以有一个名为 computeMajorGPA() 或 computeGPA(some enum) 的方法。
I recommend that you avoid having classes that are plural.我建议您避免使用复数的类。 I think classes that represent singular objects is better design and multiple objects should be handled by a collection.
我认为代表单个对象的类是更好的设计,多个对象应该由一个集合处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.