天天看點

構造方法和this的作用

一、構造方法概述

  • 構造方法是一個特殊的方法
  • 是建立對象時候調用的方法
  • 方法的名字很特殊:必須和類名保持一緻,大小寫都要一樣
  • 方法沒有傳回值
  • 方法也沒有傳回值類型
  • 構造方法無法在外部手動調用

public 類名(參數清單){

​ 構造方法的方法體

}

package com.qf.cons;

public class Demo01 {
	public static void main(String[] args) {
		System.out.println(">>>>>>>>>>>>>>>>");
		Stu stu01 = new Stu();
		System.out.println("==============");
		
		stu01.name = "張三";
		stu01.age = 23;
		
		stu01.show();

	}
}


class Stu{
	
	public Stu() {
		System.out.println("我是Stu空參的構造方法");
	}
	
	// 屬性name和age
	String name;
	int age;
	
	// 方法
	public void show() {
		System.out.println("我的名字是:" + name + ",我今年" + age);
	}
}
           

二、對象的建立過程和構造方法的調用

構造方法和this的作用

三、預設構造方法

  • 在我們建立類之後
    • 如果沒有在類中書寫任何構造方法,jvm會贈送一個空參的構造方法
    • 如果自己定義了構造方法,jvm不在贈送
package com.qf.cons;

public class Demo03 {
	public static void main(String[] args) {
		// Students s1 = new Students("zhangsan", 23, "壯士");
		
		Students s2 = new Students();
		
	}
}

class Students{
	// 屬性
	String name;
	int age;
	String gender;
	
	public Students() {
	}

	// 如果在類中沒有定義任何構造方法,jvm會預設給一個空參的構造方法
	// 如果在類中定義了任何構造方法,jvm不再贈送構造方法
	public Students(String name, int age, String gender) {
		this.name = name;
		this.age = age;
		this.gender = gender;
	}
	
}
           

四、this

4.1 定義

  • 我們在調用構造方法的時候可以傳入很多參數
  • 構造方法中的形參清單中的參數名字可能出現重複的問題
  • 建議把形參的名字定義成和對應屬性的名字一樣
  • 但是指派的時候,局部變量優先,指派可能産生問題:無法指派
  • 需要能直接調用到對象的屬性,再使用局部變量給屬性指派---this可以解決這個問題
package com.qf.cons;

public class Demo04 {
	public static void main(String[] args) {
		Dog dog = new Dog("道哥","10086");
		System.out.println(dog.name);
		System.out.println(dog.number);
		System.out.println(dog);
	}
}

class Dog{
	// 屬性
	String name;
	int age;
	String gender;
	String number;
	
	public Dog() {}
	
	public Dog(String n,int a,String g) {
		name = n;
		age = a;
		gender = g;
	}
	
	public Dog(String name,String number) {
		this.name = name;
		this.number = number;
		System.out.println(this);
	}
	
	
	// 方法
	
}
           

4.2 this代表誰?

  • this代表每一個對象
  • this是目前對象的引用
package com.qf.cons;

public class Demo04 {
	public static void main(String[] args) {
		// 建立對象dog
		Dog dog = new Dog("道哥","10086");
		System.out.println("dog:" + dog);
		dog.show();
		System.out.println(dog.name);
		System.out.println(dog.number);
		
		// 建立對象dog01
		Dog dog2 = new Dog("狗哥","10010");
		System.out.println("dog2:" + dog2);
		dog2.show();
		
		System.out.println("=======================");
		
		Dog dog3 = new Dog("狗爺","10011");
		System.out.println("dog3:" + dog3);
		dog3.show();
	}
}

class Dog{
	// 屬性
	String name;
	int age;
	String gender;
	String number;
	
	public Dog() {}
	
	public Dog(String n,int a,String g) {
		name = n;
		age = a;
		gender = g;
	}
	
	public Dog(String name,String number) {
		this.name = name;
		this.number = number;
	}
	
	
	// 方法
	public void show() {
		// 輸出每一個對象的this的位址
		System.out.println("this:" + this);
	}
}
           

4.3 this調用屬性和方法

  • this.屬性
    • 調用本類中的執行個體變量
  • this.方法()
    • 調用本類中的執行個體方法
package com.qf.cons;

public class Demo05 {
	public static void main(String[] args) {
		Cat cat = new Cat();
		cat.show();
	}
}

class Cat{
	// 屬性
	String name;
	int age;
	String gender;
	
	// 構造方法
	public Cat() {
		
	}
	
	public Cat(int age,String gender) {
		// this表示目前對象的引用,this.屬性  表示調用目前對象的某個屬性
		this.age = age;
		this.gender = gender;
	}
	
	public Cat(String name,int age,String gender) {
		
	}
	
	// 方法
	public void eat() {
		System.out.println("貓咪喜歡吃魚幹...");
	}
	
