我有一个存储其他对象的通用数组类。当我尝试从泛型数组中获取对象值时,它会打印插入对象类路径。如何访问该值?
public class Shop implements Initializable {
@FXML private TextField name;
@FXML private TextField quantity;
@FXML private TextArea list;
MyArrayList<Item> array = new MyArrayList<>();
@Override
public void initialize(URL location, ResourceBundle resources) {
}
@FXML
private void addItem() {
int q = Integer.parseInt(quantity.getText());
String n = name.getText();
Item item = new Item(n,q);
MyArrayList.getInstance().add(item);
System.out.println(MyArrayList.getInstance().getItem(0));
//Outputs sample.Item@1674184b instead of the value Banana.
}
答案 0 :(得分:1)
所以你将整数和字符串放入项目中? MyArrayList.getInstance()。getItem(0).getName()或 MyArrayList.getInstance()。的getItem(0).getQuantity() 应该打印出你输入的值。
如果你没有get方法创建它们,或者这些字段是公共的,你可以直接访问它们。
如果你想从列出的类中获取所有值,你可能想要覆盖toString()方法。func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: StepperTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! StepperTableViewCell
//stepper is your UIStepper reference from StepperTableViewCell
cell.stepper.tag = indexPath.row
cell.stepper.addTarget(self, action: #selector(stepperAction(sender:)), for: .valueChanged)
return cell
}
func stepperAction(sender: UIStepper) {
print("Stepper \(sender.tag) clicked. Its value \(sender.value)")
}
并致电
@Overwrite
public String toString(){
return name + "," + quantity;
}