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 objects and classes in Zero basic Java

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what are objects and classes of zero basic Java". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are objects and classes of zero basic Java".

Process-oriented and object-oriented

Does the third sister want to ask, what is OOP?

The English full name of OOP is Object Oriented Programming. If you want to understand it, you must first understand object-oriented. If you want to understand object-oriented, you must first understand process-oriented, because at the beginning, there is no object-oriented programming language, all process-oriented.

Take a simpler example to distinguish between process-oriented and object-oriented.

One day, you want to eat a small bowl of soup, what should I do? There are two options:

1) buy your own ingredients, tofu skin, meat, garlic moss, etc., and do it yourself.

2) when you go to the restaurant, just shout to the boss, "have a small bowl of soup."

The first is process-oriented, and the second is object-oriented.

What are the disadvantages of process-oriented? If you buy a small bowl of soup ingredients, and then want to eat Kung Pao chicken, do you have to buy new ingredients?

What are the advantages of object-oriented? If you don't want to eat a small bowl of soup, all you have to do is say to your boss, "if my small bowl of soup hasn't been made, replace it with Kung Pao chicken."

Process-oriented is process-oriented, step by step, the previous step is done, and then do the next step.

Object-oriented is modular, I do mine, you do yours, I need you to do, I will let you know. I don't need to know exactly how you do it. It only depends on hard work rather than hard work.

However, if we go after it to the end, the bottom of the object-oriented is actually process-oriented, but the process-oriented is abstracted and encapsulated into classes, which is convenient for us to call.

02. Class

The object can be anything that can be seen in real life, such as a maverick pig, or any imaginary virtual object, such as Sun WuKong, who can change 72.

Java defines these objects by class, and the state of these objects is defined by fields, such as whether the color of a pig is solid or colorful, and the behavior of these objects is defined by methods, such as pigs can eat and sleep.

Come on, define a simple class to show you.

/ * Wechat search for "Silent King II" and reply to Java * * @ author Silent King II * @ date 2020-11-19 * / public class Person {private String name; private int age; private int sex; private void eat () {} private void sleep () {} private void dadoudou () {}}

A class can contain:

Field (Filed)

Method (Method)

Construction method (Constructor)

In the Person class, there are three fields, name, age, and sex, which are also called member variables-- inside the class but outside the method, and those inside the method are called temporary variables.

Member variables, sometimes called instance variables, do not take up memory space at compile time and acquire memory at run time, that is, fields get memory only after the object is instantiated (new Person ()), which is why it is called an "instance" variable.

Three methods, eat (), sleep (), and dadoudou (), indicate what the object Person can do, that is, eat and sleep and beat beans.

Does the third sister want to ask, "Why is there no method of construction?"

It's true that you don't see it in the source file (.java) of the Person class, but you can see it in the decompiled bytecode file (.class).

/ Source code recreated from a .class file by IntelliJ IDEA / / (powered by Fernflower decompiler) / / package com.itwanger.twentythree; public class Person {private String name; private int age; private int sex; public Person () {} private void eat () {} private void sleep () {} private void dadoudou () {}}

Public Person () {} is the default constructor, which can be defaulted because it is an empty constructor (there is no content in the method body). Java smart is smart here, some very rigid code does not need to be added by developers, it will do it secretly.

03. New an object

The new keyword is required when creating a Java object.

Person person = new Person ()

This line of code creates a Person object through the Person class. All objects allocate space in heap memory when they are created.

When creating an object, you need a main () method as an entry, and the main () method can be in the current class or in another class.

The first: the main () method is placed directly in the Person class.

Public class Person {private String name; private int age; private int sex; private void eat () {} private void sleep () {} private void dadoudou () {} public static void main (String [] args) {Person person = new Person (); System.out.println (person.name); System.out.println (person.age); System.out.println (person.sex);}}

The output is as follows:

Null 0 0

The second: the main () method is not in the Person class, but in another class.

In actual development, instead of creating an object directly in the current class and using it, we usually put it in a class that uses the object, such as the PersonTest class in the figure above.

You can put the PersonTest class and the Person class in two files, or you can put them in a file (named PersonTest.java), as shown below.

/ * * @ author Wechat searches for "Silent King II" and replies with the keywords PDF * / public class PersonTest {public static void main (String [] args) {Person person = new Person ();}} class Person {private String name; private int age; private int sex; private void eat () {} private void sleep () {} private void dadoudou () {}}

04. Initialize the object

In the previous example, the program output is as follows:

Null 0 0

Why is there such an output? Because the Person object is not initialized, the default value of String, the default value of null,int, is output.

So how do you initialize the Person object (assign values to fields)?

The first: through the reference variable of the object.

Public class Person {private String name; private int age; private int sex; public static void main (String [] args) {Person person = new Person (); person.name = "Silent King II"; person.age = 18; person.sex = 1; System.out.println (person.name); System.out.println (person.age) System.out.println (person.sex);}}

Person is called the reference variable of the object Person, as shown in the following figure:

Through the reference variable of the object, the field can be initialized directly (person.name = "Silence King II"), so the output of the above code is as follows:

Silent King II 18 1

The second is initialization through the method.

/ * * @ author Silent Wang er, an interesting programmer * / public class Person {private String name; private int age; private int sex; public void initialize (String n, int a, int s) {name = n; age = a; sex = s;} public static void main (String [] args) {Person person = new Person () Person.initialize; System.out.println (person.name); System.out.println (person.age); System.out.println (person.sex);}}

Add the method initialize () to the Person class, and then pass parameters for initialization after the new object is created (person.initialize (Silent King II, 18,1).

The third is initialization through the construction method.

/ * * @ author Silent Wang er, an interesting programmer * / public class Person {private String name; private int age; private int sex; public Person (String name, int age, int sex) {this.name = name; this.age = age; this.sex = sex } public static void main (String [] args) {Person person = new Person (Silent King II, 18,1); System.out.println (person.name); System.out.println (person.age); System.out.println (person.sex);}}

This is also the most standard way to pass parameters directly in new.

Add a little knowledge, anonymous objects. An anonymous object means that there is no reference variable, and it can only be used once at creation time.

New Person ()

Methods can be called directly through anonymous objects:

New Person (). Initialize (Silent King II, 18,1); Thank you for your reading. The above is the content of "what are objects and classes of Zero basic Java". After the study of this article, I believe you have a deeper understanding of what objects and classes of Zero basic Java are, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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