Java core technology-objects and classes

Source: Internet
Author: User
Tags instance method

1 Overview of Object-oriented programming

Object-oriented programs are made up of objects, each containing a specific part of the functionality exposed to the user and a hidden implementation part.

Class 1.1

A class is a template or blueprint for constructing an object, and the process of constructing an object by a class is called creating an instance of the class.

Packaging:

Formally, data and behavior are combined in a package, and the user of the object hides the way the data is implemented.

The data in the object is called the instance domain , and the process of manipulating the data is called a method.

For each particular class instance (object), there is a specific set of instance field values that are the current state of the object.

The key to implementing encapsulation is that the methods in the class must never directly access the instance fields of other classes, and the program can only interact with the object data through the object's methods.

In Java, all classes originate from a "Mighty super-Class", which is an object.

The process of building another class by extending one class is called inheritance.

1.2 Objects

Three main features of an object:

The behavior of the object--what actions can be applied to the object, or what methods can be applied to the object.

The state of the object-how the object responds when those methods are applied.

Identity of the object-how to discern different objects that have the same behavior and state.

1.3 Recognition Classes

The simple rule of identifying a class is to look for a noun in the process of analyzing the problem, and the method corresponds to the verb.

relationship between the 1.4 classes

Among classes, the most common relationships are

* Dependent ("Uses-a"): One class method manipulates the object of another class, we say that one class depends on another class. Less dependence means less coupling.

* Aggregation ("Has-a"): Object of Class A contains an object of Class B.

* Inheritance ("is-a"): Used to represent special and general relationships.

2 Using predefined Class 2.1 objects and object variables

To use an object, you must first construct the object and specify its initial state . A constructor is a special method used to construct and initialize an object.

The name of the constructor should be the same as the class name.

Understand the difference between an object and an object variable.

It is important to realize that an object does not actually contain an object, but simply refers to an object. The value of any object variable is a reference to an object stored in another place.

Local variables are not automatically initialized to NULL, but must be initialized by calling new or by setting them to null.

2.2 Localdate Class

In Java, the date class is used to represent a point in time, and the Localdate class is used to represent calendar notation.

Instead of using constructors to construct the Localdate class, you should use a static factory method to call the constructor on your behalf: Localdate.now () Constructs a Date object that represents the time the object was constructed.

2.3 Change method and accessor method

Similar to the toUpperCase method of the string class, when this method is called, the original object is not changed, and a new object is returned that capitalizes all the characters in the original object.

In contrast, the methods that change the state of the original object are called the change method. (Gregoriancalendar.add)

Accessor method: A method that accesses only objects without modifying them.

3. User-defined class

Instance domain (private) + instance method

3.1 Constructors

* constructor has the same name as class

* Each class can have more than one constructor

* Constructors can have 0, one or more parameters

* constructor has no return value

* Constructors are always called with the new operation

3.2 Implicit parameters and display parameters

The general method has two parameters, the first of which is called an implicit parameter, and the class object that appears before the method name. The second display parameter is the value in parentheses after the method name.

In each method, the keyword this represents an implicit parameter.

3.3 Advantages of package

* A Private data field

* A public domain accessor method

* A public domain change method

Note Do not write accessor methods that return references to mutable objects, and if you need to return a reference to a Mutable object, you should first clone it.

Public Date Gethireday () {

Return (Date) Hireday.clone ();

}

3.4 Class-based access rights

A method can access private domain data for all objects of the owning class.

3.5 Final Instance Domain

The final modifier is mostly applied to the base type field, or to the domain of the immutable class.
For mutable classes, the final keyword only means that object references stored in Mutable class objects no longer point to other class objects, but this object can still be changed. (StringBuilder)

4 static domain and static method 4.1 static domain (class domain)

If the domain is defined as static, then there is only one such domain in each class. A static domain exists even if no class object exists. It belongs to a class and does not belong to any independent object.

