Class and object 1, class definition
A class is a model that determines the characteristics (attributes) and behavior (methods) that an object will have.
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. 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, define the class in Java
1. Importance: All Java programs use class classes as the organizational unit.
2, the composition of the class: Properties and methods.
3. Define the steps of the class:
(1) Define the class name.
Public class Name {}
(2) write the properties of the class:
Public class The class name {{ attribute 1 ' type Property 1 Property 2 's Type Property 2 ...} ...}
(3) How to write
Public class class Name {{ attribute 1 ' Type property 1 Property 2 ' type property 2 ... ..... Method 1 Method 2}
4, a demo
Package Com.cnblogs; Public classTelphone {//properties (Member methods) floatScreen ; floatCPU; floatMem; //Method voidCall () {System. out. println ("Pager"); } voidSendMessage () {System. out. println ("Message"); }}
Third, use Java Object 1, the steps to use the object:
(1) Create an object:
New class name ();
(2) Objects used:
2, a demo
(1) write the above class and change the SendMessage method:
void SendMessage () { System.out.println ("screen:" + screen + "CPU:" + CPU + "MEM:" + mem + "Message");}
(2) Create a class
(3) Write code:
Packagecom.cnblogs; Public classTelphone {//properties (Member methods) DoubleScreen ; DoubleCPU; DoubleMem; //Method voidCall () {System.out.println ("Call"); } voidSendMessage () {System.out.println ("Screen:" + screen + "CPU:" + CPU + "MEM:" + mem + "Message"); } }Indicates that the object's properties are 0 after the object is initialized at the beginning.
Class member variables and local variables 1, member variables, and local variables:
(1) member variables:
Defined in a class to describe the properties of an object
(2) Local variables:
Defined in the method of the class, used to temporarily save the data.
2. Main differences
(1) Different scopes: the role of local variables is limited to methods only, and member variable scopes are available throughout the class.
(2) The initial value is different: The member variable assigns the initial value, but the local variable does not.
(3) Names of local variables and member variables: Local variables are preferred
Five, the construction method
1. Create a new object using the new+ construction method
2, the constructor method is defined in the Java class to initialize the object method, the construction method with the same name and no return value.
3, no parameter construction method use:
package com.cnblogs; public class Inittelphone { public static void main (string[] args) { /span>// TODO auto-generated method stub Te Lphone phone = new Telphone (); }}
Package com.cnblogs; Public class Telphone { public telphone () { System.out.println ("constructor Execution");} }
4, the use of a method of construction:
Packagecom.cnblogs; Public classTelphone {DoubleScreen ; DoubleCPU; DoubleMem; PublicTelphone () {System.out.println ("Constructor Execution"); } PublicTelphone (floatNscreen,floatNCPU,floatNmem) { screen=Nscreen; CPU=ncpu; Mem=Nmem; System.out.println ("Create a parameter constructor, you can assign an initial value"); }}
Package com.cnblogs; Public class Inittelphone { publicstaticvoid main (string[] args) { // TODO auto-generated Method stub Telphone phone = new telphone (5,2,2);} }
5, no construction method is specified, the system automatically adds no parameter construction method
6, if the construction method is specified, regardless of the parameters or no tragic, the system will not automatically generate a non-tragic construction method.
7. Constructors can also be overloaded
8, mainly for object initialization, to a reasonable value of the object.
VI. Static variable 1 in Java, definition:
Members in Java that are decorated statically are called static members or class members. It is owned by the entire class, not by an object, which is shared by all objects of the class. Static members can be accessed directly by using the class name or by using the object name. Of course, given the specificity of his role, it is more recommended to access it with a class name.
2. Description:
Static members belong to the entire class, and when the system first uses the class, it allocates memory space until the class is unloaded for resource recycling! ~~
Vii. static Method 1, description for use of static in Java
As with static variables, we can also use the static modification method, called a static method or a class method. In fact, the main method we've been writing about is static methods.
2 static members can be called directly in a static method, but not non-static members directly. if you want to invoke a non-static variable in a static method, you can access the non-static variable by creating the object of the class and then through the object.
3, in the ordinary member method, you can directly access the same kind of non-static variables and static variables
4, static methods can not directly invoke non-static methods, you need to access the non-static method through the object
Eight,Static initialization block used in Java
1, in Java can be initialized block data assignment
2. In a class declaration, you can include multiple initialization blocks, which are executed sequentially when you create an instance of the class.
If you initialize a block with the static adornment, it is called a static initialization block.
It is important to note that static initialization blocks are only executed when the class is loaded and only once, while static initialization blocks can only assign values to static variables and cannot initialize normal member variables .
3, the program runtime static initialization block is executed first, and then execute the ordinary initialization block, the final execution of the construction method. Static initialization blocks are only executed once when the class is loaded.
Java Learning Notes (vii)--objects