集册 Java实例教程 修改方法内部数组参数的数组元素的状态

修改方法内部数组参数的数组元素的状态

欢马劈雪     最近更新时间:2020-01-02 10:19:05

537
修改方法内部数组参数的数组元素的状态

class Item {
/* from 
时 代 J a v a - N o w J a v a . c o m*/

  private double price;

  private String name;


  public Item (String name, double initialPrice) {

    this.name = name;

    this.price = initialPrice;

  }


  public double getPrice() {

    return this.price;

  }


  public void setPrice(double newPrice ) {/**时 代 J a v a - nowjava.com**/

    this.price = newPrice;

  }


  public String toString() {

    return "[" + this.name + ", " + this.price + "]";

  }

}

public class Main {

  public static void main(String[] args) {

    Item[] myItems = {new Item("Pen", 25.11), new Item("Pencil", 0.10)};

    System.out.println("Before method call #1:" + myItems[0]);

    System.out.println("Before method call #2:" + myItems[1]);


    // Call the method passing the array of Item

    tryStateChange(myItems);


    System.out.println("After method call #1:" + myItems[0]);

    System.out.println
展开阅读全文