	public void sleep() {
		System.out.println("貓咪睡覺的時間一般在白天...");
	}
	
	/**
	 * 展示的方法,調用其他方法
	 */
	public void show() {
		this.eat();
		sleep();
	}
}
           

4.4 this調用構造方法

  • this(參數清單)
  • 注意:
    • 每一個構造器中隻能調用一次其他的構造方法
    • 構造器中調用構造方法,必須放在構造代碼的第一行
    • 構造器中可以調用執行個體方法,執行個體方法中不能調用構造器
package com.qf.cons;

public class Demo06 {
	public static void main(String[] args) {
		Car car = new Car("特斯拉",300000);
	}
}

class Car{
	// 屬性
	String brand;
	int price;
	String color;
	int weight;
	int width;
	int height;
	int length;
	
	// 構造方法
	public Car() {}
	
	public Car(String brand,int price) {
		this.brand = brand;
		this.price = price;
		// 在構造方法中調用執行個體方法
		this.show();
	}

	public Car(String brand,int price,String color) {
		this.brand = brand;
		this.price = price;
		this.color = color;
	}
	
	public Car(int length,int width,int height) {
		this.length = length;
		this.width = width;
		this.height = height;
	}
	
	public Car(String brand,int price,String color,int weight) {
		this.brand = brand;
		this.price = price;
		this.color = color;
		this.weight = weight;
	}
	
	public Car(String brand,int price,String color,int weight,int length) {
		// 調用自己的構造方法
		this(brand, price, color, weight);
		this.length = length;
	}
	
	public Car(String brand,int price,String color,int weight,int length,int width,int height) {
		// 調用構造方法隻能放在構造器的第一句中
		this(length,width,height);
		// this(brand, price, color, weight);
		this.length = length;
	}

	
	// 展示品牌和價格
	public void show() {
		// 執行個體方法不能調用構造器
		// this(brand, price, color, weight);
		System.out.println(this.brand + "===" + this.price);
	}
}
           

五、ATM案例

package com.qf.cons;

import java.util.Scanner;

public class Demo07 {
	
	public static void main(String[] args) {
		/**
		 * 銀行ATM
				模拟銀行賬戶業務,實作存款、取款和餘額查詢。運作效果如下所示:
			
				1.存款  2.取款  3.查詢  0.退出
				請選擇你要辦理的業務:1
				請輸入存款金額:1000
				---------
				存款成功!
			
				1.存款  2.取款  3.查詢  0.退出
				請選擇你要辦理的業務:2
				請輸入取款金額:100
				---------
				取款成功!
			
				1.存款  2.取款  3.查詢  0.退出
				請選擇你要辦理的業務:3
				---您目前賬戶餘額:900元---
			
				1.存款  2.取款  3.查詢  0.退出
				請選擇你要辦理的業務:0
			
				O(∩_∩)O謝謝您的使用,歡迎下次光臨!
		 */
		Scanner in = new Scanner(System.in);
		// 建立銀行卡
		ATM atm = new ATM(1000);
		
		// 開啟死循環,不斷提示用書輸入資料
		outer:while (true) {
			System.out.println("1.存款  2.取款  3.查詢  0.退出\r\n請選擇你要辦理的業務:");
			int select = in.nextInt();
			
			switch (select) {
			case 0:
				System.out.println("O(∩_∩)O謝謝您的使用,歡迎下次光臨!");
				break outer;
			
			// 存款的操作
			case 1:
				System.out.println("請輸入存款金額:");
				int m1 = in.nextInt();
				atm.saveMoney(m1);
				System.out.println("成功存入金額:" + m1 + ",餘額:" + atm.getBalance());
				break;
				
			// 取款的操作
			case 2:
				System.out.println("請輸入取款金額:");
				int m2 = in.nextInt();
				double ret = atm.takeMoney(m2);
				// 判斷ret是否大于0
				if (ret == m2) {
					System.out.println("成功取出:" + m2 + ",餘額:" + atm.getBalance());
				}else {
					System.out.println("餘額不足");
				}
				break;
				
			// 查詢餘額
			case 3:
				System.out.println("目前賬戶餘額:" + atm.getBalance());

			default:
				System.out.println("輸入有誤,請再次輸入...");
				break;
			}
		}
	}
}

/**
 * 自助櫃員機
 * 	定義了餘額
 * 	定義存、取、查詢的方法
 * @author Dushine2008
 *
 */
class ATM{
	// 屬性:餘額
	double balance;
	
	// 構造方法
	public ATM() {}
	
	public ATM(double balance) {
		this.balance = balance;
	}
	
	// 存錢
	public void saveMoney(int money) {
		this.balance += money;
	}
	
	// 取錢
	public double takeMoney(int money){
		// 判斷餘額是不是充足
		if (money <= balance) {
			this.balance -= money;
			return money;
		}
		
		return 0;
	}
	
	// 查詢
	public double getBalance() {
		return this.balance;
	}
}