当前位置:网站首页>Objects and object variables
Objects and object variables
2022-07-02 22:39:00 【LIU. cn】
Classes are templates or blueprints for constructing objects , The process of constructing objects from classes can be called instances of classes .
So how should we construct objects and understand them ?
First step : We create our own class ——Person class , In it, we define Person Some properties and methods of this class .
public class Person {
// attribute
public String name;
public int age;
// Parameterless constructors
public Person() {
}
// There are reference constructors ( All parameter constructor )
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Method
public void say(){
System.out.println(" Can talk ");
}
public void create(){
System.out.println(" Be able to invent ");
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}The second step : We construct classes into objects , That is, the process of instantiation 【 a key 】.
To use objects , First, we must construct the object , And specify the initial state of the object , Then apply the method to the object .
In our Java In language , You need to use a constructor ( Constructors ) To construct a new instance . Constructors ( Constructors ) It's a special method , This method is used to initialize objects .
instructions : The constructor name needs to be consistent with the class name . So when we need to construct a Person Class , Need to be in the constructor 1 prefix new Operators can construct New object :
new Person(); // In essence, the time-lapse parameter structure is called to initialize the state
new Person("Tom", 18); // In essence, what is called is a parameterized construct ( All the cords structure ) To initialize the State After we construct the object of this class , You can call all the properties and methods of this class !
public class Test {
public static void main(String[] args) {
new Person().create();
new Person("Tom", 18).say();
}
}
/*
test result :
Be able to invent
Can talk
*/
But we found that , The object we construct , Whether it is a nonparametric structure , Or a parametric structure , It's very troublesome every time , Not convenient enough . We hope that the constructed object can be used many times , Instead of using construct once , So at this point , We need to construct the object , Put it in a variable , that Object variables It was born !
Person commonPerson = new Person();
Person Tom = new Person("Tom", 18);In this case , We have Object variables commonPerson and Tom These two object variables , These two object variables will reference the newly constructed object Person, One is the method of nonparametric construction , One is the method of parametric construction .
public class Test {
public static void main(String[] args) {
Person commonPerson = new Person();
Person Tom = new Person("Tom", 18);
commonPerson.say();
Tom.create();
}
}
/*
test result :
Can talk
Be able to invent
*/
object And Object variables There is an important difference between , If we define object variables in another way
Person commonPerson;
Person Tom;To test :

By that definition , The variable at this time commonPerson and Tom In fact, there is no reference to any object at this time , And an error will be reported indicating that it is not initialized , So can't call Person Class , It also verifies the constructor again ( Constructors ) It is used to initialize objects and construct new instances , It also echoes why the constructor name should be consistent with the class name .
summary : Pass the above test and understand , We can see that the object variable does not actually contain an object , It simply refers to an object . stay Java in , The value of any object variable is a reference to an object stored in another place , So we can also know ,new The return value of the operator is also a reference .
边栏推荐
- The failure rate is as high as 80%. What should we do about digital transformation?
- Market Research - current situation and future development trend of marine clutch Market
- New feature of go1.18: introduce new netip Network Library
- Socket socket c/s end process
- Market Research - current market situation and future development trend of aircraft wireless intercom system
- Oracle-游标
- [shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)
- [staff] Sibelius 7.5.1 score software installation (software download | software installation)
- [001] [arm-cortex-m3/4] internal register
- [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)
猜你喜欢
![[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)](/img/f7/cb41d159e5c5ef3f4f1b9468d52ccc.jpg)
[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)

SimpleITK使用——4. 奇怪的問題

"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba

Developers share | HLS and skillfully use Axi_ Customize the master bus interface instructions and improve the data bandwidth - area exchange speed

scrcpy这款软件解决了和同事分享手机屏幕的问题| 社区征文

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

It's not easy to say I love you | use the minimum web API to upload files (swagger support) # yyds dry inventory #

Basic concepts of image and deep understanding of yuv/rgb

Landingsite eband B1 smoke test case

Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts
随机推荐
Market Research - current situation and future development trend of carob chocolate market
From personal heroes to versatile developers, the era of programmer 3.0 is coming
Market Research - current market situation and future development trend of marine wet exhaust hose
[autosar-dcm] - 4.3-how UDS $22 and $2e services read and write NVM data
【ODX Studio编辑PDX】-0.1-如何快速查看各Variant变体间的支持的诊断信息差异(服务,Sub-Function...)
New feature of go1.18: trylock, which has been tossed n times
Learn computer knowledge from scratch
ServiceMesh主要解决的三大痛點
Pointer - function pointer
[shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)
Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
Oracle-游标
Phpcms realizes the direct Alipay payment function of orders
Market Research - current situation and future development trend of anti-counterfeiting label market
What "real skills" should a million year old cloud native developer master? Alibaba, Tencent, meituan and byte decrypt together
《乔布斯传》英文原著重点词汇笔记(九)【 chapter seven】
Kubernetes resource object introduction and common commands (4)
Web侧防御指南
Notes on key vocabulary of the original English book biography of jobs (IX) [chapter seven]