I. Classes
I. Definition of class
Objects with the same properties are called classes. It defines the common characteristics and functions of all the objects it contains, and an object is an instantiation of a class.
Three common members of a class: properties, methods, and constructors
II. Writing of classes
1. Definition of class name;
2. Writing class attributes (characteristics); attribute ( member variable ) = access modifier + data type + variable name
//Defining Humanity
public class Person{
public String name; //name (of a person or thing)(causality)
public int age; //(a person's) age(causality)
public String sex; //distinguishing between the sexes(causality)
}
3. Writing class methods (behavior). Method = access modifier + return value type + method name + method parameters + method body
public class Person{
public void study(){ //methodologies
("Good good study, day day up!")
}
}
III. Class declarations
Classes must be declared before they can be used before variables can be declared and objects can be created
The class declaration syntax is as follows:
[identifier] [modifier] class class-name {
// Attributes (characteristics) of the class - noun (only code related to business logic is defined)
// class method (behavior) - verb (only define code relevant to business logic)
}
IV. Examples
public class Person {
String name;
int age;
String sex;
//Define a learning method
public void study(){
("Good good study, day day up!");
}
//Write an empty constructor explicitly
public Person() {
("Person empty constructor (computing)");
}
//Write a constructor with parameters
public Person(String name, int age, String sex) {
= name; //in the name ofPersonattributename,= backnamein the name of构造器中的参数name
= age;
= sex;
}
//method overloading
public Person(String name, int age) {
= name;
= age;
}
public static void main(String[] args) {
Person p1 = new Person("two-tenths of a centavo (unit of currency)", 22, "Male");
( + " " + + " " + );
();
Person p2 = new Person("coconut", 22);
( + " " + );
();
}
}
II. Target audience
I. Definition of objects
Any transaction that exists in the real world can be called an object.
II. Creation and use of objects
There are four common ways to instantiate an object: creating an object using a class (new), cloae methods, deserialization, and reflection.
1. Format for creating objects:
Class name Object name = new class name([argument list]);
Person person = new Person;
Note: By using the new operator, a class can create multiple objects, each allocated a different memory space, and changing the variables of one object does not affect the variables of the others.
Every class has a default constructor method with no arguments. When the default constructor method is invoked to create an object via the new operator, the system automatically initializes the allocated memory space. For numeric variables, the initial value 0 is assigned, for boolean variables, the initial value false, and for reference variables, a special value null is assigned.
2. Assign values to the properties of an object:
Object name. Attribute Name
= "two-twenty guests".
= 22.
= "Male".
3. Invoke the methods of the object.
[ Return value type Name = ] Object name. Method name (parameter list)
Note: The contents of [ ] may be omitted.
();
III. Examples
public class Person {
String name;
int age;
String sex;
//Define a learning method
public void study(){
("Good good study, day day up!");
}
}
public class Test {
public static void main(String[] args) {
// Create the object
Person person = new Person(); // Create an object.
// Set the properties
= "two-two-clients".
= 22;
= "male".
// Do an extraction of the attribute
();
();
();
// Call the method
();
}
}
IV. Anonymous objects
1. Definitions
An anonymous object is an object that is instantiated without a name, meaning that when an object is created, only the statement to create it is made without assigning its address to a variable, the
Creates an anonymous object:
//Not assigning a value to a variable
new Person();
2. Adaptation scenarios
1. When an object's methods are called only once
new Person().study();
2. When used as an actual parameter or return value of a method
3. Characteristics
-
Anonymous objects, as objects, also have all the functionality of normal objects;
-
Each time an anonymous object is used, it is a new object that is newly created;
-
After the execution of the anonymous object, since there are no other references, it will be judged as garbage by Java's garbage collection mechanism and automatically recycled.
V. Destruction of objects
-
Object destruction is the process of releasing the memory space occupied by an object when the object has been used up.
-
Java object destruction is the use of garbage collection mechanism (Garbage Collection ------GC), automatic recycling of objects that are no longer accessible.
- GC can reclaim new objects, not system resources;
- GC recycles objects not immediately, but when they are idle;
- Call (), not immediately recycled programmer can not decide
-
java programmers don't need to be concerned with the destruction of Java objects, only the creation (or instantiation) of Java objects - the role of the GC
-
Java objects will not be allowed to be accessed again after they have been destroyed, which will result in a runtime error (or program crash).