可以通过kotlin默认的getter和setter在Java中初始化Kotlin数据成员吗?

我有一个带有默认getter和setter的Kotlin数据成员类: CurrentOrder.kt

class CurrentOrder {
private var currentOrderTime: String
get() = currentOrderTime
set(value){
    currentOrderTime = value
}

private var currentOrderPrice: String
get() = currentOrderPrice
    set(value){
        currentOrderPrice = value
    }
}

我想在java类中初始化: OrdersFragment.java

公共类OrdersFragment扩展Fragment {

RecyclerView recyclerView;
ArrayList<CurrentOrder> currentOrders;

public OrdersFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    getActivity().setTitle("Orders");
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_orders, container, false);
    recyclerView = (RecyclerView) v.findViewById(R.id.id_current_order_container);

    currentOrderInit(); // Initializing ArrayList<CurrentOrder>

    return v;
}

private void currentOrderInit() {
    currentOrders = new ArrayList<>();
    for (int i = 0; i < 2; i++){
        CurrentOrder currentOrder = new CurrentOrder();

        // How can I initialize these members?
    }

  }

}

我该如何初始化这些成员?可能吗?

回答(1)

3 years ago

private 访问者修改为 public

class CurrentOrder {
    public var currentOrderTime: String
    get() = currentOrderTime
    set(value){
        currentOrderTime = value
    }

    public var currentOrderPrice: String
    get() = currentOrderPrice
        set(value){
            currentOrderPrice = value
        }
    }

然后

CurrentOrder currentOrder = new CurrentOrder();
currenrOrder.setCurrentOrderTime(somevalue);