Home  >  Article  >  Java  >  An explanation of classes and objects in java

An explanation of classes and objects in java

王林
王林forward
2020-02-25 17:48:332310browse

An explanation of classes and objects in java

1. Definition

Class: A general name for a class of things, some descriptions of the current things, attribute descriptions and behavior descriptions

Object: Independent, unique, special individual

2. Definition format

class ClassName {
// 属性描述
// 行为描述
}

Requirements:

1. ClassName It is required to comply with the big camel case nomenclature, and the name must be understood

2. Attribute description is used to describe some of the current features, and variables can be used here. This variable has a specific name [Member Variable] Field

3. Behavior description is used to do something that the current class can do. Methods can be used here. This method has a specific name [Member Method] Method

(Recommended tutorial: java Quick Start)

3. Define a class in Java [Member Method 】

// 使用方法描述Person类的喜好
public void eat(String hobby) {
	System.out.println("打豆豆" + hobby);
}

Features:

(1) Currently, the static keyword is not used

(2) Method form, it must also have three major elements, return value and method Name and formal parameter list

4. Creating a class object

4.1 The form of creating a class object in Java

Person person = new person;

Format:

Class name class object = new constructor (required function)

4.2 Use class objects to call member variables in Java

// 【赋值】 Person类对象 person中对应的成员变量
person.name = "Rubick";
person.age = "66";
person.gender = "男";

// 【取值】 展示Person类对象 person中保存的成员变量数据
System.out.println("Name" + person.name);
System.out.println("Age" + person.name);
System.out.println("Gender" + person.gender);

4.3 Use class objects to call member methods in Java

//得到一个Scanner类对象sc
Scanner sc = new Scanner(System.in);
//使用方法:
sc.nextInt();
sc.nextFloat();
sc.nextLine.CharAt(0);

Format:

Class object.Method name (parameter);

. ==> can be understood as meaning "of".

For more programming-related content, please pay attention to the java video tutorial column on the php Chinese website!

The above is the detailed content of An explanation of classes and objects in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete