---
# System prepended metadata

title: '[Java] 類別(3) 建構元的多載'
tags: [多載, Java, 建構元]

---

# [Java] 類別(3) 建構元的多載
###### tags: `Java` `建構元` `多載`

---

```java=
class Car
{
	private int age;
	private String brand;
	public Car(int a, String b) {
		age = a;
		brand = b;
	}
	public Car(int a) {
		age = a;
	}
	public Car(String b) {
		brand = b;
	}
	public void show() {
		System.out.println("Brand is " + brand + ", Age is "+age);
	}
}

public class app2_1
{
   public static void main(String args[])
   {
		   Car cir1 = new Car(5, "benz");
		   Car cir2 = new Car(5);
		   Car cir3 = new Car("benz");
		   cir1.show();
		   cir2.show();
		   cir3.show();
   }
}
```
output:
Brand is benz, Age is 5
Brand is null, Age is 5
Brand is benz, Age is 0


---


再使用更多的多載建構元

```java=
class Car
{
	private int age;
	private String brand;
	public Car(int a, String b) {
		age = a;
		brand = b;
	}
	public Car(int a) {
		age = a;
	}
	public Car(String b) {
		brand = b;
	}
	public Car() {
		age = 1;
		brand = "toyota";
	}
	public void show() {
		System.out.println("Brand is " + brand + ", Age is "+age);
	}
}

public class app2_1
{
   public static void main(String args[])
   {
		   Car cir1 = new Car(5, "benz");
		   Car cir2 = new Car(5);
		   Car cir3 = new Car("benz");
		   Car cir4 = new Car();
		   cir1.show();
		   cir2.show();
		   cir3.show();
		   cir4.show();
   }
}
```
output:
Brand is benz, Age is 5
Brand is null, Age is 5
Brand is benz, Age is 0
Brand is toyota, Age is 1

---

多載的建構元呼叫另一個多載的建構元

```java=
class Car
{
	private int age;
	private String brand;
	public Car(int a, String b) {
		age = a;
		brand = b;
	}
	public Car() {
		this(5, "toyota"); // 呼叫第5行的建構元Car(int a, String b)
	}
	public void show() {
		System.out.println("Brand is " + brand + ", Age is "+age);
	}
}

public class app2_1
{
   public static void main(String args[])
   {
		   Car cir1 = new Car(); // 呼叫第9行的建構元Car()
		   cir1.show();
   }
}
```
output:
Brand is toyota, Age is 5

---

透過類別變數來計算呼叫建構元的次數

```java=
class Car
{
	private int age;
	private static int count=0; //宣告count為類別變數
	private String brand;
	public Car(int a, String b) {
		age = a;
		brand = b;
	}
	public Car() {
		this(5, "toyota"); 
		count++;
	}
	public void show() {
		System.out.println("Brand is " + brand + ", Age is "+age);
	}
	public void show_count() {
		System.out.println("count is "+count);
	}
}

public class app2_1
{
   public static void main(String args[])
   {
		   Car cir1 = new Car(); // 呼叫一次建構元 count+1
		   cir1.show();
		   
		   Car cir2 = new Car(); // 再呼叫一次建構元 count+1
		   cir2.show();
		   cir2.show_count();
   }
}
```
output:
Brand is toyota, Age is 5
Brand is toyota, Age is 5
count is 2

---

將物件A設給物件B
再修改物件A的資料
去觀察物件A與物件B的資料內容
```java=
class Car
{
	private int age;
	private String brand;
	public Car(int a, String b) {
		age = a;
		brand = b;
	}
	public Car() {
		this(5, "toyota"); 
	}
	public void show() {
		System.out.println("Brand is " + brand + ", Age is "+age);
	}
	public void set(int a, String b) {
		age = a;
		brand = b;
	}
	public void compare(Car c) {
		if(this.age > c.age) {
			System.out.println("car1's age is bigger than car2");
		}else if(this.age < c.age) {
			System.out.println("car2's age is bigger than car1");
		}else {
			System.out.println("the same");
		}
	}
}

public class app2_1
{
   public static void main(String args[])
   {
		   Car car1 = new Car();
		   car1.show();
		   
		   Car car2 = new Car();
		   car2.show();
		   
		   car1.compare(car2); // 比較兩個車子物件的age
		   
		   car2.set(10, "benz"); // 更改物件car2的兩個資料成員內容
		   car2.show();
		   car1.compare(car2); // 再比較兩個車子物件的age
		   
		   car1 = car2; // 物件car1指到物件car2，此時兩個物件的內容均等
		   car2.set(8, "mazda"); // 改變物件car2的兩個資料成員
		   car1.compare(car2); // 注意!上一行改變物件car2的兩個資料成員的同時，物件car2的兩個資料成員也同時改變
		   
		   System.out.println("+++++++++++++++++++++");
		   
		   int i=5,j=i; // 簡單的變數與物件不同
		   System.out.println("i = "+i+", j = "+j);
		   i = 6; // 不會改變j的值
		   System.out.println("i = "+i+", j = "+j);
   }
}
```
output:
Brand is toyota, Age is 5
Brand is toyota, Age is 5
the same
Brand is benz, Age is 10
car2's age is bigger than car1
the same
+++++++++++++++++++++
i = 5, j = 5
i = 6, j = 5



