JAVA Object-oriented

Source: Internet
Author: User

1; What is Object-oriented: 1; object-oriented and process-oriented is an idea

2; Process oriented: emphasis on functional behavior

3; object-oriented: Encapsulates functionality, emphasizing functional objects

2; object-oriented features: 1; encapsulation 2; inheritance 3; polymorphism

3. Object-oriented features: complex things can be simplified

Change programmer from performer to conductor

4. Class: The behavior and attributes of a general description of things in life

Java uses classes to describe things:

1; properties: member variables in the corresponding class

2; Behavior: Methods in the corresponding class

Class is a description of life

object is the description of things in life, the real individual

In Java with the new operator.

Example: Car car=new car ();

is actually the entity that was generated in the heap memory

Car is a class-type variable, and a class-type variable points to an object

Car.run ();

Car car2=new car ();

5; Information hiding: Private----proprietary: Available with decorated member variables and member methods, only valid in this class

6; Package: 1; By data encapsulation, declared as private

2; provide one or more public methods

7. Package Benefits: 1; Hides the implementation details of a class

2. Convenient control: Restricting unreasonable operation of attributes

3. Easy to modify: Increase code maintainability

8. Instance variables and local variables

Instance variable = = member variable

1;1, the member variable is defined in the class

2, the local variable is defined in the method, on the argument, in the statement

2. The member variable is valid in the class

The local variable takes effect only within the curly braces it belongs to, and is invalid if it is out.

3; The member variable exists in the work and disappears as the object disappears

Local variables exist in the stack, and are present as the running of the owning difference, ending and releasing

9; Constructor:

1: constructor does not return a value

2. The function name and class name of the constructor are the same

3; Initialize the data to the object

Note: When a class is defined, if a constructor is defined, there is no default constructor

10. Differences between constructors and general functions

1; format is different (function name, return type, return value)

2; The constructor is called when the object is created and is called only once;

The general function is called after the object is created, and can be called multiple times

Note: Create an object to do something in memory:

1. Load the Person.class file into memory first

2; Execute the main () method, open space in the stack, and then assign a variable person to the stack area of the Main method

3. Open an entity space in the heap, allocating memory address (new)

4. Attribute space allocation in the entity space and default initialization

5; Call the constructor of the entity to initialize the constructor function.

6; Call the in-memory address in the parameter of the method, and pass the value

11;this keywords

1;this: Represents the object, which is the reference to the object to which the function belongs

2; When is the same?

A: When defining a feature, use this to represent an object if the function is used internally to the object that called it

3; Call format: This,this object followed by. Member properties and methods are called

The This object is followed by () The constructor method in this class

Note: The This call function must be defined in the first row of the constructor

Because constructors are used for initialization, initialization actions are bound to execute, or compilation errors

Static keyword: is a modifier used to decorate members (member variables and member methods)

Characteristics:

1. To implement object sharing of the object's CPC data, this data can be modified statically

2; The modified member can be called by the class name. class name. Static members

3; static loads with the load of the class, and takes precedence over the existence of the object

Disadvantages:

1; Some data objects are unique data, can not be static modification, if the data is modified, then the data will become shared data, the description of the thing is a problem

2 static method, only access static members, cannot access non-static members

3. The This and Super keywords cannot be used in a static method

12. The difference between a static variable and a member variable

1; a static variable belongs to a class, also known as a class variable

2; The member variable belongs to an object, also known as an instance variable

Static variables exist in the method area

Member variables exist in the heap

Static variables exist as classes are loaded and disappear as classes disappear

Member variables exist as objects are created and disappear as objects are recycled

Static variables can be called by object and class name

Conclusion: Member variables can be called unique data for an object

Static variables can be called shared data for objects

Static code block:

static{

System.out.print ();

}

ToString () {

Return "student[id=" +id+ ", Name=" +name+ "]"

}

function: 1; The initialization of a class can be completed, static code blocks are executed as the class is loaded and executed only once (new multiple objects, executed only once)

2; If the main function is in the same class, the precedence main function executes

13; design mode: 1; Create pattern

2. Structural mode

3. Behavioral Mode

Singleton mode: Guaranteed object uniqueness of a class in memory

How to guarantee the uniqueness of an object?

1; Do not allow other programs to create this class object

2. Create a class object in this class

3. Provide methods to allow other programs to obtain this object

Steps:

1; Because the constructor initialization is required for the creation of the object, the other program cannot create the class object unless the constructor in this class is privatized

2; Create an object in this class

1;private single () {

}

private static single s=new single ();

public static single getinstance () {


}


2;private single () {

}

public static Single2 S=null;

private static Single2 getinstance () {

if (s==null) {

S=new Single2 ();

}

return s;

}

14; Inheritance:-----Animal extends Object

Benefits of Inheritance: Improved code reusability

Parent class Origin: In fact, by multiple classes to extract the common content of the continuous upward

The class that implements the inheritance is a subclass and is inherited by the parent class (base class)

Syntax format: Modifier class Subclass name extends parent class name

