1. Objects
object: Represents an entity that can be clearly identified in the real world. For example: A student, a table, etc. Each object has its own unique identity, state, and behavior.
-
- 1) The state of an object refers to those data fields that have their current values. Java uses variables to define data fields.
- 2) The behavior of an object is defined by the method. The method of invoking an object is to have an object complete an action.
2. Constructing objects using construction methods
The construction method is a special method with the following three features:
-
- 1)--the construction method must be the same as the name of the class.
- 2)--the construction method has no return value or even void.
- 3)--the construction method is called when an object is created using the new operator. Used to initialize the object.
NewNew class name (parameter);
-
- 4)--the construction method can be overloaded.
Typically, a class provides a constructor without parameters, that is, a parameterless constructor. If a constructor method is not defined in the class, the system provides a default method body that is empty, the default constructor method. (default constructor).
An example of a class:
Public classTV {intChannel = 1; intVolumelevel = 1; BooleanOn =false; //default no parameter constructor PublicTV () {}//Constructors with parameters PublicTV (intNewchannel,intNewvolumelevel,BooleanNewon) { This. Channel =Newchannel; This. Volumelevel =Newvolumelevel; This. on =Newon; } //Method Public voidturnOn () { on=true; } Public voidturnoff () { on=false; } Public voidSetchannel (intNewchannel) { if(On && newchannel >= 1 && newchannel <= 120) Channel=Newchannel; } Public voidSetVolume (intnewvolumlevel) { if(On && newvolumlevel >= 1 && newvolumlevel <= 7) Volumelevel=Newvolumlevel; } Public voidChannelup () {if(On && Channel < 120) Channel++; } Public voidChanneldown () {if(On && channel > 1) Channel--; } Public voidVolumeup () {if(On && Volumelevel < 7) Volumelevel++; } Public voidVolumedown () {if(On && volumelevel > 1) Volumelevel--; }}
Invoke instance:
Now the TV's channel is ten, and Volum level Is:3now the TV's channel is a, and Volum level is:4
3. Access object 3.1 by referencing a variable. Reference variables and reference types
An object is accessed by referencing a variable that contains a reference to the object and declares a reference variable using the following statement:
ClassName Objectrefvar; Class name object reference variable
Circle mycircle = new Circle ();//Create an object and assign its reference to the variable mycircle
ClassName Objectrefvar = new ClassName (); Class name object reference variable = new class name ();//Declare object reference variable, create object and assign reference to object to the statement of the variable
In Java, an array is considered an object. Arrays are created with the new operator. An array variable is actually a variable that contains an array reference.
3.2. Accessing the data and methods of the object
After an object is created, its data and methods can be accessed and invoked using the dot (.) operator . The operator. Also known as the object member operator:
Objectrefvar.datafield; // The data field of the Reference object Objectrefvar.method; // method of invoking an object (parameter)
Mycircle.radius//radius of reference mycircle
-
- 1) The difference between a reference variable and a basic type variable: the underlying type variable--the corresponding memory stores the value of the underlying type variable; The reference variable--the value stored in the corresponding memory is a reference and an address stored by the object.
- 2) When assigning a variable to another variable, the other variable is given the same value. For a basic data type, the value of the variable is assigned to another variable. For reference, a reference to a variable is assigned to another variable, and two variables point to the same object at the same time.
Garbage collection in Java: If the original two reference variable C1, C2 points to different objects, but later because of the reference assignment, C1 and C2 point to the object referred to C1, then the object referenced by C2 is useless, known as garbage (garbage). Garbage takes up memory space. The Java runtime detects the garbage and automatically reclaims the space it occupies, a process known as garbage collection (garbage collection).
--If you think you no longer need an object, you can display a null value for the reference to the object.
4. Static variables, constants, and methods
- 1) If you want to share data for all instances of a class, declare it as a static variable , also known as a class variable . Static variables store variables in a common memory space. Java supports static variables and static methods, and you can call static methods without creating an instance of the class.
- 2) to declare a static variable or define a static method, add static to the declaration of the variable or method.
- 3) Constants in a class are shared by all objects of the class. Therefore, the constants should be declared as final static. For example, the PI constant in the Math class: final static double pi = 3.14159265358979323846
- 4) Use the "class name. Method Name" method to invoke the static method, using the "class name. Static variable" methods to invoke the static variable.
- 5) If a variable or method does not depend on an instance object of a specific class, it should be designed as a static variable and a static method.
Circle class:
Packagedemo; Public classCircle {DoubleRadius//radius of the circle Static intnumberofobjects = 0;//number of instance objects createdCircle () {radius= 1.0; Numberofobjects++; } Circle (DoubleNewradius) {Radius=Newradius; Numberofobjects++; } Static intgetnumberofobjects () {returnnumberofobjects; } DoubleGetarea () {returnRADIUS * RADIUS *Math.PI; } }
Test class:
Packagedemo; Public classtestcircle { Public Static voidMain (string[] args) {//TODO auto-generated Method StubSystem.out.println ("Before creating an object:"); System.out.println ("The number of instances is:" +circle.numberofobjects); Circle C1=NewCircle ();//Create C1System.out.println ("After creating the C1, the radius of the C1 is:" + C1.radius + ", the area of C1 is:" +C1.getarea ()); System.out.println ("The number of instances is:" +c1.numberofobjects); Circle C2=NewCircle (2.5); System.out.println ("After creating the C2, the C2 radius is:" + C2.radius + ", the area of C2 is:" +C2.getarea ()); System.out.println ("The number of instances is:" +c2.numberofobjects); }}
Output:
before you create an object: The number of instancesis: 0 after the C1 is created, the radius of C1 is:1.0, the area of C1 is: 3.141592653589793 the number of instancesis: 1 after you create C2, the radius of C2 is: 2.5, the area of C2 is: 19.634954084936208 the number of instances is:2
5. Visibility modifier
-
- 1), if the public modifier is used before classes, methods, and data fields, it means that they can be accessed by any other class.
- 2), if you do not use the visibility modifier, the default is that the class, method, and data fields can be accessed by any one of the classes in the same package. ———— is called package private or in-package access .
- 3), private modifier qualification methods and data fields can only be accessed in its own class.
- 4), if a class is not defined as public, it can only be accessed within the same package.
- 5), modifier private can only be applied to members of the class. Modifier public can act on a member of a class or class.
- 6), using modifiers on local variables public and private will cause compilation errors.
- 7), in most cases, the construction method is public. However, if you want to prevent users from creating instances of the class, you should use a private construction method. For example, all data fields and methods of the math class are static, and there is no need to create an instance of math. How to construct it: Private Math () {}
6. Data Domain Encapsulation
To prevent direct modification to the data field, use the private modifier to declare the data field private, which is called the encapsulation of the data domain .
To modify and access a private data field, define the get and set methods: Get (reader) returns the value of the data field, and set (the set) sets a new value for the data field.
signature of the Get method: Public returntype getpropertyname () If the return value is a Boolean type: Public Boolean ispropertyname () The signature of the Set method: Public void setpropertyname (dataType propertyvalue)
7. Array of objects
New // Create an array of 10 circle objects Initialize the Circlearray array: for int i = 0; I < 10; i++) { new Circle (); }
Objects in Java