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

时间:2018-01-04 05:15:51

标签: java android kotlin getter-setter

我有一个带有默认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

public class 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 个答案:

答案 0 :(得分:-1)

修改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);

然后

<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Calculator</title>
    </head>
    <body>
    HTML Code is here:

         <form method="post">
            <input type="text" name="numb1">
            <input type="text" name="numb2">
            <select name="operator" id="">
               <option>None</option>
               <option>Add</option>
               <option>Subtract</option>
               <option>Multiply</option>
               <option>Divide</option>
               <option>Square</option>
            </select>
            <button type="submit" name="submit" value="submit">Calculate</button>
         </form>

    PHP Code:

        <?php 

            if (isset($_POST['submit'])) {
                $result1 = $_POST['numb1'];
                $result2 = $_POST['numb2'];
                $operator = $_POST['operator'];
                switch ($operator) {
                    case 'None':
                        echo "You need to select any operator";
                        break;
                    case 'Add':
                        echo $result1 + $result2;
                        break;
                    case 'Subtract':
                        echo $result1 - $result2;
                        break;
                    case 'Multiply':
                        echo $result1 * $result2;
                        break;
                    case 'Divide':
                        echo $result1 / $result2;
                        break;
                    case 'Square':
                        echo $result1 ** $result2;
                        break;
                }
            }


         ?>
        enter code here

    </body>
    </html>