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

How to use Java classes, objects, and variables

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, Xiaobian introduces in detail how to use Java classes, objects and variables, with detailed contents, clear steps and proper handling of details. I hope that this article "how to use Java classes, objects and variables" can help you solve your doubts.

Class 1. What is a class?

A class is a collection of attributes (external characteristics) and behaviors (functions) of things.

two。 Want to know what classes are in Java

We need to know what classes are in real life first, because Java comes from real life.

For example, human "class", why are we human? because we are similar in everything, we all have common external characteristics, such as ears, nose, mouth, name, age and so on. We all have similar and identical functions, for example, we all eat, drink, scatter and sleep, so if we are gathered together, we are called human beings.

3. How to write a class

The keyword class is used to define the class.

Format:

Class class name {

Properties of the class

The behavior of a class

}

Steps:

1. Know what kind of class you want to write and look for it in real life.

two。 What are the attributes in this kind of things: what are attributes? Is the external characteristics of things, member variables.

3. What are the behaviors in this kind of things: what is behavior? It is the function of things, which are generally verbs and member methods.

Example:

Demand:

Define a human.

1. We're looking for a description of the human.

two。 Attribute: name, age, sex, blood type

3. Action: eat, drink, pull, scatter, sleep

Property String name; int age; String sex; String xuexing of the class Liu {/ / class / / behavior of the class; public void eat () {System.out.println ("eat") } public void drink () {System.out.println ("drink") } public void la () {System.out.println ("pull") } public void sa () {System.out.println ("scatter") } public void sleep () {System.out.println ("sleep");}} object 1. What is the object?

An object is the concrete embodiment of a class.

two。 Create the format of the object

Class name object name = new class name ()

Liu p = new Liu ()

3. How to use properties in an object

Object name. Attribute name = attribute value

P.name = "tom"

P.age = 18

P.sex = "woman"

P.xuexing = "AB"

4. How to use the behavior in an object

Object name. Method name ()

P.eat ()

P.drink ()

P.sleep ()

5. Instance package com;// test class: the entry of the main method public class Demoliu {/ / program execution is provided. The main method public static void main (String [] args) {/ / creates a villain Liu p1 = new Liu (); / / assigns the value p1.name = "tom" to this person's attribute P1.age = 18; p1.sex = "woman"; p1.xuexing = "AB"; System.out.println (p1.name + "..." + p1.age + "..." + p1.sex + "..." + p1.xuexing); / / call this person's behavior p1.eat () P1.drink (); p1.sleep (); / / create a villain Liu p2 = new Liu (); / / assign the attribute of this person p2.name = "jerry"; p2.age = 19; p2.sex = "man" P2.xingzuo = "Sagittarius"; System.out.println (p2.name + "..." + p2.age + "..." + p2.sex + "..." + p2.xuexing); / / call this person's behavior p2.eat (); p2.drink (); p2.sleep () }} / / description class: human class Liu {/ / attribute: external feature, member variable String name; / / name int age; / / age String sex; / / gender String xingzuo; / / constellation / / behavior: function, member method public void eat () {System.out.println ("eat") } public void drink () {System.out.println ("drink");} public void sleep () {System.out.println ("sleep");}} variable

Member variables and local variables

1. What is a local variable?

Variables defined in a method, or on a method declaration, are local variables.

Example:

Public static void main (String [] args) {int I = 1; {int j = 2;}} public static int getSum (int I, int j) {int sum = I + j; return sum;} 2. What is a member variable

Defined in the class, the variables outside the method are member variables.

Example:

Class Person {String name; int age; public void eat () {}} 3. The difference between member variables and local variables

1. The definition location is different

Local variable: defined in a method or on the declaration of a method

Member variables: defined outside the method in the class

two。 Memory location is different

Local variables: stored in methods in the stack

Member variables: stored in objects in the heap

3. Initial values are different

Local variable: there is no default initial value. If you want to use it, you must assign a value before using it.

Member variables: there is a default initialization value, which can be used if it is not assigned

Default initial value null for variables of type String

The default initial value of a variable of type int is 0

Default initial value false for variables of type boolean

The default initial value of a variable of type double is 0.0

The default initial value of a variable of type char'\ u0000'

4. Life cycle is different.

Local variable: because it is stored in a method, it exists with the existence of the method and disappears with the disappearance of the method

Member variable: because it is stored in the object, it exists with the existence of the object and disappears with the disappearance of the object

5. The scope is different

Local variables: you can't use them without a method.

Member variables: can be used in all methods in this class

After reading this, the article "how to use Java classes, objects and variables" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it to understand it. If you want to know more about related articles, welcome to follow the industry information channel.

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