Home >Java >javaTutorial >How to use Java classes, objects and variables
A class is a collection of attributes (external characteristics) and behaviors (functions) of things
, 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.
To define a class, use the keyword class.
Format:
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 human1. We are looking for the described human2.Attributes: name, Age, gender, blood type3. Actions: eating, drinking, defecation, peeing, sleepingclass class name{
1. Know what class you want to write and find it from real life.
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("睡"); } }Object1. What is an objectObject 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 objectp.name = "tom";Object name.Attribute name = attribute value;
p.xuexing = "AB type"p.eat();Object name.Method name();
4. How to use the behavior in the object
Member variables and local variables1. What is a local variableA variable defined in a method, or a method declaration is a local variable. Example:
5. Examplepackage 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("睡"); } }
Variables
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; }
class Person { String name; int age; public void eat() { } }
1. Different definition locations
Local variables: defined in methods or methodsLocal variables: stored in methods on the stackMember 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!