当前位置:网站首页>对象与对象变量
对象与对象变量
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操作符的返回值也是一个引用。
边栏推荐
- Leetcode circular linked list (fast and slow pointer) code line by line interpretation
- Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
- Market Research - current market situation and future development trend of aircraft wireless intercom system
- It's not easy to say I love you | use the minimum web API to upload files (swagger support) # yyds dry inventory #
- 一周生活
- Gee: (II) resampling the image
- 关于PHP-数据库的 数据读取,Trying to get property 'num_rows' of non-object?
- phpcms实现订单直接支付宝支付功能
- [shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)
- Market Research - current market situation and future development trend of high tibial osteotomy plate
猜你喜欢
Dynamic memory allocation (malloc calloc realloc free)
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
Pointer and string
How do I access the kubernetes API?
[staff] Sibelius 7.5.1 score software installation (software download | software installation)
Basic concepts of image and deep understanding of yuv/rgb
《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!
C language, to achieve three chess games
scrcpy这款软件解决了和同事分享手机屏幕的问题| 社区征文
Commodity information management system (C language document version)
随机推荐
Simpleitk use - 4 Strange question
Destroy in beforedestroy invalid value in localstorage
Attack and defense world PWN question: Echo
Evolution of messaging and streaming systems under the native tide of open source cloud
Learn computer knowledge from scratch
Commodity information management system (C language document version)
"Actbert" Baidu & Sydney University of technology proposed actbert to learn the global and local video text representation, which is effective in five video text tasks!
ServiceMesh主要解决的三大痛點
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
Phpcms realizes the direct Alipay payment function of orders
PHP微信抢红包的算法
《乔布斯传》英文原著重点词汇笔记(十)【 chapter eight】
20220702-程序员如何构建知识体系?
From "bronze" to "King", there are three secrets of enterprise digitalization
[shutter] shutter gesture interaction (small ball following the movement of fingers)
Oriental Aesthetics and software design
Kubernetes resource object introduction and common commands (4)
Market Research - current situation and future development trend of sickle cell therapy Market
540. Single element in ordered array
What is it that makes you tremble? Those without fans can learn