Javaoo object-oriented focus attention (II)

Source: Internet
Author: User

Javaoo point of attention in object-oriented (ii)

1. Package:

encapsulation is mainly embodied in: Assembly class and information hiding.

    A. information hiding:① access modifier:private

          implementation of the ② method:get/set method,run () method - Control Flow

    B. assembly class: definition of ① class: attributes, constructs, behaviors (methods), packages, etc.

          use of the ② class:new object,"." method, reference type array

2. access Modifiers:

  Public: This class, the same package (subclass and non-subclass) and different packages (subclasses and non-subclasses) (All classes callable)

  Protected: This class, the same package (subclass and non-subclass) and different packages (subclasses) (same package and subclass can be called)

default Access modifier: This class, the same package (subclass and non-subclasses) (same package callable)

  Private: This class (only this class can be called)

the encapsulation of the 3.get/set method embodies:

  A. you can provide only get, or only set, to make the property read-only or write-only.

  The B.get method can provide permission validation,and theset method can provide a data validation check.

  C. can hide the internal properties of the storage form.

4. Inheritance:

  A. definition: In object-oriented programming, you can create a new class by extending an existing class and inheriting the properties and behaviors of that class, which is called inheritance.

  B. subclasses and Parent classes:

    ① Relationship:is-a

    ② Parent Class (base class, superclass): Having a common property, behavior

    subclass (derived class): Has a unique property, behavior

    ③ keyword:extends

    ④ pros and Cons: Because java inheritance is a single inheritance, the advantages of a clear hierarchy, the disadvantage of more than the richness of inheritance is not enough (but the interface can compensate for this)

    ⑤object: Root classes for all classes and Arrays

  C. Memory Overlay: Mr. Cheng is the part of the parent class object, then generates the subclass-specific part, and finally forms the complete subclass object.

  D. Note: The construction method cannot be inherited, and cannot be overridden.

5. overrides and overloads:

  A. rewrite: The subclass inherits the method of the parent class and implements it again. (between classes that have an inheritance relationship)

  B. overloading: In the same class, there are multiple methods with the same name, but the parameter list is different and each has its own implementation. (in the same class)

  C. overrides must meet:

    ① method names must remain consistent

    ② parameter list must be consistent (number, type, order)

    ③ return type must remain consistent

    The ④ access modifier must be greater than or equal to the parent class

    The ⑤ subclass cannot throw more exceptions than the method before the parent class overrides.

Note: " more " here does not mean that the number of Exceptions after the throw is more, but refers to a larger range.

6.toString ():

  A. function: Returns a string description of an object

Full class name +at+ reference (the address after the hash code is encrypted)

Com.lovo. [Email protected]

  B. use:① When we print an object, or when a string operation is performed on an object, it is called the ToString () method of the object .

      The toString () ② from Objiect returns the " full class name +at+ returned binary reference ". We can rewrite toString ()as needed.

7.this and Super

  A. properties and methods (xxx. ):

    ①this represents the current object;Super represents the parent part of the current object

    ②this can manipulate all the properties and methods defined on the book class;Super cannot manipulate properties and methods defined on a book class

    Both ③this and Super can only manipulate properties and methods that are allowed to be accessed by the access modifier defined on the parent class;

Summary: preferred with This, the Super isused only when the effect of the overridden method is called in this class .

8.== and . Equals

  a.==: Determines whether two references point to the same object.

  B. Equals: Determines whether two objects are equal in business. (Most of the time you need to define your own overrides)

9.final keywords :

  A. when modifying a class: This class cannot be inherited (final class / End- state Class / eunuch Class)

  B. when modifying a method: The method cannot be overridden

  C. When modifying variables: constants, cannot be modified

10.static keywords :

  A. modifier properties:① Usage: You can use classes . and Objects . 

        ② Effect: All classes share a value

        ③ Memory: stored in the static area of the data segment during the load period

  B. modification method:① Usage: You can use classes . and Objects . 

        ② Effect: Cannot manipulate non-static properties or methods of this class and cannot manipulate this or Super Object

        ③ mechanism: Non-static content cannot be manipulated because of priority loading. (There may be no objects due to object-independent)

  C. modifier initialization block: Static initialization block (see article one)

  D. modifier class: A special case of a member's inner class - static inner class (see article)

  E. static characteristics:① is usually static, regardless of the object (the object of this class), are class-level

         ② usually static , in the loading period will have special treatment