可觀察程式碼第46行，將物件car2設給物件car1，其實等同於物件car1「指向」car2，而當物件car2資料成員變動時，物件car1的資料成員內容亦變動。

---

進階範例1.
兩個物件可以拿來比較是否相等

```java=
class Car
{
	private int age;
	private String brand;
	public Car(int a, String b) {
		age = a;
		brand = b;
	}
	public Car compare(Car c) {
		if(this.age > c.age) {
			return this;
		}else {
			return c;
		}
	}
}

public class app2_1
{
   public static void main(String args[])
   {
	   Car car1 = new Car(5, "benz");
	   Car car2 = new Car(8, "toyota");
	   Car car3;
	   
	   car3 = car1.compare(car2);
	   if(car3 == car1) {
		   System.out.println("car1的age比較大");
	   }else {
		   System.out.println("car1的age沒有比較大");
	   }
   }
}
```
output:
car1的age沒有比較大

---

進階範例2.
學習使用物件陣列

```java=
class Car
{
	private int age;
	private String brand;
	public Car(int a, String b) {
		age = a;
		brand = b;
	}
	public static String compare(Car a[]) {
		int find_max = a[0].age;
		String max_car = a[0].brand;
		for(int i=0; i<a.length; i++) {
			if(find_max < a[i].age) {
				find_max = a[i].age;
				max_car = a[i].brand;
			}
		}
		return max_car;
	}
}

public class app2_1
{
   public static void main(String args[])
   {
	   Car c[] = new Car[3];  // 宣告物件陣列c，此宣告3個車子物件
	   c[0] = new Car(3, "mazda");
	   c[1] = new Car(5, "toyota");
	   c[2] = new Car(10, "benz");
	   System.out.println(Car.compare(c)+"的age最大"); // 要記得compare函數是被宣告為static，也就是可以將類別名稱當作名稱空間來呼叫函數
   }
}
```
output:
benz的age最大

---

撰寫內部類別

```java=
public class app2_1
{
   public static void main(String args[])
   {
	   Car c[] = new Car[3];  // 宣告物件陣列c，此宣告3個車子物件
	   c[0] = new Car(3, "mazda");
	   c[1] = new Car(15, "toyota");
	   c[2] = new Car(10, "benz");
	   System.out.println(Car.compare(c)+"的age最大"); // 要記得compare函數是被宣告為static，也就是可以將類別名稱當作名稱空間來呼叫函數
   }
   static class Car
   {
	   int age;
	   String brand;
	   public Car(int a, String b) {
		   age = a;
		   brand = b;
	   }
	   public static String compare(Car a[]) {
		   int find_max = a[0].age;
		   String max_car = a[0].brand;
		   for(int i=0; i<a.length; i++) {
			   	if(find_max < a[i].age) {
			   		find_max = a[i].age;
			   		max_car = a[i].brand;
			   	}
		   }
		   return max_car;
	   }
	}
}
```
output:
toyota的age最大

---

用建構元建立內部類別的物件

```java=
public class app2_1
{
	public app2_1() {
		Car car[] = new Car[3];
		car[0] = new Car(3, "toyota");
		car[1] = new Car(5, "benz");
		car[2] = new Car(7, "mazda");
		System.out.println(Car.compare(car)+"的age最大");
	}
	public static void main(String args[])
	{
		app2_1 app = new app2_1();
	}
	static class Car
	{
		int age;
	    String brand;
	    public Car(int a, String b) {
	    	age = a;
		    brand = b;
	    }
	    public static String compare(Car a[]) {
		    int find_max = a[0].age;
		    String max_car = a[0].brand;
		    for(int i=0; i<a.length; i++) {
		    	if(find_max < a[i].age) {
		    		find_max = a[i].age;
		    		max_car = a[i].brand;
			   	}
		    }
		    return max_car;
	    }
	}
}
```
output:
mazda的age最大

---

建立匿名物件

```java=
public class app2_1
{
	public static void main(String args[]) {
		(new Car() {
			void set_brand(int a, String b) {
				brand = b;
				age = a;
				System.out.println("brand is "+brand+", age is "+age);
			}
		}).set_brand(5, "toyota");
	}
	static class Car{
		String brand;
		int age;
	}
}
```
output:
brand is toyota, age is 5

---

使用巢狀類別
```java=
class Car{ // 外部類別
	private int age;
	private int kilometer;
	private Login login;
	public Car(int a, int k, String b) {
		age = a;
		kilometer = k;
		login = new Login(b);
	}
	class Login{ // 內部類別
		private String brand;
		public Login(String s) {
			brand = s;
		}
		public void show() {
			System.out.println("車子品牌: "+brand);
		}
	}
	public void output() {
		System.out.println("車子品牌: "+login.brand);
		System.out.println("車齡: "+age+" 年");
		System.out.println("已行駛公里數: "+kilometer+" km");
		login.show();
	}
}

public class app2_1
{
	public static void main(String args[]) {
		Car car1 = new Car(5, 30000, "toyota");
		Car car2 = new Car(7, 38000, "benz");
		
		car1.output();
		System.out.println();
		car2.output();
	}
}
```

output:
車子品牌: toyota
車齡: 5 年
已行駛公里數: 30000 km
車子品牌: toyota

車子品牌: benz
車齡: 7 年
已行駛公里數: 38000 km
車子品牌: benz
