java-Object-oriented (basic knowledge)

Source: Internet
Author: User

process-oriented design thinking object-oriented design and the relationship between classes the creation and use of the definition constructor (construction method) object of the Java class Thisoverride of the keyword static keyword for the package and import keyword permission modifiers (access control) class to inherit the final keyword object Transition polymorphic abstract class interface- - -- -  - - - - - - - - - - -  - --  -- -  --  - --   --  --  - --   -- - --    --Object-oriented:1, process-oriented preparation to go to a place: first buy a car, listing, open navigation, stepping on the accelerator, across the Yellow River, through the Mount Everest ...2, object oriented thought I drove to the car, how to go to all the information about 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 basic idea of object-oriented is, from the reality of the existence of things, to construct software systems, and in the structure of the system as far as possible use of human natural thinking, object-oriented is the most simple communication between human and computer language ideas appropriate method appears in the appropriate class3, what is a class and what is an object?An object is an instantiation of a class that uses the"Properties" and "methods" correspond to the "static properties" and "Dynamic properties" that describe thingsA class is an abstract concept used to describe an object of the same class, which defines the type of object that this class of objects should have."Static Properties" and "Dynamic Properties"A class can be thought of as a template for a class of objects, and an object can be seen as a concrete instance of it. Give me a chestnut (what is a bottle--The abstraction of a class of things--static properties, dynamic properties) What is a class?students are class, bottle is class, teacher is class lift a chestnut (man-Student--Zhang San) What is an object?This student is the object, this bottle is the object, this teacher is the object, conforms to this kind of thing the characteristic certain thing, calls the object leader-Show Employee's name employee: The right method appears in the appropriate class (there is a picture of the Truth) static properties: Name: Zhang San Age:24Salary:3000Dynamic Properties: Display Name display age change name pick up salary

4, the relationship between classes and classes

