集册 Java实例教程 包含名称实例变量和设置和获取其值的方法的帐户类。

包含名称实例变量和设置和获取其值的方法的帐户类。

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

531
包含名称实例变量和设置和获取其值的方法的帐户类。
/*来自 
 N o w  J a v a  . c o m*/

import java.util.Scanner;


public class Main

{

   public static void main(String[] args)

   { 

      // create a Scanner object to obtain input from the command window

      Scanner input = new Scanner(System.in);


      // create an Account object and assign it to myAccount

      Account myAccount = new Account(); 


      // display initial value of name (null)

      System.out.printf("Initial name is: %s%n%n", myAccount.getName());


      // prompt for and read name

      System.out.println("Please enter the name:");/** 来 自 n o w    j a v a  . c o m**/

      String theName = input.nextLine(); // read a line of text

      myAccount.setName(theName); // put theName in myAccount

      System.out.println(); // outputs a blank line


      // display the name stored in object myAccount

      System.out.printf("Name in object myAccount is:%n%s%n",

         myAccount.getName());

   } 

}

class Account

{

   private String name; // instance variable

 

   // method to set the name in the object              

   public void setName(String name)      

   {                                             

      this.name = name; 
展开阅读全文