Using objects in Java, in 2 steps:
1. Create an object;
2. Use the properties and methods of the object.
See the following sample code:
PackageCom.imooc;//1. Definition of a class Public classTelphone {//2. What are the attributes (member variables) floatScreen ; floatCPU; floatMem; //3. How to do voidCall () {System.out.println ("Telphone has the function of calling!" "); } voidSendMessage () {System.out.println ("Screen:" +screen+ "CPU:" +cpu+ "mem:" +mem+ "Telphone has the function of texting! "); }}
PackageCom.imooc; Public classInitialtelphone { Public Static voidMain (string[] args) {//TODO auto-generated Method StubTelphone phone =NewTelphone (); Phone.sendmessage (); //Assigning a value to an instance variablePHONE.CPU = 1.4f; Phone.mem= 2.0f; Phone.screen= 5.0f; //methods for calling objectsPhone.sendmessage (); }}
How to use objects in Java