Java Basics-Object oriented

Source: Internet
Author: User

-------Android Training, Java training, look forward to communicating with you! ----------

Object oriented

Is an idea that simplifies complex things and turns the process-oriented executor into a conductor. The process is actually a function, and the object encapsulates the function and some content. Encapsulate the attributes and behaviors of things into objects, and then abstract objects into classes.

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

Class

is a description of the common features and functions of a set of things. It is the smallest unit that is designed according to the object-oriented technology, and also the most basic module of composing the project. The concept of class is abstract, similar to the drawing in architectural design, is the abstraction of the concrete content that the reality needs to represent. The class contains only the frame structure and does not contain the specific data.

The class is: The object is the description of things in life.

Object: It is this kind of thing, the real existence of the individual.

Later development: is actually looking for objects to use, no object, create a.

Find objects, build objects, use objects, maintain relationships with objects

Definition of the class:

[modifier] class name {

1-n a tectonic method;

0-n a field;

0-n a method

}

The access modifiers for the class have public and final

How to construct a class

Constructor method: Used to construct an instance of the class (each class has a default constructor method, and if a new constructor method is created, the default disappears.) Multiple construction methods appear in reload mode)

Field: The data contained in a class or object, a description of the state of the class;

Method: The characteristics or behavior of a class or object.

Differences between the construction method and the general method

    1. The constructor method is the same as the class name, and when the object is created, the corresponding constructor is called to initialize the object. And when the object is established, there is a call that is made and only once. And without a return statement, use new to call

    2. The general method can be arbitrarily named, after the object is established can be called multiple times, you can have a return statement. Object. Method () to invoke the

How to construct and construct code block differences

Constructing a code block is to initialize all objects, and the constructor initializes the corresponding object first.

The difference between a member variable and a local variable

member variables are defined outside of the class method, valid in this class, present in heap memory

A local variable exists in a method, a parameter, a statement, and, when defined, to curly braces, which exists in the stack memory.

static keyword: used to decorate members (member variables and member functions).

The modified members have the following characteristics:

1. Load with the load of the class.

2, precedence over the existence of the object.

3. Shared by all objects.

4, can be directly called by the class name.

Note: Static methods can only access static members, but static members can be accessed by non-static members, and this is not available in static methods.

Cause: When static methods and variables exist, the object does not exist, the non-static variable does not exist, and is definitely inaccessible.

The Main method (main) is static (you can use the class name to invoke the static Main method, which is normal!). But it will fall into a dead loop, causing memory to overflow and the JVM to stop automatically! public static void Main (string[] agrs) {}.

When do I use static?

1. Static variables

When the values of the member variables in the parsed object are the same, the member can be statically decorated.

As long as the data in the object is different, is the object's unique data, must be stored in the object, is non-static.

If the data is the same, the object does not need to be modified for use only, and does not need to be stored in the object, defined as static.

2. Static functions

Whether a function is statically decorated, it is a reference to whether the function requires access to the unique data in the object.

Simply put, from the source code, whether the function needs to access non-static member variables, if necessary, the function is non-static. If you do not need it, you can define the functionality as static. There is no point.

Anonymous objects

Understanding: An object without a name, creates an object, does not assign to a variable;

Features: Only one call to the method or field, can be passed as the actual parameter, only the storage area is opened in the heap,

Note: It can only be used once, and is destroyed after use;

New Person (); Represents an anonymous object, an object without a name

New person (). Age = 17;//is used once and then destroyed.

this keyword

Features: This represents the current object, the object that is currently invoking the instance member (who called the method, who is the current object).

The use of the This keyword: calls between methods; this. field;

The constructor calls each other, but at this point this ([parameter]) must be written in the first row of the construction method.

Note: Thiscannot be used in static-modified methods and in static-modified blocks of code.

Object-Oriented---encapsulation

Encapsulates implementation details that are used to hide objects. Public access is only available externally. The advantage is to isolate changes, improve reusability, safety and ease of use.

Form in the program: typically private, the member variables in a class can be accessed by providing a method (SETXXX,GETXXX) to the variable (XXX) (there is no getxx for a Boolean variable, only isxx).

Example code:

