当前位置:网站首页>对象与对象变量
对象与对象变量
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操作符的返回值也是一个引用。
边栏推荐
- New feature of go1.18: trylock, which has been tossed n times
- Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
- [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)
- Market Research - current situation and future development trend of preclinical medical device testing service market
- Market Research - current situation and future development trend of environmental friendly fireworks Market
- 《乔布斯传》英文原著重点词汇笔记(十一)【 chapter nine】
- Market Research - current market situation and future development trend of aircraft audio control panel system
- Dynamic memory allocation (malloc calloc realloc free)
- How to write a good program when a big book speaks every day?
- About test cases
猜你喜欢

情感计算与理解研究发展概述

20220702-程序员如何构建知识体系?

Oriental Aesthetics and software design

SimpleITK使用——3. 常见操作

C language, to achieve three chess games

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

Pointer and string

建立自己的网站(22)

Ransack组合条件搜索实现
![[001] [arm-cortex-m3/4] internal register](/img/49/a0eceac1a67267216dd9b2566033a1.jpg)
[001] [arm-cortex-m3/4] internal register
随机推荐
Try to get property'num for PHP database data reading_ rows' of non-object?
What is it that makes you tremble? Those without fans can learn
Daily book - low code you must understand in the era of digital transformation
Leetcode theme [array] -169- most elements
Unity3d learning notes 4 - create mesh advanced interface
使用 EMQX Cloud 实现物联网设备一机一密验证
Market Research - current situation and future development trend of carob chocolate market
Service visibility and observability
: last child does not take effect
[sword finger offer] 56 - I. the number of numbers in the array
Basic concepts of image and deep understanding of yuv/rgb
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
20220702 how do programmers build knowledge systems?
Learn computer knowledge from scratch
Market Research - current situation and future development trend of sickle cell therapy Market
[shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)
Pointer - function pointer
【AUTOSAR-DCM】-4.3-UDS $22和$2E服务如何读取和写入NVM数据
ArrayList analysis 2: pits in ITR, listiterator, and sublist
Promise optimized callback hell