Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在不膨胀main方法的情况下将数据加载到java程序中_Java_Oop_Initialization - Fatal编程技术网

如何在不膨胀main方法的情况下将数据加载到java程序中

如何在不膨胀main方法的情况下将数据加载到java程序中,java,oop,initialization,Java,Oop,Initialization,我正在寻找一些面向对象的设计帮助 在我的程序开始时,我需要通过构建对象和分配变量,将一大堆数据加载到程序中(不是从数据库或任何地方;在本例中,我只是将其加载到程序中)。然而,这占用了很多空间,我觉得我只是阻塞了主要方法,使其无法阅读 我想知道,就OOP而言,正确的方法是什么 我尝试创建一个类SubjectBuilder,在构造函数或变量空间中创建所有内容,然后在main方法中创建一个新对象。但似乎所有数据都超出了main方法的范围,所以当我以后需要操作它时,我不能 Student jack =

我正在寻找一些面向对象的设计帮助

在我的程序开始时,我需要通过构建对象和分配变量,将一大堆数据加载到程序中(不是从数据库或任何地方;在本例中,我只是将其加载到程序中)。然而,这占用了很多空间,我觉得我只是阻塞了主要方法,使其无法阅读

我想知道,就OOP而言,正确的方法是什么

我尝试创建一个类
SubjectBuilder
,在构造函数或变量空间中创建所有内容,然后在main方法中创建一个新对象。但似乎所有数据都超出了main方法的范围,所以当我以后需要操作它时,我不能

Student jack = new Student("Jack Smith");   // Creating all the Student objects
Student jim = new Student("Jim Lucas");
Student beck = new Student("Beck Barber");
Student ann = new Student("Ann Walker");
Student lucy = new Student("Lucy Boxer");

Subject maths = new Subject("Maths");       //  Creating all the Subject objects 
Subject business = new Subject("Business");
Subject java = new Subject("Java");
Subject design = new Subject("Design");
Subject project = new Subject("Project");

business.addStudent(jack, 70);
business.addStudent(jim, 65);
business.addStudent(beck, 70);
business.addStudent(ann, 83);
business.addStudent(lucy, 78);
^^这是我需要加载的数据片段。每个科目我都有一大堆分数。 在这个阶段,我只有
应用程序
类、
主题
类(学生和整数分数)和
学生


我所需要做的就是创建这个程序,为学生或科目显示个人数据,并允许更改特定分数等。我相信我可以用主要方法完成整个项目,但我觉得这不是非常面向对象的。我只是不知道如何做,否则。

首先,创建一个方法来实现这一点,这样它就不在您的主程序中了

e、 g


现在,在loadData方法中,您可以从CSV文件加载此数据,可能吗?请参见一个简单的建议:创建一个StudentFactory来封装students实例化,facory方法可能如下所示:public List createAllStudents()


主题课也是如此。

如果你只想访问商务课的学生,你可以做以下操作

business.addStudent(new Student("Jack Smith"), 70);
business.addStudent(new Student("Jim Lucas"), 65);
business.addStudent(new Student("Beck Barber"), 70);
business.addStudent(new Student("Ann Walker"), 83);
business.addStudent(new Student("Lucy Boxer"), 78);

将数据保存在数组或其他数据结构中,如

String names = {"Jack Smith","John","Asley"};
int grades = {70,65,85}; //you can generate local or global array , its up to you 

int main()
{
     for(int i=0;i<names.length;i++)
         business.load(names[i],grades[i]);
}
stringnames={“jacksmith”、“John”、“Asley”};
整数等级={70,65,85}//您可以生成本地或全局数组,这取决于您
int main()
{

对于(int i=0;i你应该使用map来容纳你的学生,并使用
Long
String
或任何其他不可变类作为你的键。我建议使用
map
,因为它是
O(1)
来访问此类集合中的数据,而不是
O(n)
如果你使用
ArrayList

对于您的
主题
,您可以再次使用一些地图:

private void loadSubjects(){
    subjects.put("MATH" , new Subject("Mathematics"));
    subjects.put("JAVA" , new Subject("Java"));
    subjects.put("DESING_PATTERNS" , new Subject("DesingPatterns"));
    //for instance you could have more advanced subjects such as:
    subjects.put("C++" , new Subject("C++" , "lecture" , "class 101" , "prof. T. Richards")); 
}
由于调用对象的构造函数意味着该对象已准备好使用,因此应将
loadStudent()
(可能还有
loadSubjects()
方法,或用于将数据加载到应用程序中的任何方法)调用到此构造函数中

public BusinessLogic(){
    loadStudents();
    loadSubjects();
    //and so on
}
这是避免在新创建的对象上调用load data方法的首选方法,并且始终记住在使用对象之前必须调用这些方法:

BusinessLogic business = new BusinessLogic();
business.calculateFinalGrades(); // NOT GOOD BECAUSE YOU HAVE FORGOTTEN TO CALL loadStudents();
因此,你最终会得到如下结果:

public static void main(String[] args){
    buildUI();//build your command line interface / graphical user interface
    BusinessLogic businessLogic = new BusinessLogic();//you can probably now make this object a Singleton in order to prevent having multiple instances
    ...
}

你是对的,主要构建整个应用程序不是这样做的。在我看来,你应该构建一个提供数据的类,比如说
DataProvider
,它有提供给定数据的方法。因为有多种存储数据的方法,你可以根据通用代码将其设置为接口/抽象类您的项目可能会发展,以后您可能会希望从DB或XML或其他东西提取数据。这样,它将是松散耦合的,易于替换。它可能看起来像这样:

 interface DataProvider<T> {
      Collection<T> loadData();
      //...other useful methods
 }

 class DummyStudentDataProvider implements DataProvide<Student> {
      Collection<Student> loadData() {
           ///...your logic
      }     
 }    
接口数据提供程序{
收集加载数据();
//…其他有用的方法
}
类DummyStudentDataProvider实现DataProvider{
收集加载数据(){
///…你的逻辑
}     
}    

我使用泛型,因为相同的逻辑可以用于您可能需要的不同类。希望这对您有所帮助。

您不能将
业务
main
传递到
main
以外的其他对象(另一个类构造函数、函数等等…)这将创建所有的主题并将它们添加到
业务中
?我不确定我是否能很好地理解您的问题…谢谢,这对我来说很有意义,我也考虑过类似的事情。但是,一旦我加载了文件,我实际上就无法操作主方法中的对象,或者根本无法操作任何方法中的对象了?抱歉,我是不明白你的问题是什么。
BusinessLogic business = new BusinessLogic();
business.calculateFinalGrades(); // NOT GOOD BECAUSE YOU HAVE FORGOTTEN TO CALL loadStudents();
public static void main(String[] args){
    buildUI();//build your command line interface / graphical user interface
    BusinessLogic businessLogic = new BusinessLogic();//you can probably now make this object a Singleton in order to prevent having multiple instances
    ...
}
 interface DataProvider<T> {
      Collection<T> loadData();
      //...other useful methods
 }

 class DummyStudentDataProvider implements DataProvide<Student> {
      Collection<Student> loadData() {
           ///...your logic
      }     
 }