Java Object-oriented

Source: Internet
Author: User

Object-oriented three features: encapsulation, inheritance, polymorphism.

Later development: is actually looking for objects to use. If there is no object, an object is created.

Find objects, build objects, use objects, and maintain the relationships of objects.

Class and object relationships:
Real-life objects: Zhang San, John Doe
Want to describe: Extract the common content of the object. to the specific abstraction.
Description: The features of these objects are: name, age, gender, learning Java.

Mapped to Java, the description is class defined by class.
The object is the entity that corresponds to Java that is created in heap memory with new.

A class is a description of a real-life transaction.
Objects are: A class of transactions, a real entity.

The class contains the properties (variables) of the object and the functions (methods) that the object has, which are referred to as member variables and member methods in the class.

The difference between a member variable and a local variable:
1, scope of action:
Member variables act on the entire class;
A local variable acts in a function or in a statement (for loop statements)
2, storage location in memory:
The member variable exists in memory, because the object exists, and only exists in RAM;
Local variables exist in the stack memory.

How to use anonymous objects:
1, when the method of the object is called only once, you can use the anonymous object to complete, so that the write is more simplified;
If multiple members are called on an object, you must give the object a name.
2, an anonymous object can be passed as an actual parameter.

Class car{//defines the attribute (variable) String color= "Blue";//defines the car's color int num=4;//defines the number of tires for the car void run () {///define the car Run function    System.out.println ( Color+ ".." +num);}} Class Cardemo{public static void Main (string[] args) {//system.out.println ("Hello world!"); * Define a new object, create a variable C in the stack, create a new object in the heap with the color and Num properties. Assigning the object's storage address in the heap to the variable c,c is a class-type variable. Remember: The class type variable points to the object. Create new objects in Java by using the keyword new. The    action object is completed using the form "object. Object Member". For example: c.color= "Red". */car c=new Car (); c.color= "Yellow"; C.num=6;c.run (); Car c1=new car (); c1.color= "Red"; C1.run ();/* Use of anonymous objects, in the case of the heap: Create a new first anonymous object, assign its color property to "green", and then it is destroyed, garbage collected. Because this object is not referenced, it is garbage, that is, there is no variable pointing to the object. The new second object is treated the same as the first object. So it makes no sense for an anonymous object to invoke an object property, because the object is recycled after the property is processed. However, it is meaningful for an anonymous object to invoke one of the object's methods. The following is an instance of creating an anonymous object: */new car (). color= "Green"; new car (). Num=6;new car (). run ();/* The following is the procedure for an anonymous object to be passed as an actual parameter: */show (new Car ());} public static void Show (Car c) {c.num=3;c.color= "red"; C.run ();}}


Java Object-oriented

Related Article

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.