1. Relationship (weakest relationship)(with the 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

2. inheritance (avoid multiple inheritance) ( There is a picture of the truth) extends

What is a kind of what

give me a chestnut . (Athlete--ball player--basketball player)

3. Aggregation (aggregation, combination )(there is a picture of the truth)

What is part of what

give me 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

4. realization of the relationship (there is a picture of the truth) implements

interface : Just define the method, how to implement the subclass to do

give me a chestnut . (Interface (Driving method), class A can be implemented, Class B can also be implemented)

5, definitions of Java classes, objects, and references define a class: public class class Name {member variable member method} refers to this class :        //instantiation in Main (reference)Class Name Variable name =Newclass name (); Variable name. Member Variable/Member Method//You can point out the member variables and member methods inside.Java: All things are objects, everything is required object is the encapsulation of attributes (member variables, static properties) and methods (functions, dynamic properties) including: Classes provided in Java (Math/random), the programmer's own defined classwhy use object-oriented thinking: Object-oriented is easier to achieve a realm: reusable (reusability), Extensibility (Extensibility)//Meaning of inheritance and extension, some places are derived meaningsComponent-oriented (Component)--a higher level of abstraction than an object WebService6, the initialization of member variables (with the figure of Truth) integer type--0floating Point Type--0.0f/dBoolean--false            Char--'/u0000 'String--NULLmember variables can be any of the data types in Java (underlying data types and reference types) What are reference types?Those classes, which are classes, that is, those objects, which also include arrays (all objects), interface member variables can be interfaces (the interface is a reference type when it does not point to any of the referenced subclasses), and the initialization value isNULL            NULL--is an object that indicates that nothing is pointing. Member variables can be initialized or not initialized (Java default initialization) The difference between a member variable and a local variable: the scope of the member variable-The relationship of the entire class class and the object C C1=NewC ();//C is the class, C1 C2 is the object, C (); Is the method.C C2 =NewC ();//C1, C2 here is the local variable

7, constructor (construction method)

When you use new to create an object, you are invoking a constructor (constructor) of this class.

The constructor method name has the same name as the class name and there is no return value (no return value and no write void)

A constructor that distinguishes between null arguments and constructors that have parameters

If you do not write anything, the program defaults to a constructor that has an empty parameter.

If you write a constructor that has parameters, it will automatically overwrite the constructor of the null parameter, so you should also write an empty parameter construction method.

notes , naming rules!!!!!!!!!!

Variable name if the definition is arbitrary, the amount of code will forget what is written. So write a good name.

do not do so String AA = "";

constructors in Inheritance :

--the constructor method of the subclass must call the constructor of the parent class (regardless of which one is called)

--super (), calling the constructor of the parent class, this () represents a reference to the current object, and calls another constructor method in this class

8, 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

That is , in the same class, use this. Another method can be called

--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 are static variables, and there is only one copy of the static variables in each class .

it's public. (Each instantiated class can be used after assignment)

-- non-static members are not accessible in static methods

-- static variables and static methods do not need to be instantiated

-- counting

-- single case mode

Package

-- easy to manage the number of large-scale software systems in a wide range of classes , to solve the same class name phenomenon

--Generate a. jar file JAR-CVF Test.jar path/*.* (or use the export option in eclipse)

9, access control, permission modifier (private (nothing to write) protected public) (there is a picture of the truth)        Puclic class name {            private type attribute 1;            Private type attribute 2;            Set, Get Method (Eg.public String getName () {        return  name;        }         void setName (String name) {        this. Name = name;        }   )        } 

10, object-oriented three major features: encapsulation, inheritance, polymorphism

Package (...)

Encapsulation is the combination of the properties and behavior of the class (object) into a separate unit, and as far as possible to conceal the inner details of the class (object), to form a boundary outside, only a limited external interface to make it contact with the outside. The encapsulated attribute makes it impossible for a part other than a class (object) to freely access the internal data (attributes) of a Class (object), guaranteeing that the program and data are free from external interference and are not misused.

Inheritance :

subclasses have properties and methods of the parent class (except for construction methods)

is an important means of implementing code reuse. Java Inheritance has the characteristics of single inheritance , that is , only inherit from a parent class, each child class has only one direct parent class, but its parent class can inherit from another class, so that subclasses can indirectly inherit more than one parent class. But its essentially division is still a parent class and subclass of the relationship.

polymorphic ( during execution ) dynamic binding, or pool binding) (There is a picture of the truth):

Polymorphic definition: means that objects of different classes are allowed to respond to the same message. That is, the same message can be used in a variety of different ways depending on the sending object. (Sending a message is a function call)

That is, the same method that can call the parent class can achieve different results

the technique of implementing polymorphism is called dynamic binding, which is the actual type of the referenced object that is judged during execution, and its corresponding method is called according to its actual type.

the role of polymorphism: to eliminate the coupling relationship between types .

overload (Overload):

is a relationship that takes place in the same class and is different for the same method.

method names are the same, only the parameter types are different or the number of parameters is different . The return value is different and cannot be overloaded.

rewrite (overwrite/override):

Redefine the method of the parent class

--in subclasses can be overridden as needed by methods inherited from 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 Alt+shif+s for ToString method (select ToString () this)

Override of the Equals method (choose Hashcode () and equals this)

11,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

12, Object Transformation (casting)

--a reference to the parent class can point to the object of its subclass (making the code more unified and extensible)

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

Abstract class (method)---Extension of class

--Class (method) decorated with the abstract keyword

-- classes that contain abstract methods must be declared as abstract classes

--Abstract classes cannot be instantiated

--the abstract method only needs to be declared, not required to be implemented

14, interface (a special abstract class, all methods are an abstract method)--- the function        that the class needs to implement -- syntax definition: public            interface interfacetest{                        }        -- a class to implement an interface using the Implements Keyword: public            class Test implements interfacetest{                        }        --  Multiple unrelated classes can implement the same interface         - A class can implement multiple unrelated interfaces (note the phenomenon of avoiding the duplicate interface methods)        - similar to inheritance, where there        is a 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, default to public, and can only be Public, the interface can inherit other interfaces and add new properties and methods (not implemented)        --a method that can be implemented from the beginning of the JDK1.8 interface, decorated with the default keyword

--end--

java-Object-oriented (basic knowledge)

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.