One. initialization block:

  A. instance initialization block:

    ① the code block is automatically executed when the new object is constructed, andthenew one executes once

    The ②new object is constructed in 4 steps, and the block is executed before step 3 , before the object input is initialized.

    ③ The property is now assigned in the initialization block and is overwritten by the assignment of step 3

{

/* Statement block */

}

  B. static initialization block:

    ① The code block is automatically executed when the class is loaded.

    ② A class is loaded only once, so static initialization blocks are only executed once

    ③ so many of the pre-loaded actions that open the resource and enable the link can be written to the static initialization block

static {

/* Statement block */

}

Inner class:

  A. definition: In java , you can also define classes within a class, a class that is defined inside a class, called an inner class

  B. features:① A separate class, with its own properties, behaviors, constructs, etc.

      ② A separate class file

      ③ also needs to be compiled

      ④ can also produce objects

  C. classification: Member Inner Class (Exception: Static inner Class), Local inner class (Exception: Anonymous inner Class)

member Inner class:

  A. location: Written directly in the outer class, position and property, construction, method parallel

  B. modifier: You can have an access modifier that represents whether the external class can be used

Regardless of the access modifier, all methods of this class (The outer Class) can be used

  C.class file Name: External class class name +$+ Inner class class name

  D. external use: first generate an external class object, and then use the special syntax (external class . New) to produce an inner class object, and then invoke the method of the inner class object

Outclass out = new Outclass ();

Outclass.innerclass1 oi = out.new InnerClass1 ();

  E. static inner class -- A special case of a member's inner class :

Using static internal classes externally: You can access operations directly with an outer class name without generating an external class object

Outclass.innerclass2 oi = out.new InnerClass2 ();

  F. Note:① Inner class if you want to invoke the properties of an external class, write:

OUTCLASS.THIS.A = 100;

     ② static inner classes can only use static properties or static methods of external classes, write:

outclass.b = 100;

local inner classes:

  A. location: In java , the class is defined inside the method and becomes a local inner class

  B. modifier: no access modifier

  C.class file name : External class class name +$+ ordinal + Inner class class name

  E. use: Direct generation of objects

  F. anonymous inner class - A special case of a local inner class:

    ①class file name : External class class name +$+ ordinal

    ② Function: Define this class at the same time as the object is produced, only once

New Object () {

/* Properties */

/* Behavior */

}

  G. Note: Local inner classes and anonymous inner classes cannot manipulate local variables (very special) of the method in which it resides

Multi-State

  A. Purpose:★ The same behavior, different is realized ★

  B. classification:① static polymorphism: Determining the effect of behavior at compile time

      ② Dynamic polymorphism: In the compilation period can not determine the effect of the method implementation, after running according to the different binding object to determine the effect of the method final execution

  C. implementation: Static polymorphism: Overloading implementation

Dynamic polymorphism: Dynamic binding, overriding implementation (common use)

Dynamic Binding: Transformational technology,instanceof keywords

Transformation Technology

  A. transformation of basic data types:

    ① Automatic type conversion - Small-range data type conversion to a wide range of data types (No risk / positive success)

    ② Coercion Type conversion -- large range of data types to a small range of data type conversions (risk / loss accuracy / meaningless )

  B. conversions of reference data types: (Prerequisite: There is an inheritance relationship)

    ①- up type conversion - automatic type conversion (no risk / positive success)

    ② down type conversions - coercion of type conversions (risk / possibility of throwing exceptions, terminating runs)

Note: This class object, or parent reference to a subclass object, can succeed only after it is run.

17.instanceof keyword (operator)

  A. role: Used to determine the true type of the runtime object (that is, to determine whether an object is an instance of a class)

  B. return:true or false

  C. syntax:

/* Object */instanceof * * Class name */

Abstract:

  A.abstract Keywords:

    ① Modification Method: Indicates that the class has this method, but not the implementation of this method, it should be determined by its subclasses (can not modify the structure, properties)

public abstract void response ();

    ② Decorated class: Indicates that this class is abstract class. Abstract classes cannot produce objects!!! It can only be the parent Class!!!

Public abstract class girl{

Todo

}

  B. Note:① class with abstract methods, must be abstract class

      ② Abstract classes do not necessarily have abstract methods (but lose the meaning of abstract design)

      The ③ abstract class, in addition to the abstract modifier, is internally like a normal class, and can have: properties, constructs, implemented methods.

Javaoo object-oriented focus attention (II)

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.