我们如何为 java 中的对象数组中的嵌套 class 创建一个实例?
How can we create an instance for a nested class in array of objects in java?
import java.util.Arrays;
import java.util.Scanner;
public class employee{
public String name;
public class employee_address{
String street_name;
String city;
String zipcode;
String state;
String country;
}
public static void main(String []args){
Scanner user_input = new Scanner(System.in);
int no_of_employees = user_input.nextInt();
employee[] employees_list = new employee[no_of_employees];
for(int i = 0;i < no_of_employees;i++){
employees_list[i].name = user_input.nextLine();
employees_list[I].employee_address = // this is it ?
}
}
}
在上面的代码中,我明白 employee_address 是一个 class 并且无法访问
直接在没有像代码中那样创建实例的情况下,这是没有意义的。但是我如何创建与每个员工关联的 employee_address class 的实例。
就像上面的代码 'employee_address' 与每个员工相关联,但是如何初始化 class 'employee_address' 以及如何设置 street_name、城市和地址 class 中的其余成员。任何想法将不胜感激。
一些提示:
- 遵守命名约定:class Java 中的名称以大写字母开头
- 在使用它们之前使用 (class) 定义(如果不方便,请将它们收集在顶部)
- 如果您确定要使用内部 classes,请将它们设置为静态,除非您希望它们与泛型纠缠在一起。
- 通常每个文件中的普通 classes 更加灵活且更易于使用
- 如果您使用的对象只携带 public 数据,请尝试使用 final 关键字并尽快初始化它们
- 先使用合适的对象,完成后赋值给数组。 avan 更好的方法是使用
ArrayList
之类的
- 如果Employee包含
EmployeeAddress
,方便的话应该初始化一下。所以一个对象总是对它自己的东西负责
- 使用try/resrouce/catch
scanner.nextInt()
可能会因 newline/line 中断而出现问题。对于用户输入更好 readLine()
和解析输入
代码:
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Scanner;
public class Employee {
static public class EmployeeAddress {
public final String street_name;
public final String city;
public final String zipcode;
public final String state;
public final String country;
public EmployeeAddress(final Scanner pScanner, final PrintStream pOutPS) {
street_name = readLine(pScanner, pOutPS, "Please enter Street Name:");
city = readLine(pScanner, pOutPS, "Please enter City Name:");
zipcode = readLine(pScanner, pOutPS, "Please enter Zip Code:");
state = readLine(pScanner, pOutPS, "Please enter State:");
country = readLine(pScanner, pOutPS, "Please enter Country:");
}
}
static public String readLine(final Scanner pScanner, final PrintStream pOutPS, final String pPrompt) {
pOutPS.print(pPrompt);
final String value = pScanner.nextLine();
pOutPS.println();
return value;
}
static public int readInt(final Scanner pScanner, final PrintStream pOutPS, final String pPrompt) {
return Integer.parseInt(readLine(pScanner, pOutPS, pPrompt));
}
public final String name;
public final EmployeeAddress address;
public Employee(final Scanner pScanner, final PrintStream pOutPS) {
name = readLine(pScanner, pOutPS, "Please enter Employee Name: ");
System.out.println("Name: " + name);
address = new EmployeeAddress(pScanner, pOutPS);
}
public static void main(final String[] args) {
try (final Scanner user_input = new Scanner(System.in);
final PrintStream output = System.out;) {
final int no_of_employees = readInt(user_input, output, "Please enter number of users: ");
final Employee[] employees_list = new Employee[no_of_employees]; // either this line
final ArrayList<Employee> employees = new ArrayList<>(); // or this line
for (int i = 0; i < no_of_employees; i++) {
output.println("Creating user #" + (i + 1) + "...");
final Employee newEmployeeWithAddress = new Employee(user_input, output);
employees_list[i] = newEmployeeWithAddress; // either this line
employees.add(newEmployeeWithAddress); // or this line
}
}
}
}
下面的代码使用 Java naming conventions(您的代码没有)。
代码后的注释。
import java.util.Scanner;
public class Employee {
private String name;
private EmployeeAddress address;
public class EmployeeAddress {
String streetName;
String city;
String zipcode;
String state;
String country;
}
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
int noOfEmployees = userInput.nextInt();
Employee[] employeesList = new Employee[noOfEmployees];
for (int i = 0; i < noOfEmployees; i++) {
employeesList[i] = new Employee();
employeesList[i].name = userInput.nextLine();
EmployeeAddress employeeAddress = employeesList[i].new EmployeeAddress();
employeesList[i].address = employeeAddress;
employeesList[i].address.streetName = userInput.nextLine();
}
}
}
内部class是正常的class。它不是其封闭 class 的成员。如果想让classEmployee
有[员工]地址,还有[员工]姓名,需要在classEmployee
中再增加一个成员变量,其类型是 EmployeeAdress
.
Employee[] employeesList = new Employee[noOfEmployees];
上面一行创建了一个数组,但数组中的每个元素都是空的。因此,您需要首先创建一个 Employee
对象并将其分配给数组的一个元素。因此,上面我的代码中的以下行:
employeesList[i] = new Employee();
由于 EmployeeAddress
不是 静态 class,为了创建一个新实例,您首先需要一个封闭 class,即 Employee
。因此,上面代码中的以下行。
EmployeeAddress employeeAddress = employeesList[i].new EmployeeAddress();
由于所有代码都在 class Employee
中,在方法 main
中,您可以直接访问 class Employee
和 EmployeeAddress
。尽管如此,您还是需要注意 java.
中的不同 access modifiers
你不能直接创建 inner class 的实例,原因是因为它是另一个实例的 属性,我们总是需要通过父变量的实例来使用它。
假设您有一个 class,它有两个属性:
public class Employee {
public String name;
public EmployeeAddress emAddress;
}
访问emAddress
你需要通过Employee
class的实例来使用,例如-
Employee object = new Employee();
EmployeeAddress empAdd = object.new EmployeeAddress();
完整代码:
public class Employee {
public String name;
public EmployeeAddress emAddress;
public class EmployeeAddress {
String street_name;
String city;
String zipcode;
String state;
String country;
public String getStreet_name() {
return street_name;
}
public void setStreet_name(String street_name) {
this.street_name = street_name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return "EmployeeAddress [street_name=" + street_name + ", city=" + city + ", zipcode=" + zipcode
+ ", state=" + state + ", country=" + country + "]";
}
}
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
int no_of_employees = user_input.nextInt(); // let's say no_of_employees = 1
Employee[] employees = new Employee[no_of_employees];
for (int i = 0; i < no_of_employees; i++) {
Employee object = new Employee();
object.setName("Virat Kohli");
EmployeeAddress empAdd = object.new EmployeeAddress();
empAdd.setCity("New Delhi");
empAdd.setCountry("India");
empAdd.setState("Delhi");
empAdd.setStreet_name("Chandni Chalk");
empAdd.setZipcode("741124");
object.setEmAddress(emAddress);
employees[i] = object;
}
System.out.println(employees[0]);
user_input.close();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public EmployeeAddress getEmAddress() {
return emAddress;
}
@Override
public String toString() {
return "Employee [name=" + name + ", emAddress=" + emAddress + "]";
}
public void setEmAddress(EmployeeAddress emAddress) {
this.emAddress = emAddress;
}
}
我已将您的代码修改为 sonar
标准。
import java.util.Arrays;
import java.util.Scanner;
public class employee{
public String name;
public class employee_address{
String street_name;
String city;
String zipcode;
String state;
String country;
}
public static void main(String []args){
Scanner user_input = new Scanner(System.in);
int no_of_employees = user_input.nextInt();
employee[] employees_list = new employee[no_of_employees];
for(int i = 0;i < no_of_employees;i++){
employees_list[i].name = user_input.nextLine();
employees_list[I].employee_address = // this is it ?
}
}
}
在上面的代码中,我明白 employee_address 是一个 class 并且无法访问 直接在没有像代码中那样创建实例的情况下,这是没有意义的。但是我如何创建与每个员工关联的 employee_address class 的实例。
就像上面的代码 'employee_address' 与每个员工相关联,但是如何初始化 class 'employee_address' 以及如何设置 street_name、城市和地址 class 中的其余成员。任何想法将不胜感激。
一些提示:
- 遵守命名约定:class Java 中的名称以大写字母开头
- 在使用它们之前使用 (class) 定义(如果不方便,请将它们收集在顶部)
- 如果您确定要使用内部 classes,请将它们设置为静态,除非您希望它们与泛型纠缠在一起。
- 通常每个文件中的普通 classes 更加灵活且更易于使用
- 如果您使用的对象只携带 public 数据,请尝试使用 final 关键字并尽快初始化它们
- 先使用合适的对象,完成后赋值给数组。 avan 更好的方法是使用
ArrayList
之类的 - 如果Employee包含
EmployeeAddress
,方便的话应该初始化一下。所以一个对象总是对它自己的东西负责 - 使用try/resrouce/catch
scanner.nextInt()
可能会因 newline/line 中断而出现问题。对于用户输入更好readLine()
和解析输入
代码:
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Scanner;
public class Employee {
static public class EmployeeAddress {
public final String street_name;
public final String city;
public final String zipcode;
public final String state;
public final String country;
public EmployeeAddress(final Scanner pScanner, final PrintStream pOutPS) {
street_name = readLine(pScanner, pOutPS, "Please enter Street Name:");
city = readLine(pScanner, pOutPS, "Please enter City Name:");
zipcode = readLine(pScanner, pOutPS, "Please enter Zip Code:");
state = readLine(pScanner, pOutPS, "Please enter State:");
country = readLine(pScanner, pOutPS, "Please enter Country:");
}
}
static public String readLine(final Scanner pScanner, final PrintStream pOutPS, final String pPrompt) {
pOutPS.print(pPrompt);
final String value = pScanner.nextLine();
pOutPS.println();
return value;
}
static public int readInt(final Scanner pScanner, final PrintStream pOutPS, final String pPrompt) {
return Integer.parseInt(readLine(pScanner, pOutPS, pPrompt));
}
public final String name;
public final EmployeeAddress address;
public Employee(final Scanner pScanner, final PrintStream pOutPS) {
name = readLine(pScanner, pOutPS, "Please enter Employee Name: ");
System.out.println("Name: " + name);
address = new EmployeeAddress(pScanner, pOutPS);
}
public static void main(final String[] args) {
try (final Scanner user_input = new Scanner(System.in);
final PrintStream output = System.out;) {
final int no_of_employees = readInt(user_input, output, "Please enter number of users: ");
final Employee[] employees_list = new Employee[no_of_employees]; // either this line
final ArrayList<Employee> employees = new ArrayList<>(); // or this line
for (int i = 0; i < no_of_employees; i++) {
output.println("Creating user #" + (i + 1) + "...");
final Employee newEmployeeWithAddress = new Employee(user_input, output);
employees_list[i] = newEmployeeWithAddress; // either this line
employees.add(newEmployeeWithAddress); // or this line
}
}
}
}
下面的代码使用 Java naming conventions(您的代码没有)。
代码后的注释。
import java.util.Scanner;
public class Employee {
private String name;
private EmployeeAddress address;
public class EmployeeAddress {
String streetName;
String city;
String zipcode;
String state;
String country;
}
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
int noOfEmployees = userInput.nextInt();
Employee[] employeesList = new Employee[noOfEmployees];
for (int i = 0; i < noOfEmployees; i++) {
employeesList[i] = new Employee();
employeesList[i].name = userInput.nextLine();
EmployeeAddress employeeAddress = employeesList[i].new EmployeeAddress();
employeesList[i].address = employeeAddress;
employeesList[i].address.streetName = userInput.nextLine();
}
}
}
内部class是正常的class。它不是其封闭 class 的成员。如果想让classEmployee
有[员工]地址,还有[员工]姓名,需要在classEmployee
中再增加一个成员变量,其类型是 EmployeeAdress
.
Employee[] employeesList = new Employee[noOfEmployees];
上面一行创建了一个数组,但数组中的每个元素都是空的。因此,您需要首先创建一个 Employee
对象并将其分配给数组的一个元素。因此,上面我的代码中的以下行:
employeesList[i] = new Employee();
由于 EmployeeAddress
不是 静态 class,为了创建一个新实例,您首先需要一个封闭 class,即 Employee
。因此,上面代码中的以下行。
EmployeeAddress employeeAddress = employeesList[i].new EmployeeAddress();
由于所有代码都在 class Employee
中,在方法 main
中,您可以直接访问 class Employee
和 EmployeeAddress
。尽管如此,您还是需要注意 java.
你不能直接创建 inner class 的实例,原因是因为它是另一个实例的 属性,我们总是需要通过父变量的实例来使用它。
假设您有一个 class,它有两个属性:
public class Employee {
public String name;
public EmployeeAddress emAddress;
}
访问emAddress
你需要通过Employee
class的实例来使用,例如-
Employee object = new Employee();
EmployeeAddress empAdd = object.new EmployeeAddress();
完整代码:
public class Employee {
public String name;
public EmployeeAddress emAddress;
public class EmployeeAddress {
String street_name;
String city;
String zipcode;
String state;
String country;
public String getStreet_name() {
return street_name;
}
public void setStreet_name(String street_name) {
this.street_name = street_name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return "EmployeeAddress [street_name=" + street_name + ", city=" + city + ", zipcode=" + zipcode
+ ", state=" + state + ", country=" + country + "]";
}
}
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
int no_of_employees = user_input.nextInt(); // let's say no_of_employees = 1
Employee[] employees = new Employee[no_of_employees];
for (int i = 0; i < no_of_employees; i++) {
Employee object = new Employee();
object.setName("Virat Kohli");
EmployeeAddress empAdd = object.new EmployeeAddress();
empAdd.setCity("New Delhi");
empAdd.setCountry("India");
empAdd.setState("Delhi");
empAdd.setStreet_name("Chandni Chalk");
empAdd.setZipcode("741124");
object.setEmAddress(emAddress);
employees[i] = object;
}
System.out.println(employees[0]);
user_input.close();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public EmployeeAddress getEmAddress() {
return emAddress;
}
@Override
public String toString() {
return "Employee [name=" + name + ", emAddress=" + emAddress + "]";
}
public void setEmAddress(EmployeeAddress emAddress) {
this.emAddress = emAddress;
}
}
我已将您的代码修改为 sonar
标准。