构造方法和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);
}
}
二、对象的创建过程和构造方法的调用

三、默认构造方法
- 在我们创建类之后
- 如果没有在类中书写任何构造方法,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;
}
}
构造方法和this的作用的更多相关文章
- JavaSE复习日记 : 实例化对象/构造方法和this关键字
/* * 实例化对象/对象的构造方法/this关键字 */ /* * 实例化对象 * * 就是实例化某一个类; * 从不同角度去理解的话就是: * 1. 从人的认知角度: * 就是具体化某个东西; * ...
- Java基础笔记(十四)——面向对象(续)【构造方法和this关键字】
一.构造函数的规则 1.构造方法是用来在对象实例化时初始化对象的成员变量的,以完成对象的初始化工作. 2.构造方法与类名相同且没有返回值(返回值也不能为void型).如:public 构造方法名( ) ...
- 二、OC的构造方法和descriprtion方法
二.构造方法和description方法 1.构造方法的定义 - (id)initWithAge:(int)newAge andNo:(int)newNo; 2.实现构造方法 - (id)initWi ...
- 3-自定义构造方法和description方法
http://www.cnblogs.com/mjios/archive/2013/04/19/3031412.html -自定义构造方法和description方法 1 默认的构造方法是什么?有什么 ...
- python类详细说明、常用内置方法和self的作用
一.类的定义 在Python中,一切皆对象,即便是类本身,也是一种type类型的特殊对象. class Person: def __init__(self, name, age): self.name ...
- java中构造方法和this,static关键字
构造方法: 构造方法是一种特殊的方法,用于创建该类的对象,对对象的数据进行初始化 格式如下: [修饰符] 类名(形参列表){ 方法体 } 特点 A:方法名和类名相同 B:没有返回值类型,连void都 ...
- 关于Java中的构造方法和set方法()给属性赋值
对于一个类中的成员变量(属性),属性如果都设置成了private类型,那么对外给属性设置了get和set方法 , 那么外部程序中给这些属性设置值,有两种方式. 第一种就是通过set()方法. 第二种就 ...
- Java基础构造方法和this关键字整理
构造方法 8.1构造方法介绍 构造方法的格式: 修饰符 构造方法名(参数列表) { } l 构造方法的体现: n 构造方法没有返回值类型.也不需要写返回值.因为它是为构建对象的,对象创建完,方法就 ...
- 详解es6 class语法糖中constructor方法和super的作用
大多数面向对象的编程语言都支持类和类继承的特性,而JS却不支持这些特性,只能通过其他方法定义并关联多个相似的对象,这种状态一直延续到了ES5.由于类似的库层出不穷,最终还是在ECMAScript 6中 ...
随机推荐
- Educational Codeforces Round 43
Educational Codeforces Round 43 A. Minimum Binary Number 显然可以把所有\(1\)合并成一个 注意没有\(1\)的情况 view code / ...
- hdu 6860 Fluctuation Limit 双向贪心
题意: 给你n个区间[li,ri],和一个整数k,你从每一个区间内选出来一个数,把从第i个区间内选出来数放在第i个位置,这样会构成一个长度为n的序列,你需要保证序列中任意两个相邻的数之差的绝对值要小于 ...
- Codeforces Round #645 (Div. 2) C. Celex Update
题目链接:C.Celex Update 题意: 给你如图所示的图形,问从(x1,y1)−>(x2,y2)路径上的不同的元素和的数量是多少. 题解: 从(1,1)到(3,3) 元素和的1−2−4− ...
- Panasonic Programming Contest (AtCoder Beginner Contest 186) E.Throne (数学,线性同余方程)
题意:有围着一圈的\(N\)把椅子,其中有一个是冠位,你在离冠位顺时针\(S\)把椅子的位置,你每次可以顺时针走\(K\)个椅子,问最少要走多少次才能登上冠位,或者走不到冠位. 题解:这题和洛谷那个青 ...
- c语言中qsort函数的使用、编程中的一些错误
qsort()函数: 功能:相当于c++sort,具有快排的功能,复杂度的话nlog(n)注:C中的qsort()采用的是快排算法,C++的sort()则是改进的快排算法.两者的时间复杂度都是nlog ...
- service配置文件
[Unit]Description="itcp Service"After=network.target cs_tcp.service [Service]Type=simpleGu ...
- 【ybt金牌导航1-2-6】【luogu P2467】地精部落
地精部落 题目链接:ybt金牌导航1-2-6 / luogu P2467 题目大意 有一个排列,要使得每个位置要么都比两边高,要么比两边低. 而且一定要以一高一低的方式排列. 两边的只用比旁边的那个高 ...
- CF1462-F. The Treasure of The Segments
题意: 给出n个线段组成的集合,第i个线段用 \(\{l_i, r_i\}\) 表示线段从坐标轴的点\(l_i\)横跨到点\(r_i\).现在你可以删除其中的一些线段,使得剩下的线段组成的集合中至少存 ...
- K8S(03)核心插件-Flannel网络插件
系列文章说明 本系列文章,可以基本算是 老男孩2019年王硕的K8S周末班课程 笔记,根据视频来看本笔记最好,否则有些地方会看不明白 需要视频可以联系我 K8S核心网络插件Flannel 目录 系列文 ...
- ARM汇编指令-STMFD/LDMFD
根据调用规则ATPCS,程序一般都使用FD(FullDescending)类型的数据栈(满栈),那么对立的就由空栈类型的数据栈.空栈是指SP操作完后指向的地址空间是未使用的,反之满栈就是SP指向的地址 ...