提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
在创建每个Account对象时,使用Account构造函数初始化名称实例变量。
public class Main { public static void main(String[] args) { /* 来 自 时代Java - N o w J a v a . c o m*/ // create two Account objects Account account1 = new Account("James Bond"); Account account2 = new Account("Michael Bates"); // display initial value of name for each Account System.out.printf("account1 name is: %s%n", account1.getName()); System.out.printf("account2 name is: %s%n", account2.getName()); } } class Account { private String name; // instance variable // constructor initializes name with parameter name public Account(String name) // constructor name is class name /* 来自 N o w J a v a . c o m*/ { this.name = name; } // method to set the name public void se