如何从另一个类(Java对象和类)检查客户
我只是想找到 motorsize = 2.0 的客户。 我创建了一个方法来找出哪个是 checkmotorsize。 然而,它不起作用。有人可以帮我吗?谢谢
car.java
package day27;
public class Car {
String brand;
double motorSize;
}
person.java
package day27;
import java.util.ArrayList;
public class Person {
String name;
String surname;
Car car;
public static void main(String[] args) {
Person customer1 = createCustomer("Sally", "Greg", "Mercedes", 2.0);
Person customer2 = createCustomer("Linda", "Greg", "BMW", 3.0);
Person customer3 = createCustomer("Joseph", "Greg", "Toyota", 2.0);
Person customer4 = createCustomer("Midge", "Greg", "Lexus", 3.0);
Person customer5 = createCustomer("John", "Greg", "Mercedes", 1.5);
ArrayList<Person> listOfCustomer = createListOfCustomer(customer1, customer2, customer3, customer4, customer5);
checkMotorSize(listOfCustomer);
checkSurname(listOfCustomer);
}
public static Person createCustomer(String name, String surname, String brand, double motorSize) {
Person customer = new Person();
customer.name = name;
customer.surname = surname;
customer.car.brand = brand;
customer.car.motorSize = motorSize;
return customer;
}
public static ArrayList<Person> createListOfCustomer(Person... customer) {
ArrayList<Person> list = new ArrayList<>();
for (Person person : customer) {
list.add(person);
}
return list;
}
public static void checkMotorSize(ArrayList<Person> list) {
for (Person person : list) {
if (person.car.motorSize == 2.0) {
System.out.println(person);
}
}
}
public static void checkSurname(ArrayList<Person> list) {
for (Person person : list) {
if (person.surname == "Greg") {
System.out.println(person);
}
}
}
}
我只是想找到 motorsize = 2.0 的客户。 我创建了一个方法来找出哪个是 checkmotorsize。 然而,它不起作用。有人可以帮我吗?谢谢
我只是想找到 motorsize = 2.0 的客户。 我创建了一个方法来找出哪个是 checkmotorsize。 然而,它不起作用。有人可以帮我吗?谢谢
最佳答案
这是正确且正确的做法。您应该有一个 Car 的构造函数。
public class Car {
String brand;
double motorSize;
public Car(String brand, double motorSize){
this.brand =brand;
this.motorSize =motorSize;
}
}
您也应该更改 Person 类。
导入java.util.ArrayList;
public class Person {
String name;
String surname;
Car car;
public static void main(String[] args) {
Person customer1 = new Person("Sally", "Greg", new Car("Mercedes", 2.0 ) );
Person customer2 = new Person("Linda", "Greg", new Car("BMW", 3.0));
Person customer3 = new Person("Joseph", "Greg",new Car( "Toyota", 2.0));
Person customer4 = new Person("Midge", "Greg", new Car("Lexus", 3.0));
Person customer5 = new Person("John", "Greg", new Car("Mercedes", 1.5));
ArrayList<Person> listOfCustomer = createListOfCustomer(customer1,customer2,customer3,customer4,customer5);
checkMotorSize(listOfCustomer);
}
public Person (String name, String surname, Car car) {
this.name = name;
this.surname = surname;
this.car= car;
}
public static ArrayList<Person> createListOfCustomer(Person... customer) {
ArrayList<Person> list = new ArrayList<>();
for (Person person : customer) {
list.add(person);
}
return list;
}
public static void checkMotorSize(ArrayList<Person> list) {
for (Person person : list) {
if (person.car.motorSize == 2.0) {
System.out.println(person.name);
}
}
}
public static void checkSurname(ArrayList<Person> list) {
for (Person person : list) {
if (person.surname == "Greg") {
System.out.println(person);
}
}
}
}
关于javascript - 线程中的异常 "main"java.lang.NullPointerException 如何从另一个类(Java 对象和类)检查客户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61554178/