Writing a Java program is basically defining the various classes of objects in the real world abstract it shows that the properties of the object and the behavior of the object in the program class is the property of objects represented by a variable to represent the behavior of the object by using a function to represent a variable in a class in a Java program is called a field. "), a function in a class is called a method
Defining fields and methods in a class
"Example" represents the definition of a class for "person"
Class person{
String name;
int age;
void SayHello () {
System.out.println ("hello! My name is "+name"
}
}
The class header is followed by the start class keyword defined by the keyword class flag class, followed by the name of the class name class for the user-defined class should conform to the Java requirements for naming identifiers.
The class body consists of two parts, the domain and the method. Both the domain and the method are members of the class. Multiple domains and methods can be defined in a class. A class can be represented by a class diagram in a uml diagram.
Person
+name:string
+age:int
+birthdate:date
+getinfo (): string--method
1. Fields
A field is a property of an object, a representation of a state, also known as a domain variable member variable field variable
Type Name Field name
int Age
For field variables, if you do not assign an initial value, the system automatically assigns a default value (the numeric type is 0, and the Boolean type is false, the reference type is null.) String type is a reference type)
2. Methods
A method is a representation of the behavior or function of an object that flags the functions and actions of a class to encapsulate the data of a class and an object in Java, similar to a function or procedure in another language, a program fragment method that is used to accomplish some sort of operation consists of a method header and a method body.
4.1.2 Construction method and object creation
1. Construction method
In a program, it is often necessary to create objects that will invoke the constructor of the object to complete the initialization of the object while creating the object
My java--class field method