Java Learning Notes-3. Classes and objects

Source: Internet
Author: User

First, object-oriented programming

1. Concept:
OOA: Object-oriented analysis, abstract analysis of the real world

OOD: Object-oriented design

OOP: Object-oriented programs

2. Object-oriented features:

(1) Encapsulation: First, the whole state and behavior of the object are combined to form an indivisible whole, the object's private property can only be modified and read by the object's behavior.

The other is to hide the inner details of the object as much as possible, and the external connection can only be realized by the outside interface

(2) Inheritance: Special class objects have properties and behaviors of their general classes

(3) Polymorphism: refers to the same entity at the same time has a variety of forms, reflected in different objects can be based on the same message to produce their own different actions

Second, class

1. Implementation of Classes

[Access control character] [Modifier] class name

{//Class body start flag

[Description of member variables of class]//member variable definition and initialization

[Constructor method definition for class]//constructor method definition

[Member method definition for Class]//member method definition

}//class body End flag

2. Member variables

[Access control character] [modifier] Data type member variable name [= initial value];

Note: In the absence of an assignment, Java will give it a default initial value (except for the final variable)

3. Member Methods

[Access control character] [modifier] Return value type method name ([formal parameter table])

{

[Variable declaration]//variables used within the method, local variables

The body Code of the [program code]//method

[return [expression]]//Return statement

}

Third, the object

1. Creation and use of objects

Object Creation Format: Class Name Object name = new class name (parameter value list);

can also be subdivided into: Class name Object name = NULL; Declaring in stack memory

Object name = new class name (parameter value list); In the heap space Declaration

Object reference property and call method format: Object name. member variable name;

The name of the object. Member method name (actual parameter table);

2.Java memory mechanism: There are two kinds of memory in Java: Stack memory, heap memory

Basic types of variables and object references are allocated in stack memory, and when a variable is defined in a block of code, Java allocates memory space for the variable in the stack, and Java automatically frees the space allocated for that variable when the block of code for the variable is run.

Heap memory is used to hold the objects created by new, the memory allocated in the heap, managed by the automatic garbage collector of the Java Virtual machine

Essence: Create an object reference in stack memory and create the object itself in heap memory

Example: person xiaoming2 = null;

Xiaoming2 = xiaoming; The object itself is not copied, only the object reference is copied

3. The difference from C + +: Object O2 = O1;

In Java, just the reference to the object O1 is copied to O2, which points to the same object, that is, there is only one object

Copy the data of an object named O1 to O2 in C + +, i.e. there are two objects with the same values

Iv. methods

1. Method parameter passing: Java has only one parameter passing method, that is, passing by value

If the argument is a basic type of data, the parameter gets a copy of the basic type of data

If the argument is an object or an array, the parameter gets a copy of the object reference, that is, the argument and the parameter point to the same object

2. Overloading of methods: two or more methods in a class have the same method name, but the parameter list is different

The compiler determines which method to invoke based on the number and type of arguments in the number of argument lists

Note: Return value types cannot be used to differentiate methods

It is generally recommended that the same set of overloaded methods should have similar functions, which will enhance the readability of the program and facilitate the maintenance of the program.

3. Construction method: Called when the object is created

Feature: Has the same name as the class, no return value type is defined in the method declaration, no return value

If you do not define a constructor in a class, the system automatically generates a default constructor for that class.

Note: Construction methods can also be overloaded

4. Local variables: There is no default value for local variables in Java, scope: declaration begins to the end of the block that contains it

In Java, you cannot declare a variable with the same name in a nested two block, but you can declare a variable of the same name multiple times in different blocks that are not nested

Five, the keyword this

1.this: Current Object

When a local variable in a method has the same name as a member variable of a class, the member variable is hidden, and if you want to use a member variable in a member method, you must use the keyword this

2. If the first statement of a constructor has the form this (...), then this constructor calls the other constructors of this class

Cases:

Thisdemo (String s) {

System.out.println ("constructor s =" + s);

}

Thisdemo (int i, String s) {

This (s);

THIS.I = i;

}

Note: The constructor is called through this and must be the first row of the current constructor, and the constructor method can only call one constructor

Six, the key word static

1. The keyword static can be used to decorate the members and member methods of a class.

If you put the keyword static before a member variable declaration, the variable becomes a static variable, and it becomes a member variable of the class.

If you put the keyword static before a member method definition, the variable becomes a static method and a member method of the class.

2. Static variables (methods) can be accessed directly through the class name (not advocated through the corresponding object access), and the call does not need to instantiate first

3. Static method does not have a corresponding this reference

Non-static and non-static methods of the owning class cannot be accessed directly in a static method

Non-static methods can directly access the static variables and static methods of the owning class

Seven, the package

1.package declaration format: package name;

When there are child packages in the package, you can use "Package 1. Package 2 ... Package N "is specified

The package name can be understood as a directory name

Import format for 2.package: Imported package name. Class Name

Note: This class must be declared as public

Refer to all classes in a package: Available "*" For example: Import test.demo.*

Viii. access Control

1. Access control for class members

Control level In the same class In the same package Subclasses in different packages Any class
Private
Default
Protected
Public

2. Access Control for classes

Control level In the same package In different packages
Default
Public

Java Learning Notes-3. Classes and 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.