What are Java objects? Introduction to Java objects (code examples)
This article brings you what is a Java object? The introduction of Java objects has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Example:
public class Book { /** * 书名 */ private String name; /** * 作者 */ private String author; /** * 获取书名 * @return */ public String getName() { return name; } /** * 设置书名 * @param name */ public void setName(String name) { this.name = name; } /** * 获取作者 * @return */ public String getAuthor() { return author; } /** * 设置作者 * @param author */ public void setAuthor(String author) { this.author = author; } }
What is an object
The definition of an object in "JAVA Programming Thoughts" is: the elements in the problem space And their representations in the program space are called "objects".
1. Problem space: the actual problem model to be solved;
2. Solution space: computer (machine model).
The representation of the actual problem in the computer (machine model) is called an object. In the above example: the computer uses a unique entity (new Book()) to represent the book. This entity is called an object, and the object is an instance of the class.
What is a class
The abstraction of an object with the same properties and behavior is a class, that is, a class is the blueprint of an object, and the relationship between the two is that an instance of the class is an object , the abstraction of objects is classes. In the above example: Book is the class.
Characteristics of objects
1. Behavior: Class methods, methods in the example: getName(), setName(String name), getAuthor(), setAuthor( String author).
2. Status: attributes of the class, attributes in the example: name, author,
3. Identity: the address of the class in memory, the address of new Book() in memory is the identification.
Object-oriented features
1. Encapsulation: Hide the properties of the object, but provide methods for users to interact with these properties. The key point is that users can only use methods To access the properties of an object, encapsulation requires a private property: name, a property access method: getName(), and a property changing method: setName(String name).
2. Inheritance: By inheriting an existing class, you can reuse the attributes and methods of this class. The inherited class is called a parent class, and the inherited class is called a subclass. Subclasses can create their own specific functions. The code, the parent class generally saves common methods and attributes. Inheritance is achieved through the extends keyword.
Inheritance example:
// 备注:class可以定义多个类,但只有一个public类,而且public修饰的类名称要和文件名称一致。 public class BookTest{ public static void main(String[] args) { System.out.println(new HistoryBook().getName()); } } class HistoryBook extends Book{ /** * 发生日期 */ private LocalDate happenDate; protected LocalDate getHappenDate() { return happenDate; } public void setHappenDate(LocalDate happenDate) { this.happenDate = happenDate; } }
The above HistoryBook is a subclass of Book. In the main method, you can get the book title directly through the HistoryBook object. This is an attribute that is not included in the HistoryBook class, but because of inheritance , it can call all public, protected, and default modified methods of the parent class.
3. Polymorphism: The phenomenon that a variable reference can point to multiple actual types is called polymorphism. The formation of polymorphism is based on inheritance (extends)/implements (implements). The important point is that the parent class reference points to the subclass variable, but the parent class reference cannot be assigned to the subclass variable.
Polymorphic example:
// 父类引用指向子类,形成多态 Book book = new HistoryBook(); // 报错,原因:不能把父类引用赋值给子类,因为不能确定父类的具体类型,父类可能是A extends Book,也可能是B extends Book,不能把A或B强制转成HistoryBook,因为它们两之间是没有任何联系的。 HistoryBook historyBook = (HistoryBook) new Book(); // 这里是正常运行的,因为book其实指向的是一个HistoryBook的对象,这里是可以强制转换的。 HistoryBook hb = (HistoryBook) book;
The above is the detailed content of What are Java objects? Introduction to Java objects (code examples). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Java object creation involves the following steps: Class loading: Loading the binary code of a class. Memory allocation: Allocate memory space for objects in heap memory. Instantiation: Create a new instance of an object in the allocated memory space. Initialization: Initialize the object's instance variables with default values. Constructor call: The appropriate constructor is called to initialize the remaining fields of the object.

MyBatis is an excellent persistence layer framework that simplifies the process of interacting with databases in Java applications and greatly improves development efficiency. The core idea of the MyBatis framework is to map SQL statements to Java objects, and implement SQL mapping through XML configuration files or annotations, so that we can easily perform database operations. In MyBatis, the process of mapping SQL to Java objects can be simply divided into three steps: configuring the SQL mapping file, defining Java objects and

Java objects are created by classes defined by ourselves. They are actually 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.

The Java object life cycle includes: object creation, initialization, reachability, invalidation, and recycling. Objects are created through the new keyword, and initialization is performed in the constructor. Reachability refers to access through reference variables, failure means that it is no longer reachable, and recycling means that the garbage collector releases the memory of the invalid object.

JPA (JavaPersistenceAPI) is an ORM specification introduced in JavaEE5.0. Its purpose is to simplify the mapping of objects and relational databases and help Java developers more easily persist Java objects into relational databases. JPA hides the mapping between Java objects and relational databases by abstracting the concept of data. Developers can focus on writing business code without paying attention to the details of data storage. In this article, we will introduce how to use

JVM internal data structures include object headers, class tables, and hash tables, which are used to efficiently manage Java objects. The object header stores metadata, the class table provides class information, and the hash table implements fast object lookup, which together ensure the high-performance operation of Java applications.

Java object is the core concept in Java programming language. It is a concrete entity obtained through class instantiation. It has properties and methods and can be regarded as a representation of things, concepts or abstract concepts in the real world. A Java object is an instantiation of a class. By using the constructor of the class, a specific instance of a person can be created; a Java object has attributes, which describe the characteristics of the object, represented by using member variables of the class; a Java object has methods, Used to change the state of an object or provide the functionality of an object; Java objects are encapsulated and so on.

Gson is a json library for Java created by Google that can be used to generate JSON. By using Gson, we can generate JSON and convert a bean/java object into a JSON object. We can call the toJson() method of the Gson class to convert Java objects into JSON objects. Syntax publicjava.lang.StringtoJson(java.lang.Objectsrc) example importcom.google.gson.Gson;publicclassConvertJavaObjectToJSONTest{&n
