如何在没有 属性 class 的情况下访问另一个 class 中的私有变量?
How to access the private variable in one class in another without property class in javafx?
我不确定如何从狗 class 调用 dogID 并将其添加到客户中存在的 dogID class。我还有狗 class 的所有者,这是该特定狗的客户 ID。我还有交易 class,我需要从狗 class 访问狗 ID。我有其他变量,如到达日期、出发日期、服务费率,它们只能采用 3 个值,如 1、2、3 和每天的费率。我不确定如何定义日期数据类型,因此必须根据开始日期对交易信息进行排序。
public class Customer implements Comparable<Customer>{
private int CustomerID;
private String lastName;
private String firstName;
private Set<Dog> dogs;
}
public class Dog implements Comparable<Dog>{
private int dogID;
private String dogName;
private Customer owner;
private String breed;
private String color;
}
public class Transaction implements Comparable<Transaction> {
private Dog dogID;
private Date arrivalDate;
private Date departureDate;
private List<Integer> serviceLevel = Arrays.asList(1,2,3);
private List<Integer> ratePerDay = Arrays.asList(89,129,149);
private double totalcharge;
private double deposit;
}
关于吸气剂和吸气剂
As all the variables in the classes are private, I am not sure how to call these variables
private
变量在定义它们的 class 之外是不可见的。
如果你想从另一个class访问私有变量的值,那么你需要提供一个具有更高可见性的访问器方法。那将是 getter 和(如有必要)setter。然后在封装 class 上调用它来访问封装的变量值,例如dog.getId()
.
大多数 IDE 可以为您自动生成 getter 和 setter。我建议使用 IDE functionality to automatically generate standard code 等构造函数、getters、setters、toString 和 hashCode 函数。
示例应用程序
对于您的示例,您可以使用如下所示的内容。这个例子可能比您需要的要复杂一些。但也许你可以通过研究它来了解一些东西。
应用输出
Opening shop
Servicing customer: Customer{id=0, dogs=[Dog{id=1, name='Fido', ownerId=0}, Dog{id=2, name='Dido', ownerId=0}]}
Servicing customer: Customer{id=3, dogs=[Dog{id=4, name='Poly', ownerId=3}, Dog{id=5, name='Anna', ownerId=3}]}
Servicing customer: Customer{id=6, dogs=[Dog{id=7, name='Butch', ownerId=6}]}
Closing shop
狗狗美容服务
import java.util.ArrayList;
public class DogGroomingService {
private ArrayList<Customer> customers = new ArrayList<>();
private void enrollCustomers() {
addCustomer("Fido", "Dido");
addCustomer("Poly", "Anna");
addCustomer("Butch");
}
private void addCustomer(String... dogNames) {
Customer customer = new Customer();
for (String dogName: dogNames) {
Dog dog = new Dog();
dog.setName(dogName);
customer.addDog(dog);
}
customers.add(customer);
}
private void serveCustomers() {
System.out.println("Opening shop");
for (Customer customer: customers) {
System.out.println("Servicing customer: " + customer);
}
System.out.println("Closing shop");
}
public static void main(String[] args) {
DogGroomingService groomingService = new DogGroomingService();
groomingService.enrollCustomers();
groomingService.serveCustomers();
}
}
IdService
public class IdService {
private static final IdService instance = new IdService();
private int nextId = 0;
public int generateNextId() {
return nextId++;
}
public static IdService getInstance() {
return instance;
}
}
客户
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
public class Customer {
private int id;
private final Set<Dog> dogs = new HashSet<>();
public Customer() {
this.id = IdService.getInstance().generateNextId();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Set<Dog> getDogs() {
return Collections.unmodifiableSet(dogs);
}
public void addDog(Dog dog) {
dogs.add(dog);
dog.setOwner(this);
}
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", dogs=" + dogs +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Customer customer = (Customer) o;
return id == customer.id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}
狗
import java.util.Objects;
public class Dog {
private int id;
private String name;
private Customer owner;
public Dog() {
id = IdService.getInstance().generateNextId();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Customer getOwner() {
return owner;
}
public void setOwner(Customer owner) {
this.owner = owner;
}
@Override
public String toString() {
return "Dog{" +
"id=" + id +
", name='" + name + '\'' +
", ownerId=" + owner.getId() +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Dog dog = (Dog) o;
return id == dog.id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}
一般注意事项
我使用 IDE (getters/setters/equals/hashcode).
自动生成了一些代码
我确实对生成的代码做了一些小修改,例如在为狗生成字符串时,我只引用客户 ID 而不是完整的客户数据,否则,您将陷入无限循环,因为在这些实体之间建立了 two-way 引用。
如果您不熟悉它们,请参阅 Baeldung tutorial on equals and hashcode,以及为什么您可能会使用它们而不是像您在问题代码中那样实施 Comparable。
Comparable 用于订购,但是一只狗比另一只狗少或一位顾客比另一位顾客多是什么意思?也许您可以根据狗名或客户 ID 定义默认订单(我不知道这取决于您的要求)。也许定义比较器可能更好。查看差异:
Follow-up 其他问题
I have two classes Customer information and Dog information, which will include the following data variables, customer ID, dog ID list (contains the dog ID for each dog owned by the customer).
I have Dog information class which takes the dog information and the owner ID(Customer ID) of that dog.
I am not sure how to add the dog ID from the dog class to the corresponding customer (owner of the dog) in the customer class dog list
如果您只想存储 ID 而不是完整的对象引用,您可以这样做。
参见下面的客户 addDog
方法:
public void addDog(Dog dog) {
dogIds.add(dog.getId());
dog.setOwnerId(this.id);
}
但是,您确实无法查看 ID 之外的对象中的其他信息。要找出答案,您需要提供单独的函数来按 ID 查找完整的对象数据。例如:
public interface DataAccess {
Customer findCustomerById(int customerId);
Dog findDogById(int dogId);
}
我不会在这里提供那些查找功能的实现,因为我不知道您如何在应用程序中存储和检索数据。
这里是一些修改后的输出和 classes,它们仅通过 ID 而不是对象引用来引用客户和狗。
Opening shop
Servicing customer: Customer{id=0, dogIds=[1, 2]}
Servicing customer: Customer{id=3, dogIds=[4, 5]}
Servicing customer: Customer{id=6, dogIds=[7]}
Closing shop
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
public class Customer {
private int id;
private final Set<Integer> dogIds = new HashSet<>();
public Customer() {
this.id = IdService.getInstance().generateNextId();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Set<Integer> getDogIds() {
return Collections.unmodifiableSet(dogIds);
}
public void addDog(Dog dog) {
dogIds.add(dog.getId());
dog.setOwnerId(this.id);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Customer customer = (Customer) o;
return id == customer.id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", dogIds=" + dogIds +
'}';
}
}
import java.util.Objects;
public class Dog {
private int id;
private String name;
private int ownerId;
public Dog() {
id = IdService.getInstance().generateNextId();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getOwnerId() {
return ownerId;
}
public void setOwnerId(int ownerId) {
this.ownerId = ownerId;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Dog dog = (Dog) o;
return id == dog.id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return "Dog{" +
"id=" + id +
", name='" + name + '\'' +
", ownerId=" + ownerId +
'}';
}
}
我不确定如何从狗 class 调用 dogID 并将其添加到客户中存在的 dogID class。我还有狗 class 的所有者,这是该特定狗的客户 ID。我还有交易 class,我需要从狗 class 访问狗 ID。我有其他变量,如到达日期、出发日期、服务费率,它们只能采用 3 个值,如 1、2、3 和每天的费率。我不确定如何定义日期数据类型,因此必须根据开始日期对交易信息进行排序。
public class Customer implements Comparable<Customer>{
private int CustomerID;
private String lastName;
private String firstName;
private Set<Dog> dogs;
}
public class Dog implements Comparable<Dog>{
private int dogID;
private String dogName;
private Customer owner;
private String breed;
private String color;
}
public class Transaction implements Comparable<Transaction> {
private Dog dogID;
private Date arrivalDate;
private Date departureDate;
private List<Integer> serviceLevel = Arrays.asList(1,2,3);
private List<Integer> ratePerDay = Arrays.asList(89,129,149);
private double totalcharge;
private double deposit;
}
关于吸气剂和吸气剂
As all the variables in the classes are private, I am not sure how to call these variables
private
变量在定义它们的 class 之外是不可见的。
如果你想从另一个class访问私有变量的值,那么你需要提供一个具有更高可见性的访问器方法。那将是 getter 和(如有必要)setter。然后在封装 class 上调用它来访问封装的变量值,例如dog.getId()
.
大多数 IDE 可以为您自动生成 getter 和 setter。我建议使用 IDE functionality to automatically generate standard code 等构造函数、getters、setters、toString 和 hashCode 函数。
示例应用程序
对于您的示例,您可以使用如下所示的内容。这个例子可能比您需要的要复杂一些。但也许你可以通过研究它来了解一些东西。
应用输出
Opening shop
Servicing customer: Customer{id=0, dogs=[Dog{id=1, name='Fido', ownerId=0}, Dog{id=2, name='Dido', ownerId=0}]}
Servicing customer: Customer{id=3, dogs=[Dog{id=4, name='Poly', ownerId=3}, Dog{id=5, name='Anna', ownerId=3}]}
Servicing customer: Customer{id=6, dogs=[Dog{id=7, name='Butch', ownerId=6}]}
Closing shop
狗狗美容服务
import java.util.ArrayList;
public class DogGroomingService {
private ArrayList<Customer> customers = new ArrayList<>();
private void enrollCustomers() {
addCustomer("Fido", "Dido");
addCustomer("Poly", "Anna");
addCustomer("Butch");
}
private void addCustomer(String... dogNames) {
Customer customer = new Customer();
for (String dogName: dogNames) {
Dog dog = new Dog();
dog.setName(dogName);
customer.addDog(dog);
}
customers.add(customer);
}
private void serveCustomers() {
System.out.println("Opening shop");
for (Customer customer: customers) {
System.out.println("Servicing customer: " + customer);
}
System.out.println("Closing shop");
}
public static void main(String[] args) {
DogGroomingService groomingService = new DogGroomingService();
groomingService.enrollCustomers();
groomingService.serveCustomers();
}
}
IdService
public class IdService {
private static final IdService instance = new IdService();
private int nextId = 0;
public int generateNextId() {
return nextId++;
}
public static IdService getInstance() {
return instance;
}
}
客户
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
public class Customer {
private int id;
private final Set<Dog> dogs = new HashSet<>();
public Customer() {
this.id = IdService.getInstance().generateNextId();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Set<Dog> getDogs() {
return Collections.unmodifiableSet(dogs);
}
public void addDog(Dog dog) {
dogs.add(dog);
dog.setOwner(this);
}
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", dogs=" + dogs +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Customer customer = (Customer) o;
return id == customer.id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}
狗
import java.util.Objects;
public class Dog {
private int id;
private String name;
private Customer owner;
public Dog() {
id = IdService.getInstance().generateNextId();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Customer getOwner() {
return owner;
}
public void setOwner(Customer owner) {
this.owner = owner;
}
@Override
public String toString() {
return "Dog{" +
"id=" + id +
", name='" + name + '\'' +
", ownerId=" + owner.getId() +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Dog dog = (Dog) o;
return id == dog.id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}
一般注意事项
我使用 IDE (getters/setters/equals/hashcode).
自动生成了一些代码我确实对生成的代码做了一些小修改,例如在为狗生成字符串时,我只引用客户 ID 而不是完整的客户数据,否则,您将陷入无限循环,因为在这些实体之间建立了 two-way 引用。
如果您不熟悉它们,请参阅 Baeldung tutorial on equals and hashcode,以及为什么您可能会使用它们而不是像您在问题代码中那样实施 Comparable。
Comparable 用于订购,但是一只狗比另一只狗少或一位顾客比另一位顾客多是什么意思?也许您可以根据狗名或客户 ID 定义默认订单(我不知道这取决于您的要求)。也许定义比较器可能更好。查看差异:
Follow-up 其他问题
I have two classes Customer information and Dog information, which will include the following data variables, customer ID, dog ID list (contains the dog ID for each dog owned by the customer).
I have Dog information class which takes the dog information and the owner ID(Customer ID) of that dog.
I am not sure how to add the dog ID from the dog class to the corresponding customer (owner of the dog) in the customer class dog list
如果您只想存储 ID 而不是完整的对象引用,您可以这样做。
参见下面的客户 addDog
方法:
public void addDog(Dog dog) {
dogIds.add(dog.getId());
dog.setOwnerId(this.id);
}
但是,您确实无法查看 ID 之外的对象中的其他信息。要找出答案,您需要提供单独的函数来按 ID 查找完整的对象数据。例如:
public interface DataAccess {
Customer findCustomerById(int customerId);
Dog findDogById(int dogId);
}
我不会在这里提供那些查找功能的实现,因为我不知道您如何在应用程序中存储和检索数据。
这里是一些修改后的输出和 classes,它们仅通过 ID 而不是对象引用来引用客户和狗。
Opening shop
Servicing customer: Customer{id=0, dogIds=[1, 2]}
Servicing customer: Customer{id=3, dogIds=[4, 5]}
Servicing customer: Customer{id=6, dogIds=[7]}
Closing shop
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
public class Customer {
private int id;
private final Set<Integer> dogIds = new HashSet<>();
public Customer() {
this.id = IdService.getInstance().generateNextId();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Set<Integer> getDogIds() {
return Collections.unmodifiableSet(dogIds);
}
public void addDog(Dog dog) {
dogIds.add(dog.getId());
dog.setOwnerId(this.id);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Customer customer = (Customer) o;
return id == customer.id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", dogIds=" + dogIds +
'}';
}
}
import java.util.Objects;
public class Dog {
private int id;
private String name;
private int ownerId;
public Dog() {
id = IdService.getInstance().generateNextId();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getOwnerId() {
return ownerId;
}
public void setOwnerId(int ownerId) {
this.ownerId = ownerId;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Dog dog = (Dog) o;
return id == dog.id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return "Dog{" +
"id=" + id +
", name='" + name + '\'' +
", ownerId=" + ownerId +
'}';
}
}