Class person{

private String name;

private int age;

public void Show () {

System.out.println (":" +name+age);

}

Public person (String name) {

Super ();

THIS.name = name;

}

Public person (String name, int age) {

Super ();

THIS.name = name;

This.age = age;

}

}

Object-Oriented---inheritance

First there are classes that reflect the general nature of things, and then on that basis reflect the classes of special things; that is to say: Inheritance is a kind of relationship from general to special;

Format: [modifier] class subclass extends Superclass

The superclass class is either a parent class or a base class, subclass is a subclass or a derived class, or an extension class (Java.lang.Object is the parent class of all classes).

Characteristics:

1, improve the reusability of the code.

2, let the class and the relationship between the class, with this inheritance relationship has a polymorphic characteristics.

3.only single inheritance is supported in the Java language .

Because multiple inheritance is easy to bring security risks (the parent class is more, the function is the same, there will be call uncertainty, overwrite a method, exactly overwrite the parent class?) )。

Attention:

1. The private member subclass of the parent class cannot inherit; The constructor method of the parent class cannot be inherited.

2, a class has and only a direct parent class; When a class does not show inheritance of another class, the default direct parent class is the object class, and the default direct parent class object is canceled when a class is shown inheriting another class.

3, in Java, a class can have only one direct parent class; Java.lang.Object is the parent class for all classes, and object is either a direct parent or an indirect parent class.

4. The subclass object must first call the constructor method in the parent class before invoking its own construction method before instantiating it.

9. Subclass Access Parent class and method overwrite

Attention:

1. Subclasses cannot directly access private members of the parent class (but subclasses can call non-private methods in the parent class to indirectly access the private members of the parent class).

2. Method overwrite causes: When a method in the parent class is not suitable for a subclass, the subclass has a method that is identical to the parent class.

Call the overridden parent class method: Use Super. Method name (argument);

The return value type of a subclass method is smaller or equal than the return value type of the parent class method

The exception that the subclass method declaration throws should be smaller or equal than the exception that the parent method declares;

Subclass methods should have greater or equal access to the parent class method;

Super keyword and calling parent class construction method

Feature: Represents the default reference for a parent class object

If the subclass is to invoke an instance method that the parent class is overridden, you can use super as the caller to invoke the overridden instance method of the parent class.

Calling the parent class method using Super

To call the constructor of a parent class using super

Call construction method

Call another overloaded constructor method in this class with this (parameter list)

Subclass constructor Method call Parent class construction method with super (parameter list)

Note: When a subclass calls the constructor method of the parent class: Super must be placed in the first sentence;

Java will call the parent class's parameterless constructor before executing the subclass's constructor, which is intended to initialize the members that inherit from the parent class.

When creating an object, the subclass calls the non-parametric constructor method of the parent class by default, and if the subclass constructor method displays the specified call to the parent class, the constructor method is invoked, and the parent class is called without a parameter constructor method.

Object-Oriented---polymorphism

Concept: polymorphism refers to the same entity with multiple forms simultaneously

The type at compile time is determined by the type used when declaring the variable, and the type of runtime is determined by the object that is actually assigned to the variable.

If the compile-time type differs from the run-time type, polymorphism occurs.

For example: Student extends Person:

Person p = new person ();

Student s = new Student ();

Person p = new Student ();//polymorphism, referential relationship: Parent class variable pointing to subclass instance object

To implement a polymorphic mechanism:

The reference variable of the parent class can point to the instance object of the subclass, and the method called by the program is dynamically bound at run time, that is, the method that refers to the real instance object that the variable points to, that is, the method of the object that is running in memory, not the method defined in the type of the reference variable.

The role of polymorphism:

Consider the different subclass objects as the parent class, you can block the differences between different sub-class objects, write generic code, and make general programming to adapt to the changing needs.

Modify the implementation of the method only, without modifying the declaration of the method

Inheritance is the precondition of polymorphism;

Classification:

Compile-time polymorphism: Method overloading

Run-time polymorphism: Method overwrite

This article is from the "Dot Drop" blog, please be sure to keep this source http://arctictern.blog.51cto.com/10120640/1659825

Java Basics-Object oriented

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.