当前位置:网站首页>对象与对象变量
对象与对象变量
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操作符的返回值也是一个引用。
边栏推荐
- 【C 题集】of Ⅴ
- U++ 学习笔记 ----松弛
- ArrayList analysis 2: pits in ITR, listiterator, and sublist
- Promise optimized callback hell
- LightGBM原理及天文数据中的应用
- An overview of the development of affective computing and understanding research
- Web侧防御指南
- Market Research - current market situation and future development trend of third-party data platform
- Technological Entrepreneurship: failure is not success, but reflection is
- [C question set] of V
猜你喜欢

SimpleITK使用——3. 常见操作
![[shutter] shutter application theme (themedata | dynamic modification theme)](/img/77/6b0082368943aee7108ac550141f28.gif)
[shutter] shutter application theme (themedata | dynamic modification theme)

How do I access the kubernetes API?

《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!

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

Landingsite eband B1 smoke test case

Etcd raft protocol

The source code of the daily book analyzes the design idea of Flink and solves the problems in Flink

Simpleitk use - 3 Common operations
随机推荐
Market Research - current market situation and future development trend of aircraft front wheel steering system
Promise optimized callback hell
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?
Market Research - current market situation and future development trend of intravenous injection (IV) bottles
Market Research - current situation and future development trend of marine clutch Market
【AUTOSAR-DCM】-4.3-UDS $22和$2E服务如何读取和写入NVM数据
Unity3d learning notes 4 - create mesh advanced interface
Struct, bit segment, enumeration, union
《乔布斯传》英文原著重点词汇笔记(九)【 chapter seven】
Pointer and string
C language, to achieve three chess games
SimpleITK使用——4. 奇怪的问题
Servicemesh mainly solves three pain points
Basic concepts of image and deep understanding of yuv/rgb
Regular expression (2)
Perceptron model and Application
Web侧防御指南
Bridge emqx cloud data to AWS IOT through the public network
【leetcode】1380. Lucky number in matrix