• Android方法的传递值及其改变


    MainActivity如下:

    package cn.testchangevar;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.app.Activity;
    /**
     * Demo描述:
     * Java中的传值及其改变
     *
     */
    public class MainActivity extends Activity {
    	private int id=7;
    	private Person mPerson;
        private Button mButton;
        private String mString;
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
    		init();
    	}
       private void init(){
    	    mString=new String("OOOOO");
    	    mPerson=new Person("xx", 22);
    	    mButton=(Button) findViewById(R.id.button);
    	    mButton.setOnClickListener(new OnClickListener() {
    			@Override
    			public void onClick(View view) {
    				System.out.println("111 id="+id);
    				System.out.println("111 mString="+mString);
    				System.out.println("111 mPerson.getAge()="+mPerson.getAge());
    				System.out.println("=========================");
    				Util.change(id);
    				Util.change(mString);
    				Util.change(mPerson);
    				System.out.println("=========================");
    				System.out.println("222 id="+id);
    				System.out.println("222 mString="+mString);
    				System.out.println("222 mPerson.getAge()="+mPerson.getAge());
    			}
    		});
       }
    }
    


     

    Util如下:

    package cn.testchangevar;
    
    public class Util {
       //不可以改变.因为int属于基础类型
       public static void change(int i){
    	   i=9527;
    	   System.out.println("has been changed i="+i);
       }
      
       //可以改变.传递的是对象
       public static void change(Person person){
    	   person.setAge(88);
    	   System.out.println("has been changed string="+person);
       }
       
       //不可以改变.虽然String也是对象,但是较为特殊
       public static void change(String string){
    	   string="XXXXX";
    	   System.out.println("has been changed string="+string);
       }
    }
    


    Person如下:

    package cn.testchangevar;
    
    public class Person {
        private String name;
        private int age;
    	public Person(String name, int age) {
    		super();
    		this.name = name;
    		this.age = age;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public int getAge() {
    		return age;
    	}
    	public void setAge(int age) {
    		this.age = age;
    	}
    	@Override
    	public String toString() {
    		return "Person [name=" + name + ", age=" + age + "]";
    	}
    }
    


    main.xml如下:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         >
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world"
            android:layout_centerInParent="true"
        />
    
    </RelativeLayout>


     

  • 相关阅读:
    Spring(001)-Hello Spring
    actuator beans不展示
    Java读取property文件
    PostgreSQL 后端存储
    python 中global() 函数
    python项目配置文件格式
    大数据时代,数据成了研究的基石
    10w数据重建索引报错:java.lang.IllegalStateException: Batch statement cannot contain more than 65535 statements
    java学习day15--API-ArrayList--LinkedList
    java学习day14--API-集合(Collection)+List
  • 原文地址:https://www.cnblogs.com/aukle/p/3221659.html
Copyright © 2020-2023  润新知