Home > Java > javaTutorial > How to use Java classes, objects and variables

How to use Java classes, objects and variables

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-04-20 20:46:22
forward
1140 people have browsed it

Class

1. What is a class

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

2. Want To know what a class is in Java

  • , we must first know what a class is in real life, because Java originates from real life.

  • For example, let’s talk about human beings. Why are we humans? Because we are similar in everything. We all have common external characteristics, such as ears, nose, mouth, etc., and we all have names. , age, etc. We all have similar and identical functions, such as eating, drinking, sleeping, and so on, so if we are surrounded together, we are called human beings.

3. How to write a class

To define a class, use the keyword class.

Format:

class class name{
                                                                                                                                                                                     

1. Know what class you want to write and find it from real life.

2. What are the attributes in this type of thing: What are attributes? It is the external characteristics of things and member variables.

3. What are the behaviors in this type of thing: What is behavior? It is the function of something, usually a verb or a member method.

Example:

Requirements:

Define a human

1. We are looking for the described human

2.Attributes: name, Age, gender, blood type

3. Actions: eating, drinking, defecation, peeing, sleeping

				class Liu {
					//类的属性
					String name;
					int age;
					String sex;
					String xuexing;
					//类的行为;
					public void eat() {
						System.out.println("吃");
					}
					public void drink() {
						System.out.println("喝");
					}
					public void la() {
						System.out.println("拉");
					}
					public void sa() {
						System.out.println("撒");
					}
					public void sleep() {
						System.out.println("睡");
					}
				}
Copy after login

Object

1. What is an object

Object It is the specific embodiment of the class.

2. Format of creating objects

Class name object name = new class name();

Liu p = new Liu();

3. How to use the attributes in the object


Object name.Attribute name = attribute value;
p.name = "tom";

p.age = 18;

p.sex = "Woman";
p.xuexing = "AB type"



4. How to use the behavior in the object

Object name.Method name();
p.eat();

p.drink();

p.sleep();


5. Example

package com;
//测试类:里面会提供主方法
public class Demoliu {
	//程序执行的入口,主方法
	public static void main(String[] args) {
		//创建出来一个小人
		Liu p1 = new Liu();
		//给这个人的属性赋值
		p1.name = "tom";
		p1.age = 18;
		p1.sex = "女人";
		p1.xuexing = "AB型";
		System.out.println(p1.name + "..." + p1.age + "..." + p1.sex + "..." + p1.xuexing);
		//调用这个人的行为
		p1.eat();
		p1.drink();
		p1.sleep();
		//创建出来一个小人
		Liu p2 = new Liu();
		//给这个人的属性赋值
		p2.name = "jerry";
		p2.age = 19;
		p2.sex = "男人";
		p2.xingzuo = "射手座";
		System.out.println(p2.name + "..." + p2.age + "..." + p2.sex + "..." + p2.xuexing);
		//调用这个人的行为
		p2.eat();
		p2.drink();
		p2.sleep();
	}
}
//描述类:人类
class Liu {
	//属性:外在特征,成员变量
	String name; //姓名
	int age; //年龄
	String sex; //性别
	String xingzuo; //星座
	//行为:具备的功能,成员方法
	public void eat() {
		System.out.println("吃");
	}
	public void drink() {
		System.out.println("喝");
	}
	public void sleep() {
		System.out.println("睡");
	}
}
Copy after login

Variables

Member variables and local variables

1. What is a local variable

A variable defined in a method, or a method declaration is a local variable.

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;
	}
Copy after login

2. What is a member variable

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

Example:

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

1. Different definition locations

Local variables: defined in methods or methods

Member variables: defined outside the methods in the class

2. Different memory locations
Local variables: stored in methods on the stack

Member variables: stored in objects in the heap
3. Different initial values
Local variables: There is no default initial value. If you want to use it, you must first assign a value before using it
Member variables: There is a default initialization value. You can also use it if you don’t assign a value.
String type Variable default initial value NULL
int type variable default initial value 0
Boolean type variable default initial value FALSE
Double type variable default initial value 0.0
char type variables u0000'
4. Different life cycles
Local variables: Because they are stored in the method, they exist with the existence of the method and disappear with the disappearance of the method
Member variables: Because they are stored in the method in the object, so it exists with the existence of the object and disappears with the disappearance of the object
5. Different scopes
Local variables: they cannot be used after the method is used
Member variables: in this local variable

can be used in methods in the class

The above is the detailed content of How to use Java classes, objects and variables. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template