Creating struct like data structure in Java

前端 未结 3 1653
Happy的楠姐
Happy的楠姐 2020-12-05 11:48

I am new to Java and I am trying to find out a way to store information like a struct in C. Say for example I want to have a program hire employees. It would take from the

3条回答
  •  孤街浪徒
    2020-12-05 12:26

    A struct in C just like a class in Java and much more powerful, because class in Java can contain method, and C++ does. You create a new class. For example :

       class Employee {
           private String name;
           private int code;
    
       // constructor
       public Employee(String name, int code) {
          this.name = name;
          this.code = code;
       }
    
           // getter
           public String getName() { return name; }
           public int getCode() { return code; }
           // setter
    
           public void setName(String name) { this.name = name; }
           public void setCode(int code) { this.code = code; }
        }
    

    And when you want to create multi employees, create array just like in C:

    Employee[] arr = new Employee[100];  // new stands for create an array object
    arr[0] = new Employee("Peter", 100); // new stands for create an employee object
    arr[1] = new Employee("Mary", 90);
    

提交回复
热议问题