1. Create an Object
Class Name Object name = new class name ();
Telphone phone = new Telphone;
2. Working with objects
Reference object's Properties: Object Name . Properties
phone.screen = 5; //Assign a value of 5 to the screen property
Method of referencing object: Object name . Method Name ()
phone.sendmessage (); //Call SendMessage () method
Instance:
Create two of them . Java files
Telphone.java
//1. Definition of a class Public classTelphone {//2. What are the attributes (member variables) floatScreen//Screen Doublecpu//CPU intMem//Memory//3. How to do voidCall () {System.out.println ("Telphone is very helpful."); } voidSendMessage () {System.out.println ("Screen:" +screen + "\NCPU:" +cpu + "\nmem:" +cpu + "\ntelphone has the function of texting."); }}
Initailtelphone.java
Packageday8_09; Public classInitailtelphone { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub /** Telphone: The type of object, the class of the object is the type of the object * Phone: The name of the instantiated object * New Telphone (): Instantiate a Object*/Telphone Phone=NewTelphone (); Phone.screen= 4.5f;//to see the type of a member variable when assigning a value to a property (member variable)PHONE.CPU = 2.50d; Phone.mem= 6; Phone.call ();//method of calling callPhone.sendmessage ();//call the method of sending text messages }}
Operation Result:
Telphone has the function of calling ~ ~ Screen:4.5CPU:2.5mem:2.5Telphone has the function of texting ~ ~ ~
Creation and use of Java objects