当前位置:网站首页>2021-05-07构造器
2021-05-07构造器
2022-06-23 09:55:00 【鹿其其鹿】
构造器讲解
类中的构造器也称为构造方法,是进行创建对象的时候必须要调用的,并且构造器有两个特点:
- 必须和类的名字相同
- 必须没有返回类型,也不能写void
一个类即使什么都不写,他也会存在一个构造方法
package oop.demo2;
//java编译之后会生成class文件
public class Person {
//一个类即使什么都不写也会存在一个方法
String name;
//构造器会实例化初始值
//使用new关键字,本质是在调用构造器
//
public Person(){
this.name = "qj";
}
//有参构造 一旦定义有参构造,无参构造就必须显示定义,否则new0实例化对象会报错
public Person(String name){
this.name = name;
}
}
package oop.demo2;
//一个项目应该只存在一个main方法
public class Application {
public static void main(String[]args){
//类:抽象的,实例化
//类实例化后会返回自己的对象
//student对象就是一个Student类的具体实例
Student xiaoming = new Student();
Student xiaohong = new Student();
xiaoming.name = "小明";
xiaohong.age = 18;
System.out.println(xiaoming.name);
System.out.println(xiaohong.age);
//new 实例化了一个对象
Person person = new Person();
System.out.println(person.name);
Person person1 = new Person("kuang");
System.out.println(person1.name);
}
}
构造器:
- 和类名相同
- 没有返回值
作用:
- new本质是在调用构造方法
- 初始化对象的值
注意点:
- 定义有参构造之后,如果想使用无参构造,要显示定义的一个无参构造
- Alt+Insert


边栏推荐
- The second Tencent light · public welfare innovation challenge was launched, and the three competition topics focused on the social value of sustainable development
- 分布式锁的三种实现方式
- RT-Thread 添加 msh 命令
- Difference between global shutter and roller shutter
- After the uncommitted transactions in the redo log buffer of MySQL InnoDB are persisted to the redo log, what happens if the transaction rollback occurs? How does the redo log handle this transaction
- After installing these five plug-ins, pycharm can be used for takeoff!
- 135137138445 unable to remotely connect to the database due to disabling
- oracle中遇到的bug
- RPC kernel details you must know (worth collecting)!!!
- High performance computing center RDMA implementation technology
猜你喜欢

必须知道的RPC内核细节(值得收藏)!!!
![[GYCTF2020]Blacklist](/img/a8/0f7231e5c498e6c89ff2e3bcb122f2.png)
[GYCTF2020]Blacklist

Comic | code review is driving me crazy!

Install the typescript environment and enable vscode to automatically monitor the compiled TS file as a JS file

Gorm advanced query

Bioinformatics | 基于相互作用神经网络的有效药物-靶标关联预测

Pet Feeder Based on stm32

Go string comparison

What is BFC? What problems can BFC solve

web--信息泄漏
随机推荐
Gstore weekly gstore source code analysis (IV): black and white list configuration analysis of security mechanism
Successful experience in postgraduate entrance examination for MTI master of English translation major of Beijing University of science and technology in 2023
Install using snap in opencloudos NET 6
利用华为云ECS服务器搭建安防视频监控平台
135137138445 unable to remotely connect to the database due to disabling
2021-04-12 链表第一次实现!!!
Go 单元测试
[GYCTF2020]Blacklist
Go 字符串比较
Bi SQL drop & alter
Unity技术手册 - 生命周期内速度限制(Limit Velocity Over Lifetime)子模块和速度继承(Inherit Velocity)子模块
我被提拔了,怎么和原来平级的同事相处?
RBtree
Mysql database introduction summary
ICLR 2022 | 视频中的动态卷积TAdaConv以及高效的卷积视频理解模型TAdaConvNeXt
AI system frontier dynamics issue 38: Google has abandoned tensorflow?; Four GPU parallel strategies for training large models; Father of llvm: modular design determines AI future
Distributed common interview questions
Use Base64 to show pictures
Go language JSON processing
Navagation navigation