可以创建对象的类方法

时间:2013-03-17 17:52:29

标签: java

  1. 我有一个包含Lecturer和Subject两个类的时间表项目,还指定了已知对象。

  2. 他们都必须在一个对象或arraylist数组

  3. 该项目应该允许用户能够创建或添加,删除,搜索和更新班级讲师的对象。

  4. 我真的不知道如何去做这个我可以创建一个方法,如果它是主类中的类方法或方法,可以执行上面的任务吗?

    如何让用户能够使用方法或任何其他方式创建对象?

    我已经创建了类,对象和arraylist被卡住了

    很抱歉,我也可以将用户创建的对象添加到arraylist中,即使可能吗?

    class Lecturer   {                
    static int lec_id;                 
    static String lec_name;
    static String lec_rank;
    static int lec_salary;
    static String lec_department;
    

    }

    class Subject   {                  /* initialisation for the class subject*/
    String sub_code;
    String sub_name;
    String sub_department;
    int sub_lec_id;                 /*sub lec id represent the lecturer taking the subject*/
    String sub_day;
    String sub_time;
    String sub_venue;
    

    }

    /* intialisation for the class lecturer*/
    
    public class Timetable {
    
    public static void main(String[] args) {
        /* the code below represent a created list of objects  */                       /* `Lecturer l =Lecturer ()(System.in);*/`
    
    
        Lecturer l1 =new Lecturer (1, "Dadson",  "Dr",   8000,  "Computing"); 
        Lecturer l2 =new Lecturer (2, "Hall",   "Prof", 12000,  "Computing");
        Lecturer l3 =new Lecturer (2, "Berry",   "Dr",   7500,  "Computing");
        Lecturer l4 =new Lecturer (2, "Bull",    "Dr",   9000,  "Engineering");
        Lecturer l5 =new Lecturer (2, "Viles",  "Prof", 13500,  "Engineering");
        Lecturer l6 =new Lecturer (2, "Dyson",   "Dr",   7200,  "Engineering");
    
        /*The code below represent the array list holding objects of the class Lecturer*/
    
       List<Lecturer> L=new ArrayList<Lecturer>();
       L.add(l1); 
       L.add(l2);
       L.add(l3);
       L.add(l4);
       L.add(l5);
       L.add(l6);
    
    
    
    
       /* the code below is to create the object for the class Subject*/ 
       Subject s1=new Subject ("EC3000", "Computing Basics",         "Computing",   1, "Mon",      "8-10am",  "S310");
       Subject s2=new Subject ("EE3000", "Engineering Basics",       "Engineering", 2, "Tuesday",  "10-12pm", "S310");
       Subject s3=new Subject ("EC3100", "Programming Fundamentals", "Engineering", 1, "Monday",   "1-3pm",   "S203");
       Subject s4=new Subject ("EE3100", "Engineeering Math",        "Computing",   4, "Thursday", "3-5pm",   "s420");
       Subject s5=new Subject ("EC3200", "Problem Solving",          "Computing",   3, "Friday",   "8-10pm",  "S208");
       Subject s6=new Subject ("EE3200", "Circuit",                  "Enginerring", 4, "Monday",   "10-12pm", "S104");
       Subject s7=new Subject ("EC3300", "Networking",               "Computing",   2, "Tuesday",  "1-3pm",   "S310");
       Subject s8=new Subject ("EE3300", "Electromagnetic",          "Engineering", 5, "Wednesday","1-3pm",   "S330");
       Subject s9=new Subject ("EC3400", "Project",                  "Computing",   3, "Thursday", "3-5pm",   "S312");
      Subject s10 =new Subject("EE3400", "Project",                  "Engineering", 6, "Tuesday",  "10-12pm", "S415");
    
      /*The code below represent the array list holding objects of the class Subject*/
    
      Subject[]S=new Subject[10];
      S[0]=s1;
      S[1]=s2;
      S[2]=s3;
      S[3]=s4;
      S[4]=s5;
      S[5]=s6;
      S[6]=s7;
      S[7]=s8;
      S[8]=s9;
      S[9]=s10;
    
    
    } 
    
    
    }
    

2 个答案:

答案 0 :(得分:1)

应该'主题'是'讲师'的儿童班吗?

如果是这样,您可以通过以下方式在主要方法中创建讲师的arraylist:

ArrayList <Lecturer> al_lecturers = new ArrayList<Lecturer>();

//create 10 lecturers
for(int i = 0; i < 10; i++){
    al_lecturers.add(new Lecturer());
}

通过使用ArrayList,您可以利用该类提供的add()remove()contains()方法来获取您请求的一些功能。

我希望能帮到你一点。

为了让用户能够添加自己的讲师,您可以创建自己的方法:

private void addLecturer(/*lecturer info vars*/){
    al_lecturers.add(new Lecturer());
}

然后从用户输入此信息的位置调用此方法,可能是某种ActionListener

答案 1 :(得分:0)

如果你想要一个允许你创建Lecturer对象的方法,下面是一个如何实现这个目标的例子(可以有其他方法)

public void createLecturer(int num, String name,String title,int num2,String department)
{
   Lecturer lec = new Lecturer(num,name,title,num2,department);
   aList.add(lec);
}

上面将根据您传递给方法的参数创建一个新的Lecturer对象,并将讲师添加到您选择的List中。

删除讲师可以通过使用remove方法完成,该方法接受Lecturer对象并将其删除。例如:

Lecturer lec = aList.get(0);
aList.remove(lec);

这将从名单中删除第一位讲师。

您可以使用与contains类似的remove方法来搜索讲师。

最后,更新Lecturer对象的方法是使用set方法,该方法获取要更新Lecturer详细信息的索引,以及带有新详细信息的Lecturer对象。

例如set(0,lec2)

希望这能为你提供一个起点。