Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the classes and objects of JavaSE

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces what the classes and objects of JavaSE are, and it has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.

1. Basic concept

First of all, we need to figure out several concepts: what is object-oriented, what is a class, and what is an object? Or one by one.

1.1 object oriented

We often say that Java is an object-oriented language and C language is a process-oriented language, so what is object-oriented and what is the difference between object-oriented and process-oriented?

Object-oriented is an idea of solving problems, and its core is to rely on the interaction between objects to solve things.

As for the difference between object-oriented and process-oriented, suppose there is a math problem:

Process-oriented: you do the calculation by hand, you need to pay attention to what formula to apply, how to simplify, which combination of conditions can draw what conclusion, etc., there is a link in the middle that can not be wrong.

Object-oriented: if you give the problem to the computer and let the computer solve it, we only focus on how the two objects interact with each other.

From this point of view, object-oriented seems to be more advanced than process-oriented, but in fact, there is no difference between good and bad. They all have their own special application scenarios, such as when you take a math exam, you can only write the questions yourself ("▽").

1.2 classes and objects

Class: a description of an entity

Objects: instantiation of classes

Suppose you want to design something, the class is the drawing you draw, and the object is the physical object you create from the drawing; a class can produce multiple objects.

two。 Class definition and using 2.1 definition

You need to use one keyword when defining a class: class

The specific syntax is as follows:

Class ClassName {field; method;}

ClassName: class name

Field: member variable

Method: member method

We now define a class Book, in which the member variables include the book title, author and price. The member method is to print all the member variables.

The concepts later in the article will be explained around this class.

2.2 instantiation of classes

The keyword used to instantiate an object in the main method: new

The code is as follows:

Book book1 = new Book ()

You can then use "." To access member variables and member methods in the class, as follows:

We said before that all variables in Java need to be initialized, and the member variables in the class are no exception. If the member variables are not initialized, the default is the 0 value of the corresponding data type.

The integer variable is 0, the floating point number is 0, the Boolean type is false, and the reference type is null,char'\ u0000'

3.this reference 3.1 access member variables

We now add another method setBook to the class to initialize the member variables.

Public void setBook (String name,String author,int price) {name=name; author=author; price=price;}

Let's take another look at the results:

Why are all zero values?

The reason for this is that the parameter name of the method is equal to the name of the member variable, and the formal parameter is considered by the program to be all formal parameters because of the number of local variables. The name and other variables in the method are all formal parameters.

We only need to add this to the variable to the left of the equal sign to solve this problem.

This represents a reference to the current object (whoever calls this is the current object)

The setBook method called by book1 in the figure above, so book1 is the current object, and the variables such as this.name in the method are equivalent to telling the program that this is not the formal parameter of the method, but the member variable of the object book1.

3.2 access to member methods

This is easy to understand, which is to refer to other methods in the class in the method, but note that references cannot be looped.

Suppose the show method is referenced in setBook, then setBook can no longer be referenced in the show method, otherwise it will be out of order.

There is one more point to add:

It was mentioned above that a class can produce multiple objects, so multiple objects call the same method, how can the program tell which object is called?

It's still a this reference.

Every method in the class starts with an implicit this parameter. You can know which object called the method through this, that is, the setBook method actually has four parameters, as follows:

Public void setBook (Book this,String name,String author,int price) {this.name=name; this.author=author; this.price=price;}

In addition to accessing member methods, this can also call other constructors of this class, which are described in the constructor

Properties of 3.3this referenc

1. Can only be used in member methods

two。 Is the first hidden parameter of the member method

3. Only the current object can be referenced in member methods (using this in object Book1 can only reference Book1, not object Book2)

4. Is the type reference of the corresponding class, that is, the object call is the reference type of the object.

4. Construction method

The constructor is also a member method, but it is special because the initialization of the object is in addition to using "." in the main method. In addition to initialization, you can also use construction methods.

You must go through two steps when instantiating an object:

Allocate memory

Call the appropriate constructor

If the user does not write a constructor, the compiler will provide a constructor without parameters by default, and then call the constructor written by the user.

4.1 characteristics of construction methods

The method name should be the same as the class name

There is no return value, even if you write void

Can only be called once during the entire declaration cycle of the object

Can be overloaded

We write a construction method as follows:

Public Book (String name, String author, int price) {this.name = name; this.author = author; this.price = price;}

Then the initialization object is as follows:

4.2this is used in constructors

As mentioned above, the constructor can be overloaded, so let's write a constructor with no parameters.

Public Book () {}

This can call other constructors in the constructor. For example, I call the above constructor with three parameters in the constructor with no parameters.

Note:

This also can't form a ring.

This must be placed on the first line of the constructor

Thank you for reading this article carefully. I hope the article "what are the classes and objects of JavaSE" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report