/*
- 變量的賦值
- 變量是基本數據類型時:此時賦值的是變量所保存的數據值
- 變量是引用數據類型時:此時賦值的是變量所保存的數據的地址值
- 方法中參數的傳遞機制
- 方法必須由其所在類或對象調用纔有意義。若方法含有參數:
- 實參:方法調用時實際傳遞給形參的參數值
- 形參:方法聲明時的參數
- Java中方法的參數傳遞方式只有一種:值傳遞。將實際參數值的副本傳入方法內,而參數本身不受影響。
- 形參是基本數據類型時:將實參的基本數據類型的數據值傳遞給形參
- 形參是引用數據類型時:將實參的引用數據類型變量的地址值傳遞給形參
*/
package com.atguigu.contact;
import java.util.*;
public class Object6 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//變量賦值情況
int a = 23;
int b = a;
b = 11;
System.out.println("a=" + a + ",b=" + b);
Arguments argu = new Arguments();
argu.num = 33;
Arguments argu1 = argu;
System.out.println(argu.num + " " + argu1.num);
argu1.num = 22;
System.out.println(argu.num + " " + argu1.num);
//方法形參傳遞測試1
//交換方法形參爲基本數據類型,變量m,n與方法形參m,n都在棧空間中存儲,指向兩套地址。
int m = 11;
int n = 22;
System.out.println("m = " + m + ",n = " + n);//m = 11,n = 22
argu.exchange(m, n);
System.out.println("m = " + m + ",n = " + n);//m = 11,n = 22,交換失敗
//交換方法形參爲引用基本數據類型,變量d與方法中date指向同一地址值,操作同一塊堆空間。
Date d = new Date();
d.m = 11;
d.n = 22;
System.out.println("m = " + d.m + ",n = " + d.n);//m = 11,n = 22
argu.swap(d);
System.out.println("m = " + d.m + ",n = " + d.n);//m = 22,n = 11,交換成功
Date d1 = new Date();
d1.m = 11;
d1.n = 22;
System.out.println("m = " + d1.m + ",n = " + d1.n);//m = 11,n = 22
argu.swap1(d1);
System.out.println("m = " + d1.m + ",n = " + d1.n);//m = 11,n = 22,交換失敗
//方法形參傳遞測試2
Object6 o = new Object6();
o.frist();
//0, 9
//17, 7
//遍歷數組每個元素除以第一個元素形成新數組
//說明:數組元素在運算後會被改變賦值,需要創建臨時變量儲存首元素值,或者倒序遍歷
int[] arr = new int[] {11,23,45,6,78,7,90,12,11};
System.out.println(Arrays.toString(arr));
//錯誤方式
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[i] / arr[0];
}
System.out.println(Arrays.toString(arr));
//正確方式1
int[] arr1 = new int[] {11,23,45,6,78,7,90,12,11};
for (int i = arr1.length - 1; i >= 0; i--) {
arr1[i] = arr1[i] / arr1[0];
}
System.out.println(Arrays.toString(arr1));
//正確方式2
int[] arr2 = new int[] {11,23,45,6,78,7,90,12,11};
int temp = arr2[0];
for (int i = 0; i < arr2.length; i++) {
arr2[i] = arr2[i] / temp;
}
System.out.println(Arrays.toString(arr2));
//測試題3,println方法的特例
int[] array = new int[] {1,2,3};
System.out.println(array);//輸出地址值
char[] array1 = new char[] {'a','b','c'};
System.out.println(array1);//輸出abc
//測試題4,輸出圓的面積
CircleA c = new CircleA();
c.radius = 4.5;
System.out.println(c.findArea());
ErgodicCircleA circle = new ErgodicCircleA();
circle.printAreas(c, 10);
//circle.printAreas(new CircleA(),10);只調用一次Circle類,可以使用匿名對象的形式
//測試題5,無關方法值傳遞
int i = 10;
int j = 20;
method(i,j);
//編寫方法method,實現輸出 i = 100,j = 200
System.out.println("i = " + i);
System.out.println("j = " + j);
}
public static void method(int i,int j) {
i = i * 10;
j = j * 10;
System.out.println("i = " + i);
System.out.println("j = " + j);
System.exit(0);//退出JVM,不在輸出後面的語句
}
//方法二,方法重寫,調用新的println方法,輸出特定結果
// public static void method1(int i,int j) {
// PrintStream ps = new PrinStream(System.out)
// {
// public void printlin(String x) {
// if("i = 10".equals(x)) {
// x = "i = 100";
// }else if("j = 20".equals(x)) {
// x = "j = 200";
// }
// super.println(x);
//
// }
// };
// System.setOut(ps);
// }
//Object6 類內創建方法frist,second以供測試2調用
public void frist() {
int m = 7;
Date d = new Date();
d.m = 11;
second(d,m);
System.out.println(d.m + ", " + m);
}
public void second(Date d,int m) {
m = 9;
d.m = 17;
Date da = new Date();
d = da;
System.out.println(d.m + ", " + m);
}
}
//爲測試一創建類Arguments
class Arguments{
int num;
public int setValue(int i) {
return i;
}
public void exchange(int m,int n) {//方法內參數爲基本數據類型,接收實參數據值,交換方法內變量m,n,與方法外變量m,n無關
int temp;
temp = m;
m = n;
n = temp;
}
public void swap(Date date) {//參數是引用數據類型時交換地址值,這樣才能對方法外的變量實現交換
int temp;
temp = date.m;
date.m = date.n;
date.n = temp;
}
public void swap1(Date date) {
date = new Date();//相當於在堆中新建一個空間,date指向新地址值,後面語句不影響d
int temp;
temp = date.m;
date.m = date.n;
date.n = temp;
}
}
//創建類Date爲測試2使用
class Date{
int m ;
int n ;
}
//創建類CircleA爲測試4使用
class CircleA{
double radius;
public double findArea() {
return radius * radius * Math.PI;
}
}
//創建類ErgodicCircle爲測試4使用
class ErgodicCircleA{
public void printAreas(CircleA c,int i) {
System.out.println("Radius\t\tArea");
for (int j = 1; j <= i; j++) {
c.radius = j;
System.out.println(c.radius + "\t\t" + c.findArea());
}
}
}