I. Definition of Class 1 and class
Class is the encapsulation of data and related functions to form a special structure
2, the characteristics of the class
(1) class is the type of object
(2) A collection of a set of objects that have the same properties and methods.
(3) Within the same Java program, if more than one class is defined, it can only be declared as public by a single class, in which case the file name must be the same as the class name declared public
3. Properties of the object:
The various characteristics that an object has.
4, the method of the object:
The action that the object performs (what can be done).
5. Relationships between classes and objects
A class is an abstract concept, just a template. And the object is a concrete entity.
Second, the definition of Class 1, the general structure of the class
Iii. creation and use of objects 1. Creating objects
(1) Declaring a variable that points to an object created by the class
(2) Use the new operator to create an object and assign it to the previously created variable
For example, creating an object of cylinder class cylinder can be created using the following syntax:
Cylinder Volu; Declares a variable that points to an object Volu = new Cylinder (); Creating objects
can also be combined into one sentence
Cylinder Volu = new Cylinder (); Declare and create a new object, and let Volu point to the object
2. Object use
Object name. Object Member
This reference allows you to access the members of the object, and also to get and modify the values of member variables in the class
Class Cylinder { double radius; int height; Double pi = 3.14; void Area () { System.out.println ("area =" + pi * radius * radius); } void Volume () { System.out.println ("volume =" + pi * radius * radius * height);} } public class Demo1 { /** * @param args * /public static void main (string[] args) { Cylinder Volu ; Declares a variable that points to an object Volu = new Cylinder (); Create object Volu.radius = 2; Volu.height = 3; Volu.area (); Volu.volume (); }}
3. Parameter passing
public class Demo1 {/** * @param args */public static void main (string[] args) {Cylinder volu; Declares a variable that points to an object Volu = new Cylinder (); Create Object Volu.setcylinder (2.5,5,3.14); System.out.println ("R =" +volu.radius); System.out.println ("h =" +volu.height); System.out.println ("area =" +volu.area ()); System.out.println ("volume =" +volu.volume ());}} Class Cylinder {double radius;int height;double pi = 3.14;void setcylinder (double R, int h, double p) {pi = P;radius = R;he ight = h;} Double area () {return Pi*radius*radius;} Double volume () {return area () *height;}}
4. Anonymous objects
When an object is created, you can call the method of the object without defining the object's reference variable, and call the object's method directly, such an object called an anonymous object
New Cylinder (); // Create Object volu.setcylinder (2.5,5,3.14);
Rewritten as
New Cylinder (). Setcylinder (2.5,5,3.14);
When the method is finished, the object becomes garbage.
There are usually two cases of using anonymous objects:
(1) Only one method call is required for an object
(2) Passing an anonymous object as an argument to a method call
JAVA Classes and objects