このようなArrayListから継承するクラスのコレクションを参照するには、どのようにTHISキーワードを使用できますか?私は設計時にエラーが発生し、IDEは自分のコードをコンパイルできません。これをコレクションクラス内で使用する方法
public class Company{
private EmployeeCollection employees;
public Company(){
this.employees = new EmployeeCollection();
this.employees.add(new Employee());
this.employees.add(new Employee());
this.employees.add(new Employee());
this.employees.add(new Employee());
this.employees.add(new Employee());
}
public void MyMethod(){
Employee fourthEmployee = employees.getFourth();
}
}
public class EmployeeCollection extends ArrayList<Employee>{
public Employee getFourth(){
return this[3]; //<-- Error
}
public Employe getEmployee(int id){
for(int i = 0; i< this.size(); i++){ //<-- Error
if(id == this[i].id){ //<-- Error
return this[i]; //<-- Error
}
}
}
}
通常はC#で、私はあなたがthis
キーワードを使用することができ、この
public object test(int id)
{
for (int i = 0; i < this.Count; i++)
{
if (this[i].ID == id)
{
return this[i];
}
}
return null;
}
リストは配列ではないので、配列の構文は機能しません。代わりに 'this.get(3);'を使うことができます。 – JonK
ところで、 'this.size()'には何のエラーもありません。 – SomeDude