当前位置:网站首页>对象与对象变量
对象与对象变量
2022-07-02 22:01:00 【LIU.cn】
类是构造对象的模板或者蓝图,由类构造对象的过程可以称之为类的实例。
那么我们该如何构造对象并该如何理解呢?
第一步:我们创建一个自己的类——Person类,里面我们定义了Person这个类的一些属性和方法。
public class Person {
// 属性
public String name;
public int age;
// 无参构造器
public Person() {
}
// 有参构造器(全参构造器)
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// 方法
public void say(){
System.out.println("可以说话交流");
}
public void create(){
System.out.println("能够创造发明");
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
第二步:我们将类构造成对象,即实例化的过程【重点】。
要想使用对象,首先必须构造出对象,并指定对象的初始状态,然后对对象应用方法。
在我们的Java语言中,需要使用构造器(构造函数)来构造新的实例。构造器(构造函数)是一种特殊的方法,这种方法用来初始化对象。
须知:构造器的名字需要与类名相一致。因此当我们需要构造一个Person类的对象时,需要在构造器1前面加上new操作符就可以构造出新对象:
new Person(); // 实质上调用的时无参构造来初始化状态
new Person("Tom", 18); // 实质上调用的是有参构造(全参构造)来初始化状态
我们构造出来这个类的对象后,就可以调用这个类的所有属性和方法!
public class Test {
public static void main(String[] args) {
new Person().create();
new Person("Tom", 18).say();
}
}
/*
测试结果:
能够创造发明
可以说话交流
*/
但是我们发现,我们构造出来的对象,不论是无参构造,还或者是有参构造,每次这样都非常的麻烦,不够便捷。我们希望构造出来的对象可以做到多次使用,而不是使用一次构造一次,所以这个时候,我们就需要将我们所构造出来的对象,放到一个变量当中,那么对象变量就诞生了!
Person commonPerson = new Person();
Person Tom = new Person("Tom", 18);
这样的话,我们就有了对象变量commonPerson和Tom这两个对象变量,这两个对象变量就会去引用新构造的这一对象Person,一个走的是无参构造这一方法,一个走的是有参构造这一方法。
public class Test {
public static void main(String[] args) {
Person commonPerson = new Person();
Person Tom = new Person("Tom", 18);
commonPerson.say();
Tom.create();
}
}
/*
测试结果:
可以说话交流
能够创造发明
*/
对象与对象变量之间存在一个重要的区别,如果我们换一种方式来定义对象变量
Person commonPerson;
Person Tom;
进行测试:
这样定义的话,这时候的变量commonPerson和Tom实际上在这个时候是没有引用任何对象的,并且会报错显示未初始化,所以无法调用Person类的任何属性和方法,同样也再一次验证了构造器(构造函数)是用来初始化对象并构造新的实例,也与为什么构造器的名称要与类名一致相呼应。
总结:通过以上的测试以及理解,我们可以看到对象变量其实并没有实际包含一个对象,它仅仅只是去引用一个对象。在Java中,任何对象变量的值都是对存储在另一个地方的某一个对象的引用,所以我们也能够知道,new操作符的返回值也是一个引用。
边栏推荐
- 情感计算与理解研究发展概述
- 一周生活
- Phpcms realizes the direct Alipay payment function of orders
- Daily book - low code you must understand in the era of digital transformation
- Pointer array parameter passing, pointer parameter passing
- [shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)
- Ransack combined condition search implementation
- Learn computer knowledge from scratch
- ServiceMesh主要解决的三大痛點
- Unity3d learning notes 4 - create mesh advanced interface
猜你喜欢
Share how to make professional hand drawn electronic maps
开发者分享 | HLS, 巧用AXI_master总线接口指令的定制并提升数据带宽-面积换速度...
Dynamic memory allocation (malloc calloc realloc free)
C language, to achieve three chess games
20220702-程序员如何构建知识体系?
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
540. Single element in ordered array
Based on asp Net (used mobile phone sales management system) +asp Net+c # language +vs2010+ database can be used for course design and post design learning
《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!
[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)
随机推荐
Market Research - current situation and future development trend of cell-based seafood market
Official announcement! The golden decade of new programmers and developers was officially released
Unity3D学习笔记4——创建Mesh高级接口
Unity3d learning notes 4 - create mesh advanced interface
Pointer and string
ServiceMesh主要解决的三大痛点
任务和特权级保护
LightGBM原理及天文数据中的应用
[shutter] shutter opens a third-party application (url|launcher plug-in search and installation | url| launcher plug-in official example | open browser | open a third-party application)
kubernetes资源对象介绍及常用命令(四)
Unity3d learning notes 4 - create mesh advanced interface
sql service 截取字符串
Oriental Aesthetics and software design
《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!
How to write a good program when a big book speaks every day?
PHP wechat red packet grabbing algorithm
Ransack组合条件搜索实现
[Jianzhi offer] 57 And are two numbers of S
《乔布斯传》英文原著重点词汇笔记(十一)【 chapter nine】
Using emqx cloud to realize one machine one secret verification of IOT devices