4.2 Static constants

Math.PI, System.out

Public constants can only be accessed and cannot be modified: publicly final

The system class has a SetOut method to see that the System.out (final variable) can be set to a different stream because the SetOut method is a local method that is not implemented in the Java language and can bypass the Java language's access control mechanism.

4.3 Static methods

A static method is a method that cannot be manipulated against an object.

A static method cannot access an instance domain of a class, but can access a static domain in its own class.

There are two scenarios for using static methods:

* A method does not need to access the object state, its required parameters are provided by the display parameters (MATH.POW)

* A method only needs to access the static domain of the class

4.4 Factory Method

There is another common use of static methods: Static factory methods, which do not pass new , but rather use a static method to provide an instance of themselves externally.

The main difference between static factory construction objects and constructors constructs objects (more flexible):

1. Constructor name can be named

2. Create new objects without each call

3. Can return subclasses of the original return type

5 Method parameters

Arguments are passed to the method in two ways: by value invocation and by reference.

A method can modify the value of the variable that corresponds to the passed reference, and cannot modify the value of the variable that corresponds to the pass-through value invocation.

Java is called by value.

There are two types of method parameters:

* Basic Data type

* Object reference

Usage of method parameters in Java:

* A method cannot modify the parameters of a base data type

* A method can change the state of an object parameter

* A method cannot have an object argument referencing a new object

6 Object Constructs6.1 Overloading

Overload resolution by the compiler.

Method Signature: To fully describe a method, you need to indicate the method name and the type of the parameter. (The return type is not part of the method signature)

6.2 Default Domain initialization

if the domain is not explicitly given an initial value in the constructor, it is automatically assigned the default: The value is 0, the Boolean value is False, and the object reference is null.

This is the main difference between a domain and a local variable--The local variable has no default initialization and must be displayed to initialize the local variable.

6.3 Parameter-Free constructors

If at least one constructor is provided in a class, but no parameterless constructor is provided, it is considered illegal to construct the object without supplying a parameter.

The system provides a default constructor only if no constructors are provided in the class.

6.4 Calling another constructor

If the first statement of the constructor is shaped like this (), the constructor calls another constructor of the same class.

6.5 Initialization block

Methods for initializing data fields:

1. Setting values in the constructor

2. Assigning values in declarations

3. Initializing blocks

To invoke the constructor initialization procedure:

All data fields are initialized to default values (static domain initialization), data domain initialization statement, initialization block, and constructor

6.6 Object destructor and Finalize method

The Finalize method is called before the garbage collector clears the object.

If a resource needs to be closed immediately after it has been used, a close method can be applied to complete the cleanup operation.

7 packs

The main reason for using a package is to ensure the uniqueness of the class name.

Import of Class 7.1

A class can use all the classes in the owning package, as well as the public classes in other packages.

You can only import a package using an asterisk (*), and you cannot import all packages prefixed with Java using Import java.* or import java.*.*.

It is possible to add a specific package name to resolve two packages that have class reference problems.

If the two same-name classes in the two package are to be referenced, the full package name can be preceded by each class name.

7.2 Static Import

The import statement not only imports the class, but also adds the ability to import static and static fields.

7.3 Putting classes in a package

The compiler operates on files (files with file separators and extension. java). The Java Interpreter load class (with a. delimiter)

7.4 Packet Scope

The parts marked public can be used by any class, and the parts marked as private are used only by the class in which they are defined. If public or private is not specified, this section (class, method, variable) can be accessed by all methods in the same package.

Class 8 Design Tips

1. Be sure to keep your data private

2. Be sure to initialize the data

3. Do not use too many basic types in the class

4. Not all domains require a separate domain accessor and domain change

5. Decomposition of classes with excessive responsibilities

6. Class names and method names can reflect their responsibilities

7. The use of immutable classes is preferred (consider the specific situation)

Java core technology-objects and classes

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.