Ii. Java Object-oriented (3) _ Classes and objects

Source: Internet
Author: User
Tags modifier

2018-04-29

Classes and Objects

First, what is abstract

  In computer science, abstraction is a process in which the form of data and program definitions is similar to the connotation language of the representative, while the implementation details are hidden.

Abstraction: A concept or idea that is not tied to any particular specific instance.

    

Simply put: "Abstraction only reveals some of the relevant details of the object from the right angle." ”

In object-oriented programming theory, abstraction involves defining the language foundation of an abstract object, how it works, how to acquire and change state, and "communicate" with other objects in the system.

Abstraction works in many aspects of any programming language. Generate low-level language calls, such as design patterns, from creating subroutines to defining interfaces

  

  Reference Blog

Ii. the relationship between objects and classes

 1. Objects

Status (attributes)

Behavior (function)

--------------------------------------------------------------------------------------------------------------- ------------

anything that exists in reality can be viewed as an object that is categorized by its attributes

For example, animals and other species, animals are divided into cats and cats and other species of the cat Division
and Each object is a single individual, It's like there's no two of you in the world

This is also true in programs, where the inheritance of classes is like animals, animals, including cats .
    The object is also a separate entity created by different classes.

There are only inheritance relationships between classes (extends) and the above-mentioned creatures include animal animals including cats
And the interface is equivalent to having the ability a creature can have a lot of power, but he belongs to only one category.
so only the interface can be implements.

    You can also understand that extends came over is the parents taught implements is self-taught.

--------------------------------------------------------------------------------------------------------------- ---------------

  through for multiple objects of the same type analysis, we can put the object Abstract into categories.

2. Class

    An abstraction of an object with the same attribute (state) and behavior (function) is the class. A class is actually a data type.

    Thus, an object's abstraction is a class, and the materialization of the class is the object (the instantiation of the class is the object).

A class has a property that is the state of an object, using member variable definitions to encapsulate the data.

The function of a class is the behavior of the object, defined in the method.

The class is the template/type of the object. Creating an object is the use of a class as the basis for building the object. The description class is an object, and the description object is also a class. "A chicken or an egg first".

In object-oriented programming, classes have objects first, and if no class objects cannot be instantiated.

An object is an instance of a class that is a template for an object. (Create an instance from a template)

Iii. definition of Class

[modifier] class name {

Defining member variables (field/field)

Defining member Methods

}

    

--------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------

  Considerations for Defining classes:

1) If the class uses the public modifier, the current file name must be the same as the class name.

2) class names are best represented by nouns, because classes represent a class of things. The first letter of the class name is capitalized, and if multiple words are composed, it is named using hump notation, such as: employeesetting

3) In the object-oriented learning process, a class is provided specifically for the description object, which does not require the main method, and we define a test class to run the program.

Iv. basic operation of the object

A) creation and use of objects

  1. (depending on the class) The syntax for creating an object:

    Class name variable = new class name ();

2. Setting values for a field

    The object variable. Field name = value of this type;

3. Get field values

    The field type variable = object variable. field value;

4. Calling methods by object

  Object variable. Method name ([actual argument]);

  

B) Object Instantiation process

Allocates space for an object and initializes it by default for the property type.
PS: Eight basic data types, initialize by default, other data types default to NULL

  

Initial values for different types of member variables:

  

Object instantiation of Memory analysis:

  

c) The life cycle of the object

object's print operation: The ToString method of an object

Object comparison operation: "= =" is a value compared to the base data type, and is an in-memory address value for reference types

If objects have exactly the same data and want them to be equal, use the Equals method of object

The life cycle of an object:

Start: When creating an object with new, a space is allocated in memory

End: When the object in the heap is not referenced by any variable, the object is garbage, waiting to be recycled by the garbage collector (GC), and the object is destroyed when the garbage is recycled. (The purpose of recycling garbage is to free up more memory space.)

d) Anonymous objects

  An object that has no name and is not assigned to a variable after the object is created.

New Servant ();//The anonymous object simply creates a new memory space in the heap, but does not assign the memory address of the space to any variable

Because there is no name, an anonymous object can only be used once.

------------------------------------------------------------------------------------------------------------

General Declaration One object is this.
a = new A ();
so at this point A is an object of Class A, and the object name is a.
Take a look at the following example:
method (a a);
sorting method is a way that he needs to pass an object as a parameter, then there are 2 ways to do this:
Method 1:
A =new a ();
method (a);
Method 2:
Method (New A ());

in Method 2, new A () is an anonymous object, and he has no name. That makes sense.

--------------------------------------------------------------------------------------

Use of Anonymous objects:

1) when the method of an object is called only once, it can be done with an anonymous object, which simplifies writing.

If you make multiple calls to an object, you must give the object a name.

2) Anonymous object use Method two: You can pass an anonymous object as an actual parameter.

e) Student Payment case

  Student Class (Student) has two fields: name (name) and Isfee (whether to pay tuition), there is a way to pay tuition (fees).

Each student is a new object through the student class, now create an array to hold multiple student objects, and then determine whether the students in the array have already paid, and if not, call the method of tuition.

Code implementation:

1 classStudent2 {3String name;//name4     BooleanIsfee =false;//whether to pay the fee, default unpaid5 6     //define the way to pay tuition7     voidfees () {8Isfee =true;9     }Ten } One  A  - //Test Class -  Public classTestdemo the { -      Public Static voidMain (string[] args) { -          -Student S1 =NewStudent (); +s1.name = "Li Bai"; -S1.isfee =true; +  AStudent s2 =NewStudent (); atS2.name = "Luban"; -          -  -Student s3 =NewStudent (); -S3.name = "Zhang San"; -S3.isfee =false; in  -Student S4 =NewStudent (); tos4.name = "Li June"; +S4.isfee =true; -  the         //create an array to hold multiple student objects *student[] arr =Newstudent[] {S1,S2,S3,S4}; $         //iterating over algebraic group elements using For-eachPanax Notoginseng          for(Student ele:arr) { -             //System.out.println (Ele.name + "," +ele.isfee); the  +             //decide whether to pay the fee, or call the fees method if there is no fee A             if(!Ele.isfee) { the ele.fees (); +             } -         } $          for(Student ele:arr) { $System.out.println (Ele.name + "," +ele.isfee); -         } -  the     } -}

Output Result:

Memory Analysis:

Ii. Java Object-oriented (3) _ Classes and objects

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.