Home > Common Problem > body text

How to understand java objects

百草
Release: 2023-06-21 11:13:01
Original
1948 people have browsed it

Java objects are created by classes defined by ourselves. In fact, they are the specific implementation of the class. Without classes, there are no objects. A class can create many objects. A class is a collection of entities with certain common characteristics. It is an abstract data type, an abstraction of entities with the same characteristics, and an abstraction of the attributes and behaviors of a type of "thing". An object is an entity in the real world. There is a one-to-one correspondence between objects and entities. This means that every entity in the real world is an object, so the object is a specific concept.

How to understand java objects

Operating system for this tutorial: Windows 10 system, Java 19.0.1 version, Dell G3 computer.

1. Object Concept

1. Objects are created by classes defined by ourselves.

2. The object is actually the specific implementation of the class.

For example: the design drawings drawn when building a building are classes, and the real habitable

buildings built according to the design drawings are objects.

Class--Abstract [Extract something like]--Template [Design]

Object--Implementation--Instance [Building]

3. None There are no objects in a class

4. A class can create multiple objects

5. A class is the template of an object, and the object is the true expression of the class

The role of objects

Call variables and methods in a class

A class is a collection of entities with certain common characteristics. It is an abstract data type, which is an abstraction of entities with the same characteristics. In object-oriented programming languages, a class is an abstraction of the properties and behavior of a type of "thing".

An object is an entity in the real world. There is a one-to-one correspondence between objects and entities. This means that every entity in the real world is an object, so the object is a specific concept.

2. How to create an object [new]

1. In the current class--new constructor---this

2. In other classes--new constructor

Format: new constructor ([parameter value]);

For example: in this class

packagecom.wangxing.test1;

publicclassduiXiang{
//创建公共实例成员变量/静态变量
publicStringname="zhangsan";
publicstaticintint1=1001;
publicduiXiang(){
newduiXiang();
//当对象在类中声明,要使用变量或方法时
System.out.println(newduiXiang().int1);
System.out.println(this.int1);
}
}
Copy after login

in other classes

packagecom.wangxing.test1;

publicclasstest{
publicstaticvoidmain(Stringargs[]){
newduiXiang();
}
}
Copy after login

3. Object access variables

1. Local variables cannot be accessed in other classes.

2. Instance variables can only be accessed by objects

3. Static variables can be accessed by class name and can be accessed by objects

4.Instance variables are among multiple objects of the same class Data cannot be shared between

Static variables can share data between multiple objects of the same class

For example:

packagecom.wangxing.test1;

publicclassfangWenBianLiang{
//创建实例成员和静态成员
publicStringname="lisi";
publicstaticintint1=1001;
publicfangWenBianLiang(){}
//创建一个无返回值和无参实例方法
publicvoidmethod1(){
//实例方法只能对象访问或者this指向这个类的对象
System.out.println("对象访问实例变量="+newfangWenBianLiang().name);
System.out.println("对象访问实例变量="+this.name);
//System.out.println("实例变量只能对象访问实例变量="+object.name);
//静态变量类名访问,可以对象访问
System.out.println("对象访问静态变量="+this.int1);
System.out.println("对象访问静态变量="+newfangWenBianLiang().int1);
System.out.println("对象访问静态变量="+fangWenBianLiang.int1);
}
}
Copy after login

Test class test object access

packagecom.wangxing.test1;

publicclasstest{
publicstaticvoidmain(Stringargs[]){
//创建fangWenBianLiang类的对象
fangWenBianLiangobj1=newfangWenBianLiang();
fangWenBianLiangobj2=newfangWenBianLiang();
//测试实例变量和静态变量在多个对象之间数据是否共享
//实例变量
System.out.println("实例变量obj1.name="+obj1.name);
obj1.name="zhangsan";
//多个对象中不能共享数据
System.out.println("实例变量obj1.name="+obj1.name);
System.out.println("实例变量obj2.name="+obj2.name);
//静态变量
System.out.println("静态变量obj1.int1="+obj1.int1);
obj1.int1=1002;
//多个对象中能共享数据
System.out.println("静态变量obj1.int1="+obj1.int1);
System.out.println("静态变量obj2.int1="+obj2.int1);
}
}
Copy after login

How to understand java objects

4. Object access methods

1.Constructor method access--new

2.Instance methods can only be accessed by objects

3. Static method class name access, object access

4. There is a parameter method, you need to pass parameters [number, type] [what you want, what to give]

5. There is return Value, the last sentence in the method body is return. The returned data value must match the return value type of the method.

When calling a method with a return value, you need to define a variable to receive the return value of the method [what to give, what to receive What】

For example:

packagecom.wangxing.test1;

publicclassfangWenFangfa{
publicfangWenFangfa(){}
//创建无返回值无参的实例方法
publicvoidmethod1(){
System.out.println("无返回值无参的实例方法");
//this.method1();
}
publicStringmethod3(intid,Stringname){
System.out.println("有返回值返回值有参的实例方法");
Stringinfo="id=="+id+"---"+"name=="+name;
returninfo;
}
//创建无返回值无参的静态方法
publicstaticvoidmethod2(){
System.out.println("无返回值无参的静态方法");
}
}
Copy after login

Test class

packagecom.wangxing.test1;

publicclasstestFangFa{
publicstaticvoidmain(Stringargs[]){
//创建fangWenFangfa类的对象
fangWenFangfafwff=newfangWenFangfa();
//实例方法只能对象访问,在同一类时可以使用this指向类对象访问实例方法
fwff.method1();
//fangWenFangfa.method1();
//静态变量,在同一类时可以使用this指向类对象访问静态方法
fwff.method2();
fangWenFangfa.method2();
//有参数方法,需要传递参数【个数,类型】【要什么,给什么】
//有返回值,方法体中的最后一句是return,返回的数据值要与方法的返回值类型匹配,
//调用有返回值的方法需要定义变量来接收方法的返回值【给什么,收什么】
Stringinfo1=fwff.method3(1001,"zhangsan");
System.out.println("对象访问有返回值参数的实例方法结果:"+info1);
}
}
Copy after login

How to understand java objects

The above is the detailed content of How to understand java objects. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!