Java基础——Day10——构造方法和this的含义和使用

Day 10

一、构造方法概述

  • 构造方法是一个特殊的方法
  • 是创建对象时候调用的方法
  • 方法的名字很特殊:必须和类名保持一致,大小写都要一样
  • 方法没有返回值
  • 方法也没有返回值类型
  • 构造方法无法在外部手动调用

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);
	}
}

二、对象的创建过程和构造方法的调用

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZOpKBNfr-1597023523685)(对象创建和构造方法调用.png)]

三、默认构造方法

  • 在我们创建类之后
    • 如果没有在类中书写任何构造方法,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;
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值