创建结构像Java中的数据结构 [英] Creating struct like data structure in Java
问题描述
我是新来的Java和我试图找出一种方法来存储象C说一个结构比如我想有一个节目租赁员工的信息。这将需要来自用户的姓,名,身份证号码,并将其存储。然后,用户可以查看基于关闭的状态(如果数据库有超过1雇员例如)该信息。可以这样做的任何建议的最佳方式?
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 user a first name, last name, and id number and would store it. The user could then view that information based off a condition (if the database had more than 1 employee for example). Can any suggest the best way for doing this?
推荐答案
在C A结构就像Java的一个类,更强大,因为在Java类可以包含方法,和C ++一样。您创建一个新的类。例如:
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(String code) { this.code = code; }
}
而当你想就像在C上创建多员工,创建数组:
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);
这篇关于创建结构像Java中的数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!