- 阅读权限
- 255
- 威望
- 1 级
- 论坛币
- 49407 个
- 通用积分
- 51.9304
- 学术水平
- 370 点
- 热心指数
- 273 点
- 信用等级
- 335 点
- 经验
- 57815 点
- 帖子
- 4006
- 精华
- 21
- 在线时间
- 582 小时
- 注册时间
- 2005-5-8
- 最后登录
- 2023-11-26
|
- /**
- * MethodCall
- * Copyright 2006 by Jeff Heaton(jeff@jeffheaton.com)
- *
- * Example program from Chapter 6
- * Java for the Beginning Programmer
- * http://www.heatonresearch.com/articles/series/15/
- *
- * This class shows how objects can be used to return values from methods.
- *
- * This software is copyrighted. You may use it in programs
- * of your own, without restriction, but you may not
- * publish the source code without the author's permission.
- * For more information on distributing this code, please
- * visit:
- * http://www.heatonresearch.com/hr_legal.php
- *
- * @author Jeff Heaton
- * @version 1.1
- */
- import javax.swing.*;
- public class MethodCall
- {
- static void changeValue(JButton button)
- {
- // Change the text of the button, this
- // new value is reflected outside of the
- // call to "changeValue"
- button.setText("New value");
- }
- static void changeReference(JButton button)
- {
- // Create a new button, and assign its
- // reference to "button". This change is
- // not reflected outside of the call
- // to "changeValue"
- button = new JButton("New value");
- }
- static void changePrimitive(int i)
- {
- i = i + 1;
- }
- /**
- * Main entry point for example.
- * @param args Not used.
- */
- public static void main(String args[])
- {
- // setup the variables
- JButton button1 = new JButton("Old Value");
- JButton button2 = new JButton("Old Value");
- int var = 5;
- // call the methods
- changeValue(button1);
- changeReference(button2);
- changePrimitive(var);
- // display the new values
- System.out.println("Button1:" +
- button1.getText());
- System.out.println("Button2:" +
- button2.getText());
- System.out.println("Primitive variable:" + var);
- }
- }
复制代码
|
|