Java Object-oriented

Source: Internet
Author: User

Object-oriented:
1, process-oriented thinking
Prepare to go to a place:
First buy a car, listing, open navigation, step on the accelerator, across the Yellow River, through the Mount Everest ...

2, object-oriented thinking
I drive the car, how to go to the car
All the information to go to this place, all encapsulated in this car, I do not know how the car is running, I do not need to understand, I just know I can reach this place on the line

The basic idea of object-oriented is to construct the software system from the reality in the real world, and to use human's natural thinking mode as far as possible in the structure of the system, object-oriented is the simplest language thought of communication between man and computer at present.

The right method appears in the right class.

3, what is a class and what is an object?

Give me a chestnut (what is a bottle--and a class of things---static properties, dynamic properties)
A chestnut (man-to-student--Zhang San)

An object is an instantiation of a class that uses "Properties" and "methods" to correspond to the "static properties" and "Dynamic properties" that describe things.

A class is an abstract concept used to describe an object of the same class, which defines the "static properties" and "Dynamic Properties" that this class of objects should have.

A class can be seen as a template for a class of objects, and an object can be seen as a concrete instance of

What is a class
Students are classes, bottles are classes, teachers are classes

What is an object
The student is the object, the bottle is the object, the teacher is the object,
Something that conforms to the characteristics of such a thing, called an object.

Leader--Displays the name of the employee

Employee: The right method appears in the right class (there is a picture of the truth)
Static properties:
Name: Zhang San
Age: 24
Salary: 3000
Dynamic Properties:
Display Name
Show age
Change name
Receive Salary

4, the relationship between classes and classes
Association (The weakest Relationship) (with the figure of Truth):
Not good for the next definition, (the parameter of a method in Class A is an object of Class B)
Give me a chestnut (student <--School--teacher)
Teacher to lecture, this is a method, to pass a parameter in, parameter is student object
* Inheritance (Avoid multiple inheritance) (there is a picture of the truth)
What is a kind of what
A chestnut (athlete---ball player---basketball player)
Aggregation (aggregation, composition) (there is a picture of the truth)
What is part of what
Raise a Chestnut (team----captain Wang, team member)
Combination----Person: Head, body

Aggregation: This part of the class can be of this class, or it can be another class,
Combination: Each part belongs to only one class
* Realize the relationship (there is a picture of the truth)
Interface: Just define the method, how to implement the subclass to do
For a chestnut (interface (driving method), class A can be implemented, Class B can also be implemented)

5, definitions, objects, and references for Java classes
Define a class
public class class Name {
Member variables
Method
}

Class Name Variable name = new class name ();
Variable name ....

Java: Everything is the object, everywhere needs

An object is an encapsulation of attributes (member variables, static properties) and methods (functions, dynamic properties)

Classes provided in Java, programmers define their own classes
Why use object-oriented thinking:
Object-oriented makes it easier to achieve a realm:
Reusable (reusability), Extensibility (Extensibility)
Component-oriented (Component)-a higher level of abstraction than an object
WebService

The difference between a member variable and a local variable
Initialization of member variables (there is a picture of the truth)
Member variables can be any of the data types in Java (underlying data types and reference types)
What is a reference type?
Member variables can be initialized or uninitialized (Java default initialization)
Member variables are scoped to the entire class

Relationship of classes and objects
c C1 = new C ();
c C2 = new C ();//C1, C2 here is the local variable

7, constructor (construction method)
When using new to create an object, it is called a constructor (constructor) of the class, the constructor name is the same as the class, and there is no return value (no return value and no write void)

constructor and parameter constructors for null parameters

Notes, naming rules!!!!!!!!!!

Constructors in Inheritance:
--the constructor method of the subclass must call the constructor of the parent class
--super (), call the constructor of the parent class, this () call another constructor method in this class

9, keyword this, static, package, and import statements
This: (this represents a reference to the current object)
--Use the This keyword in the method definition of a class to represent a reference to an object that uses the method
--Use this when you must indicate who is currently using the method's object
--Sometimes this can handle the case of member variables and parameter names in a method
--this can be seen as a variable whose value is a reference to the current object
Static (not inside the heap nor inside the stack, in the data seg):
--Class name directly. Variables that come out are static variables, with only one copy of the static variables in each class, which is common (each instantiated object is available after assignment)
--Non-static members are not accessible in static methods
--Static variables and static methods do not need to be instantiated
Package
Easy to manage the large number of classes in big software systems, to solve the phenomenon of class name duplicate

10, access control, permission modifier (Public private default protected)
Puclic class Name {
Private type attribute 1;
Private type attribute 2;

Set, get
}

11, object-oriented three major features: encapsulation, inheritance, polymorphism
Encapsulation (...)
Inherited:
Subclasses have properties and methods of the parent class
Polymorphic ((during execution) dynamic binding, pool binding) (there is a picture of the truth):
Overload (Overload):
Method name is the same, only the parameter type is different or the number of parameters is different, the return value is not overloaded
Rewrite (overwrite/override):
Redefine the method of the parent class
--in subclasses can be overridden as needed from a method integrated in the parent class
--The overriding method must have the same method name as the overridden method, a parameter list, and a return type
--The overriding method cannot use more restrictive access than the overridden method
Override of ToString Method
Override of the Equals method

12,super, final keyword
Super: Reference to Parent class
Final value of variable cannot be changed
The final method cannot be overridden
The final class cannot be inherited

13, Object Transformation (casting)
--a reference to the parent class can point to the object of its child class
--a reference to the parent class cannot access the new members of its subclass object (including properties and methods), if hard to access--and strong
--You can use the INSTANCEOF keyword to determine whether this variable is a class or a subclass of this class.

14, abstract class (method)
--Class (method) decorated with the abstract keyword
--classes that contain abstract methods must be declared as abstract classes
--abstract classes cannot be instantiated
--abstract methods only need to be declared, not to be implemented
Abstract method:
Define method, no implementation

15, interface (a special kind of abstract class, all methods are an abstract method)
--Syntax Definition:
public interface interfacetest{

}
A class to implement an interface that uses the Implements keyword
public class Test implements interfacetest{

}
--Multiple unrelated classes can implement the same interface
--A class can implement multiple unrelated interfaces
--Similar to inheritance, there is polymorphism between interfaces and implementation classes
--the property in the interface defaults to public static final and can only be public static final
--the interface can only define methods that are not implemented, the default is public, and can only be public, the interface may inherit other interfaces and add new properties and methods (not implemented)

The picture is as follows:

Java Object-oriented

Related Article

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.