私は2つのクラスを最初に1つのメンバーと2番目のストアを作成しました。メンバー クラスからオブジェクトを作成できるメソッドを作成し、メンバー クラスに Store 型のフィールド ストアを作成しようとしています。メンバーが入力したストアへの参照を保存したいと考えています。これを行うように言われた人もいます: memberRegister() は、引数として、現在いる Store オブジェクトへのポインターを渡す必要があります。
実際、Store オブジェクトは、Member オブジェクトに「自分を指している」ことを伝えることができる必要があります。つまり、Store オブジェクトにはそれ自体へのポインターが必要です。しかし、私はそれを取得しませんでした
これはメンバークラスです
private int pinNumber;
private String name, id;
private Store store;
/**
* Constructor for objects of class Member
*/
public Member(String name, String id, int pinNumber, Store store)
{
// initialise instance variables
this.name = name;
this.id = id;
this.pinNumber = pinNumber;
checkId();
checkPinNumber();
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
private void checkId()
{
// put your code here
int length;
length = id.length();
if (length > 10 ){
System.out.println("lentgh must be at 10 ");
}
}
private void checkPinNumber()
{
int length;
length = id.length();
if ((length > 4) && (length < 4 )){
System.out.println("lentgh must be at 4");
}
クラスストア
private String storeName;
private int total;
private Member member;
/**
* Constructor for objects of class Store
*/
public Store(String storeName, int total)
{
// initialise instance variables
this.storeName = storeName;
this.total = total;
}
/**
*
*/
public String getStoreName()
{
return storeName;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public Member memberRegister(String name, String id, int pinNumber)
{
// put your code here
Member member;
member = new Member(name, id, pinNumber)
return member;
}