Extends is the inherited keyword, which is the meaning of the extension

Method rewrite: Three same small one big

Three-way: Method name, same parameter list, same return value

A small: The exception class thrown by a subclass method declaration should be smaller or identical than the exception class thrown by the declaration of the parent class method

Large: Subclass methods should have greater or identical access to the parent class method

15;super keyword: What are the characteristics of the members of a class after the subclass appears?

1; member variable: When a subclass appears with the same attribute, the object of the subclass type calls the property, and the value is the property value of the child class

System.out.println (this.birname+ "only runs on the ground");

System.out.println (super.birname+ "only runs on the ground ");

This: Represents an object reference of this class type

Super: Represents a memory space reference in the parent class to which the subclass belongs

If you want to invoke the properties of the parent class, you need super this keyword

2 constructor: When the constructor of a child class is found to run, the constructor of the parent class is run first

The first line in all the constructors of the subclass actually has a stealth statement super ();

3; member function: When the child class parent class appears the same way, the subclass object will run a method of the subclass, and the method of the parent class will be overwritten (overridden)
If you need to call the parent class method, you need to Super.bird ();

Classes in Java only allow single inheritance

Multiple inheritance: A inherits B,b inheritance C,c inheritance D

16; method overloading: 1; what is called overloading: defines a set of methods that have the same name, perform class operations, but use different methods,

2. Three main principles of overloading: the same method name

Different parameters: different numbers, different types, different order

Same scope

Tip: The method overload has no relation to the return value type

1; Member Method overloading

2; constructor overloading

17;final keywords

Final decorated member property, this property becomes constant, immutable, must be assigned the initial value

Final modification method, this method cannot override the

The final decorated class, which cannot be inherited, and all methods and properties in the class are final

18. Relationships between classes: Dependency (use-a) Inheritance (is-a) aggregation (HAS-A)

19 Abstract Class: Abstract class cannot be instantiated

Abstract method: Just declare, do not need to implement

Abstract Keywords: abstraction

If this method is an abstract method, then this class must be an abstract class

Abstract classes can contain ordinary members (variables and methods)

Subclasses must override the abstract method of the parent class, and if not overridden then this class is also an abstract class

Abstract classes have constructors, with and initialization

Abstract keyword and what cannot be shared: private final static

Abstract classes do not necessarily contain abstract methods

20;abstract Features:

1. Abstract methods can only be defined in abstract classes, abstract classes and abstract methods must be decorated by the abstract keyword (methods, classes, cannot modify variables)

2; Abstract methods define method declarations only, and do not define method practices (only method names, no method bodies)

3 abstract class cannot be instantiated (cannot create object)

4; subclass inherits abstract class, must override abstract method of parent class, if not rewrite, it is an abstract class, if not abstract class, then compile does not pass

5;abstract keyword cannot and final,private,static

Accessories: Ctrl+shift+r-----Find Files

Ctrl+t-----View class Structure

CTRL + click------to locate the file

CTRL + C-----Copy

Interface: A collection of definitions of abstract methods and constant values

Interface keywords

DAO------The first interface of a data access object

The interface can contain only static constants and cannot contain normal variables.

Modifier must be public

The methods inside the interface are abstract (only method names, no method bodies, and abstract keywords can be omitted), and cannot contain common methods

The implementation class must override the abstract method of the interface

Inheriting------extends implementation------implements

A class implements multiple interfaces, and an interface can inherit multiple interfaces

A class can also implement multiple interfaces while inheriting another class

Interface for design, features:

1. Interface is a rule provided externally

2; interface is the extension of the function

3. The appearance of the interface reduces coupling

21; Polymorphism: Same function, multiple forms

< object instanceof Type >

What's called polymorphism: the same function, multiple forms

The function itself is polymorphic, and some things have different concrete manifestation.

Prerequisites for polymorphism: 1; must have a relationship (inheritance and implementation)

2. There will be method overrides (Overrides)

Embodiment: A reference to a parent class or interface, pointing to its own subclass object

Benefits: Improved Program extensibility

Disadvantage: When a parent class reference points to a subclass object, but only the method of the parent class is accessible, the method that is unique to the subclass cannot be accessed

exception: An error occurred in the program

1; compile-time exception

2; run-time exception

Run-time exceptions include: Run errors, logic errors

try{-----------} monitor the code portion of the exception that can occur

catch (Exception type E) {-----------}

1;exception: Root class for exception hierarchies

2;arithmeticexception: Arithmetic anomalies

3;arrayindexoutofboundexception: Array subscript out of bounds

4;nullpointerexception: Accessing a null reference

5;numberformatexception: String Conversion number failed

Root class for 6;ioexception:io exceptions

Finally, whether or not an exception occurs, it will execute

No matter how many catch you have, you will perform a

throw new Exception ("------------");

E.getmessage ();

Run-time exception, do not need to force try{-----}catch (Exception type E) {------}



This article is from the "10281529" blog, please be sure to keep this source http://10291529.blog.51cto.com/10281529/1659531

JAVA 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.