Classes and Objects in Java

What are classes?

A collection of objects with the same properties and methods. It is an abstract data type, which is an abstraction of entities with the same characteristics.

What is an object?

Objectively existing entities are objects. Everything is an object. In daily life, common foods are objects: men, women, scenic spots, computers, sports cars and so on.

For example,
students are classified according to grades, which are classes; the specific students in each class are objects.
Items placed in the supermarket are classified by area; each item is a single object.

**A class is a template for an object, and an object is an instance of a class. Classes can only be used through objects, and classes should be generated first during development, and then objects should be generated. Classes cannot be used directly, objects can be used directly.

How to create and use an object
1. Create an object: class name object = new class name ();
School center = new School ();
2. Reference object members: use "." to perform the following operations:
reference class attributes: object name.attribute
Refer to the method of the class: object name. method name ()
center.name = "World Cup"; // assign a value to the name attribute.
center.showCenter(); //Call the showCenter() method.

example

Use the object class to operate.
insert image description here
The running result is:
insert image description here
in the process of learning, and will be supplemented later. . .

Guess you like

Origin blog.csdn.net/weixin_47139540/article/details/106244164