Android方法的傳遞值及其改變

來源:互聯網
上載者:User

MainActivity如下:

[java]
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()); 
            } 
        }); 
   } 

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如下:

[java]
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); 
   } 

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如下:

[java]
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 + "]"; 
    } 

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如下:

[html]
<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> 

<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>
 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.