Creation and use of Java objects

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.