JAVA Class and Object basics

Source: Internet
Author: User
Tags abstract definition modifier

/* The code used in the article is only a part of the source can be contacted by email me [email protected]*/

Like C + +, Java is also an object-oriented language whose foundation and core are classes and objects. And the object-oriented thought is the source and the display life, naturally in the study contact real life will understand deeper.

1. Objects

First, what is an object?

In real life, as long as a specific object or a specific thing is an object. Any object you see, any object you think of, such as: a computer, a person

Second, you know what an object is and then describe it. In what ways would you describe the object? What are some of the different things?

A computer
Features: size, material, shape, color, CPU, graphics card, memory, HDD, Brand 、...
Functions: Display, heat dissipation, calculation, input, power on, shutdown 、...

A certain person
Characteristics: Height, weight, appearance, age, gender, name, domicile, hobby, specialty 、...
Behavior: Eating, sleeping, learning, programming, working, playing, walking, talking 、...

2. Composition of Classes

How do you classify objects in real life?
The classification is based on similar characteristics of objects and similar behaviors (functions, uses); The classes in life are abstract.

What is the class in the program?
A class in a program is an abstract definition based on the characteristics and behavior (function/purpose) of a real-life object, according to the fixed format of the class in the program.
The class in the program is made up of two parts: 1. Property 2. Methods
Attributes are defined according to the characteristics of the object;
There are two methods of classes in Java: 1. Common Method 2. Construction method
The normal method is defined according to the object's behavior (function, purpose).
3. Basic structure of the class

public class class Name {
Defining properties
Access modifier data Type property name;

To define the format of a common method
Access modifier return Value data type method name (data type parameter name,...) {
}
}
Classes are just templates, basic units in Java, so you can't call properties and methods using classes, and calling properties and methods takes place in the main function.

  

1  Public classStudent {2         //Properties3     PrivateString name;4     5     Private intScro;6 7     //Construction Method8      PublicStudent ()9     {Ten         //This ("Unknown Name", "Java"); OneScro=0; A     } -     //Common Methods -      Public voidSetName (String N) the     { -Name=N; -     } -  +}
Student Class

4. Instantiating objects and calling property method formats
Keyword for instantiating an object: New
Format:
Class Name Object name = new class name ();
Class Name Object name = new Construction Method (parameter value,...);

To invoke the format of properties and methods:
Object name. Property name
The name of the object. Method Name (parameter value,...);

1  Public classStudent {2     Private intScro;3     4      PublicStudent ()5     {6Scro=0;7     }8     9     Ten      Public voidStudy () One     { Ascro++; -System.out.println (name+ "Learning"); -     } the      -      Public Static voidmain (String [] args) -     { -Student st1=NewStudent ();//instantiation of +St1.study ();//Method Invocation -     } +}
View Code

5. Construction method
Define the format of the construction method:
Access modifier constructor Method name (data type parameter name,...) {

}
Attention:

1. The class will provide a parameterless constructor by default, but if you customize the construction method in your class, the default constructor is overwritten, no longer available, and you can only use your custom construction method.
2. The construction method cannot define the return value data type;
3. The name of the construction method must be exactly the same as the class name.
The purpose of the construction method:
1. Instantiate the object;
2. You can set an initial value for a property.

1  Public classStudent {2     PrivateString name;3     4     Private intScro;5     6      PublicStudent ()7     {8Scro=0;9     }Ten  One      PublicStudent (String name) A     { -         //This (name, "Java"); -          This. name=name; theScro=0; -     } -  -}
Construction Method

6. Method overloading

As with function overloading in C + +, overloading of methods can also be done in Java.

The conditions for implementing method overloading are:
1. Method names are the same
2. The type, number and order of the parameters must be different
Different methods are called depending on the parameters of the method.

1  Public classStudent {2     3      PublicStudent ()4     {5         //This ("Unknown Name", "Java");6Scro=0;7     }8     9      PublicStudent (String name)Ten     { One         //This (name, "Java"); A          This. name=name; -Scro=0; -     } the  -}
View Code

7.this keywords

As with C + +, when you call a property in a method of a class, you can use this if the parameter name is the same as the property name. Differentiate

Another this (parameter value,...); The constructor method that invokes the corresponding parameter.

So who does this refer to?
Who is calling the method, the this in the method represents who. Assuming that a object name is called in the method, then the this in the method represents a, and if you change to a B object name in the calling method, the This in method represents B. Similar to the word "I" in life.

1  Public classStudent {2     PrivateString name;3     4     Private intScro;5     6      PublicStudent ()7     {8          This("Java");9Scro=0;Ten     } One      A      PublicStudent (String name) -     { -          This. name=name; theScro=0; -     } -  -}
This keyword

JAVA Class and Object basics

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.