当前位置:网站首页>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 .
边栏推荐
- Market Research - current situation and future development trend of marine clutch Market
- The book "new programmer 002" is officially on the market! From "new database era" to "software defined car"
- Radis:Linux上安装Redis(步骤)
- Bridge emqx cloud data to AWS IOT through the public network
- [shutter] shutter application theme (themedata | dynamic modification theme)
- [shutter] shutter gesture interaction (small ball following the movement of fingers)
- Task and privilege level protection
- 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
- php实现根据输入的年龄查询出生日期符合的数据
- 建立自己的网站(22)
猜你喜欢

Socket套接字C/S端流程

Oracle-游标

UE4 game architecture learning notes

C语言,实现三子棋小游戏

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

Official announcement! The golden decade of new programmers and developers was officially released

New feature of go1.18: introduce new netip Network Library

Pointer and string

数学建模——图与网络模型及方法(一)

Oracle-PL/SQL编程
随机推荐
[leetcode] sword finger offer 11 Rotate the minimum number of the array
Pointer and string
sql service 截取字符串
100 important knowledge points that SQL must master: management transaction processing
Destroy in beforedestroy invalid value in localstorage
App page sharing password rails implementation
Oracle-PL/SQL编程
Pointer - function pointer
Bridge emqx cloud data to AWS IOT through the public network
服务器响应状态码
APP页面分享口令Rails实现
Les trois principaux points de douleur traités par servicemesh
New feature of go1.18: trylock, which has been tossed n times
[shutter] shutter gesture interaction (small ball following the movement of fingers)
Sql service intercepts string
【leetcode】1380. Lucky number in matrix
LightGBM原理及天文数据中的应用
UE4 UI自适应屏幕
100 important knowledge points that SQL must master: using cursors
Unity发布WebGL播放声音的一种方法