简体   繁体   English

如何通过另一个 class 的方法调用实例变量的 class

[英]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.

相关问题 如何从Android中的另一个类调用预定义类中的实例方法 - How to call instance method in predefined class from another class in android 一个类的调用方法以使用另一个类的实例 - Call method of a class to use instance of another class 如何通过主 Class 从另一个 Class 调用方法? - How to Call a Method From Another Class, Through The Main Class? 如何在另一个类的if语句中调用实例变量 - How to call instance variables in another class's if statement 如何将另一个类中的变量调用到 ActionListener 方法中? - How to call variables from another class into an ActionListener method? 如何调用另一个类的方法? - How to call method of the another class? 如何调用另一个类内部的类的方法? - How to call a method of a class that is inside another class? 如何实现OOP来从一个类调用一个实例方法以存储在另一个类中另一个实例方法的数组中? - How can OOP be implemented to call an instance method from one class to be stored in an array in another instance method in a different class? 如何从同一类的另一个方法中调用变量,以及如何调用该方法? - How do I call for variables from another method in same class, as well as call upon the method? 从静态方法访问另一个类的实例变量 - Accessing instance variables from another class from a static method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM