构造方法和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中 ...
随机推荐
- The 2019 ICPC Asia Shanghai Regional Contest H Tree Partition k、Color Graph
H题意: 给你一个n个节点n-1条无向边构成的树,每一个节点有一个权值wi,你需要把这棵树划分成k个子树,每一个子树的权值是这棵子树上所有节点权值之和. 你要输出这k棵子树的权值中那个最大的.你需要让 ...
- 2019牛客多校 Round7
Solved:5 Rank:296 E Find the median (线段树) 题意:最开始一个空的数组 4e5次操作 每次把Li,Ri中的每个数插入进来 问当前的中位数 题解:把这n个区间离散化 ...
- 【poj 1284】Primitive Roots(数论--欧拉函数 求原根个数){费马小定理、欧拉定理}
题意:求奇质数 P 的原根个数.若 x 是 P 的原根,那么 x^k (k=1~p-1) 模 P 为1~p-1,且互不相同. (3≤ P<65536) 解法:有费马小定理:若 p 是质数,x^( ...
- hoj2430 Counting the algorithms
My Tags (Edit) Source : mostleg Time limit : 1 sec Memory limit : 64 M Submitted : 725, Acce ...
- python代理池的构建4——mongdb数据库的增删改查
上一篇博客地址:python代理池的构建3--爬取代理ip 一.mongdb数据库的增删改查(Mongo_pool.py) #-*-coding:utf-8-*- ''' 实现代理池的数据库模块 ●作 ...
- 向 3D 世界迈出一小步
前言 OpenGL 的学习资料很多,一个是比较著名的 OpenGL 红宝书<OpenGL 编程指南>,可以在这里 http://opengl-redbook.com/ 下载该书配套的源代码 ...
- RuntimeError already started
Env: os: Ubuntu python3 pytorch vscode Desc 在上述环境中运行A3C多进程模型,使用命令行时没问题,使用vscode时出现 'RuntimeError: al ...
- 在kubernetes集群里集成Apollo配置中心(3)之交付Apollo-portal至Kubernetes集群
1.执行apollo-portal数据库脚本 apollo-portal数据库脚本链接:https://raw.githubusercontent.com/ctripcorp/apollo/1.5.1 ...
- dll的注册与反注册
regsvr32.exe是32位系统下使用的DLL注册和反注册工具,使用它必须通过命令行的方式使用,格式是:regsvr32 [/i[:cmdline]] DLL文件名命令可以在"开始→运行 ...
- codeforces 01B
B. Spreadsheets time limit per test 10 seconds memory limit per test 64 megabytes input standard inp ...