一、构造方法概述

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

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

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

三、默认构造方法

  • 在我们创建类之后

    • 如果没有在类中书写任何构造方法,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. express创建项目
  2. passing parameters by value is inefficient when the parameters represent large blocks of data
  3. App_Code 引起的 ambiguously 问题
  4. Java学习笔记之深入理解引用
  5. fedora之防火墙
  6. OC基础 NSDate
  7. 使用命令行的方式操作hdfs
  8. Java循环和条件
  9. Android Keystore 对称-非对称加密
  10. 【Linux基础】查看硬件信息-硬盘
  11. Springboot 1.简介 及第一个demo
  12. vue项目移植tinymce踩坑
  13. Lambda 表达式有何用处?如何使用?
  14. 1-学习tecplot360
  15. Using Custom Java code in ODI
  16. Android 使用MediaPlayer 播放 视频
  17. 2017易观OLAP算法大赛
  18. P2043 质因子分解
  19. MariaDB主从复制搭建
  20. 随机生成30道四则运算-NEW

热门文章

  1. Codeforces Global Round 11 C. The Hard Work of Paparazzi (DP)
  2. Atcoder ABC162 D - RGB Triplets
  3. springboot源码解析-管中窥豹系列之BeanDefinition(八)
  4. Dapr 已在塔架就位 将发射新一代微服务
  5. Linux-输出/输入重定向
  6. Mybatis-02 CRUD
  7. 使用 Jenkins 搭建 CI/CD All In One
  8. Linux &amp; change username &amp; computer name &amp; .bashrc
  9. alipay 小程序 &amp; online IDE &amp; demos
  10. Taro API