まず、これは宿題の一部ですので、答える際はその点に注意してください。
そのため、CustomerList クラスが継承する汎用の LinkedList クラスを使用しようとしています。以下のコードはすべて私が書いたもので、課題の一部として与えられたものではありません。
正常に機能する顧客タイプを作成しました。問題はそのクラスとは関係ないため、コードは必要ありません。3 つの int フィールドと、その情報を出力するメソッドがあります。
public void printCustomerInfo() {
System.out.println([int field 1, 2, 3]);
}
この問題は、ジェネリック LinkedList クラスの挿入メソッド (私は信じています) に関係しています。決定されたクラスのオブジェクトを取り込み、現在の LinkedList オブジェクトの前のリストに挿入します。これは、現在の LinkedList オブジェクトのコピーを作成し、それを nextList として設定してから、現在の LinkedList オブジェクトのデータを指定された dataIn に変更することによって行われます。LinkedList クラスのコードは次のとおりです。
public class LinkedList<T> {
T data;
protected LinkedList<?> nextList;
public LinkedList() {
data = null;
nextList = null;
}
public boolean isEmpty() {
return (null == nextList);
}
public LinkedList<?> getNextList() {
return nextList;
}
public void insert(T dataIn) {
System.out.println("dataIn passed to insert method: \t" + dataIn);
LinkedList<T> tempList = new LinkedList<T>();
tempList.data = this.data;
tempList.nextList = this.nextList;
this.nextList = tempList;
this.data = dataIn;
System.out.println("data field of current object: \t\t" + data);
}
@SuppressWarnings("unchecked")
public T delete() {
T tempDump = data;
data = (T) nextList.data;
nextList = nextList.nextList;
return tempDump;
}
public void printInfo() {
if (isEmpty()) {
System.out.println("-END-");
} else {
System.out.println(data);
nextList.printInfo();
}
}
}
CustomerList クラスはそれを拡張し、データ型を顧客に設定します。コードは次のとおりです。
public class CustomerList extends LinkedList<Customer> {
Customer data;
public void printInfo() {
if (isEmpty()) {
System.out.println("-END-");
} else {
data.printCustomerInfo();
nextList.printInfo();
}
}
}
最後に、テスト オブジェクト:
public class GeneralTesting {
public static void main(String[] args) throws InterruptedException {
// Test LinkedList class
System.out.println(" - Create CustomerList and test methods");
CustomerList rList = new CustomerList();
System.out.println(" - Create a customer to store in the list");
Customer dude = new Customer(10, 65);
dude.setTimeServed(120);
System.out.println("proof that customer object exists: \t" + dude);
System.out.println(" - Insert customer into the list");
System.out.println("---method call: insert---");
rList.insert(dude);
System.out.println("---method end: insert----");
System.out.println("data in the list after return: \t\t" + rList.data);
}
}
これはコンソールが出力するものです:
- Create CustomerList and test methods
- Create a customer to store in the list
proof that customer object exists: assignment3.Customer@3c250cce
- Insert customer into the list
---method call: insert---
dataIn passed to insert method: assignment3.Customer@3c250cce
data field of current object: assignment3.Customer@3c250cce
---method end: insert----
data in the list after return: null
私が知る限り、これはスコーピングの問題です。おそらく、メソッドが解決されるときに無視されるレベルで変数を割り当てています。以前に同様の問題に遭遇し、解決できましたが、これを理解できません。残念ながら、私の教授は今後数日間外出するので、ここで助けを求めています。
渡されたオブジェクトにデータフィールドを設定するだけのメソッドを作成しようとしました:
public void setData(T dataIn) {
this.data = dataIn;
}
これでも、データは null から変更されませんでした。これは、Javaジェネリックを正しく理解していないことが原因であるに違いないことを私は知っています.