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!