当前位置:网站首页>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 .
边栏推荐
- 【外刊】睡眠与减肥
- Landingsite eband B1 smoke test case
- scrcpy这款软件解决了和同事分享手机屏幕的问题| 社区征文
- LxC terminal login method
- Utilisation de simpletk - 4. Question étrange
- 【微服务|Sentinel】重写sentinel的接口BlockExceptionHandler
- UE4 UI自适应屏幕
- Share how to make professional hand drawn electronic maps
- How to center the positioned text horizontally and vertically
- Les trois principaux points de douleur traités par servicemesh
猜你喜欢
Official announcement! The golden decade of new programmers and developers was officially released
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
附加:【登录信息存储】与【登录状态校验】;(包括:总结了到目前为止,有关【登录信息存储】与【登录状态校验】的所有内容;)
The book "new programmer 002" is officially on the market! From "new database era" to "software defined car"
NC50965 Largest Rectangle in a Histogram
An overview of the development of affective computing and understanding research
It's not easy to say I love you | use the minimum web API to upload files (swagger support) # yyds dry inventory #
《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!
建立自己的网站(22)
540. Single element in ordered array
随机推荐
任务和特权级保护
Using emqx cloud to realize one machine one secret verification of IOT devices
Technological Entrepreneurship: failure is not success, but reflection is
Market Research - current situation and future development trend of carob chocolate market
kubernetes资源对象介绍及常用命令(四)
[QT] Q multithreaded development - Analysis of multithreaded application examples (Mandelbrot)
使用 EMQX Cloud 实现物联网设备一机一密验证
Pointer array parameter passing, pointer parameter passing
Notes on key vocabulary of the original English book biography of jobs (IX) [chapter seven]
开发者分享 | HLS, 巧用AXI_master总线接口指令的定制并提升数据带宽-面积换速度...
建立自己的网站(22)
20220702 how do programmers build knowledge systems?
Market Research - current situation and future development trend of herringbone gear Market
The book "new programmer 002" is officially on the market! From "new database era" to "software defined car"
[QT] QT multithreading development - reentrancy and thread safety
Les trois principaux points de douleur traités par servicemesh
SimpleITK使用——4. 奇怪的问题
《乔布斯传》英文原著重点词汇笔记(十一)【 chapter nine】
Unity3d learning notes 4 - create mesh advanced interface
LxC terminal login method