java EE study concluded ------- fifth week

fifth week

interface

Defined interfaces

  • An interface corresponding to particular abstract class that defines the way, similar to the components of the abstract class

    • Use interface keyword defines an interface

    • No constructor, can not create object

    • Can only be defined: public static constants, public abstract methods

    • example

      interface MyInterface{
      public static final String FIELD =“Value”;
      public abstract void method();

      }

  • Interface to a standard

  • Interface is a standard

Similarities and Differences interface class

  • the same:

    • Files can be compiled into bytecode
    • You can not create object
    • It can be used as a reference type
    • Object class includes a method as defined in
  • different

    • All properties are public static constant, implicitly modified using public static fianl
    • All properties are constant public static implicit use public abstract modification
    • No constructor, dynamic code block, static code block

Application Interface

  • What is the interface:

    • Microscopic concepts: the interface is an ability and conventions

      • Definition of the interface: the ability to represent some kind of
      • Defined method: specific capacity requirements
    • experience:

      • java as single inheritance, the parent class when the species can not meet the needs of sub-categories, expand the subclass can implement the interface capability
    • Interface supports multi-realized, for a variety of classes to expand capacity

  • Interface Specification

    • Any class that implements the interface, all the abstract methods in the interface must be implemented, such as the otherwise abstract
    • When the abstract methods in the interface implementation, access modifier must be public
  • Reference interface

    • Like the parent class, interface can also be declared as a reference, and point to the object implementation class

    • note

      • Only callable interface declared in the method, can not call the class in a unique way to achieve
      • Can achieve strong back to class type itself, a unique method calls

Polymorphic interface

  • example

    public class TestPolymorpfic{

    public static void main(String[] args){

         Dog myDog = new Dog();
        Animal a = myDog;
        Runnable r =myDog;
        Swimmable s =myDog;
    

    }

    }

    interface Runnale{
    public absrtact void run();

    }

    interface Swimmable{
    public abstract void swim();

    }

    Animal class {abstract
    public void EAT () {} // inherited methods
    public void sleep () {} // inherited methods

    }
    Class Animal Dog the extends the implements the Runnable, swimmable {
    public void RUN () {} // interface method
    public void swim () {] // interface method
    public void shout () {} // unique method

    }

    • Various types of reference point to the same object, representing different perspective view of an object
    • Different references can see a range of different objects, you can only call itself the type declared in section
  • Different reference types, the method can be called only in the type itself declared

Common Interface relationship

  • Class and class

    • Single inheritance
    • extends the parent class name
  • Classes and interfaces

    • Multi-realization
    • implement an interface name, the name of the interface 2, the interface name n
  • Interface and Interface

    • Multiple Inheritance
    • 1 extends parent interface, parent interface 2, n parent interface

Interface Specification

  • When any class that implements the interface, all the abstract methods in the interface must be implemented, otherwise, such as an abstract class
  • When the abstract methods in the interface implementation, access modifier must be public

Constant Interface

  • Commonly used in a plurality of fixed value or a variable representing the state, in the form of static constant defined in the interface of the unified management, improve code

Callback Interface

  • First, the user interface, after the implementer interface
  • Callback interface: first, the user interface, the programmer has to realize Interface: Calling Tools: Users Interface Interface: implementors standard interface: Programmer

Interface benefits:

  • Coupling procedure is reduced
  • More natural polymorphic
  • Design and realization of complete separation
  • Easier to build a framework
  • Easier to replace specific implementation

Common categories:

Inner classes

  • Members of the inner class

    • Is defined within the class, instance variables and instance methods of the class the same level
    • A portion of the outer class instance. When you create an inner class object, you must rely on external class object
    • When the same name is present outside the class properties, the internal class, class attributes priority access to internal
    • Members of the inner class can not define static members
  • Static inner classes

    • Do not rely on external object, or create direct access by class name, declare static members
    • Can directly access only static members of the outer class (instance members need to instantiate an external class object) outer.Inner inner = new Outer.Inner () Outer.Inner.show ():
  • Partial inner class

    • Outside the class defined in the method, scope and create objects only within the current methods
    • Local inner class access methods outside the class when the current local variables because it could not guarantee their life cycle with the same variable, the variable must be modified to final
    • Of restricted use of
  • Anonymous inner classes

    • No local inner class class name (all features are the same as the partial inner class)

    • You must inherit a parent class or implement an interface

    • Syntax merger can only create a class object definition class, the implementation class, create objects

    • advantage:

      • Reduce the amount of code
    • Shortcoming

      • Poor readability
  • The concept of inner classes: a complete re-definition of the class within a class

  • Features inner class

    • It may generate a separate file after compiling byte code
    • Inner class can directly access the private members of the outer class, without destroying the package
    • Provides the necessary internal functional components of the outer class

object class

  • Super class, the base class for all classes of direct or indirect parent, tree located at the top level to continue

  • Any class, if there is no writing extends to inherit a class display, by default inherit the Object class directly, or indirectly, to continue to

  • A method as defined in the class Object, all objects have a method

  • Object types can store any object

    • As a parameter, a pharmaceutically any object
    • As a return value, to return any object

getClass () method

  • public fianl Class<?>get Class(){}
  • Returns the actual object reference is stored in the type
  • Application: Typically two references for determining the actual storage object types are consistent

hashCode () method

  • public int hashCode(){}
  • Returns the hash code value for this object hexadecimal
  • Calculated hash algorithm based on the object of the address or number or string of values ​​of type int
  • Hash code is not unique, the same subject may be guaranteed to return the same hash code, try to ensure that different objects return different hash code

toString () method

  • public String toString(){}
  • Returns a string representation of the object (forms)
  • Coating method according to program requirements, such as: display property value of each object

equals () method

  • public boolean equals (Object obj){}
  • The default implementation is (this == obj), compare two objects are the same address
  • Can be covered, compare the contents of two objects are the same

equals () method covering step

  • Comparison of two references point to the same object
  • Whether obj is null
  • Analyzing; two reference points to the actual object types are consistent
  • Cast
  • Sequentially comparing each attribute value are the same

finalize () method

  • When the object is determined to be garbage objects, this automatically invoked by jvn method for marking garbage objects, into a recovery queue
  • Garbage Object: No valid reference point to the object, the object is garbage.
  • Garbage Collection: GG destroyed by the garbage objects, the release of data storage space
  • Automatic recovery mechanism: JVM running out of memory, one-time recovery of all garbage objects
  • Manual collection mechanism: using System.gc (): inform the JVM garbage collection

Wrapper class

  • definition:

    • Basic data types corresponding reference data types
  • Object can unify all the data, the default value is null packaging

  • Basic data types

Conversion type with the packing, unpacking

  • 8 kinds of packaging embodiment provides conversion between different types of

    • Numer parent class provides six common method
    • parseXXX () static method
    • valueOf () static method
  • Note: need certification package types are compatible, otherwise an exception is thrown NumberFornamatException

  • JDK 5.0, automatic packing, unpacking. Basic data types and automatic conversion packaging

Integer buffer

  • java pre-create the 256 packaging commonly used integer type object
  • In actual application, the object has been created for multiplexes

String class

  • Strings are constant, immutable after creation
  • String literal character string stored in the pool can be shared
  • String s = "Hello"; generate an object, stores the string pool
  • String s = new String ( "Hello"); // create two objects, stacks, each memory cell a

Common method

  • public char charAt (int index): Gets the character according to index
  • public boolean contains (Stirng str): determine whether the current string comprising str
  • public char [] toCharArray (): converts a string into an array
  • public int indexOf (String str): str Find the first occurrence of the subscript, the presence of the index return; does not exist. Or -1
  • public int lastIndexOf (Stirng str): Find a subscript index of the last string that appears in the current string
  • public int length (): returns the length of a string
  • public int length (): returns the length of a string
  • public String trim (): remove the spaces around the string
  • public String toUpperCase (): turn the lowercase to uppercase
  • public boolean endWith (String str): determines whether the end of string str
  • public String replace (char oldChar, char newChar): Replace the string with a new string into
  • public String [] split (String str): Split made according str

The variable string

  • StringBuffer: variable-length string, JDK1.0 provide, slow operating efficiency, security thread
  • StringBuilder: variable-length string, JDK5.0 provide fast operating efficiency, thread safe

BigDecimal

  • 除法:Bigdecimal(BigDecimal bd ,int scal,RoundingMode mode)

  • Parameters scal: Specifies the accuracy of several decimal places

  • Parameters mode

    • Designated choice mode fractional part, usually rounding mode
    • Value of BigDecimal.ROUND_HALF_UP

Common categories

set

Concept: object container. Object storage objects, can replace the array

Features: tools container, defines a plurality of objects commonly used method of operation

Location: java.util *.

Collection collection system

The root interface architecture, on behalf of a group of objects a "collection"

  • List

    List interface features: orderly, subscript, elements can be repeated

    • ArrayList
    • LinkedList
    • Vector
    • 4 subtopics
  • Set

    Set interface features: disordered, no subscript ', the elements can not be duplicated

    • hashSet

    • SortedSet

      • TreeSet

Parent interface Collection

  • Features: representatives of a group of objects of any type, random, unsubscripted

  • method

    • boolean add (Object obj) // add an object
    • boolean addAll (Collection c) // add all objects in a collection to this collection
    • void clear () // clear all objects in this collection
    • Boolean contains (Object o) // Check whether this collection contains an object o
    • boolean equals (Object o) // This comparison is equal to a specified set of objects
    • boolean isEmpty () // determines whether the set is empty
    • Boolean remove (Object o) // in this collection to remove objects 0
    • int size () // returns the number of elements in this set
    • Object [] toArray () // this set into an array

List collection

  • List sub-interface

    • Features: orderly, subscript, elements can be repeated

    • method

      • void add (int index, Object o) // insert an object position index
      • boolean addAll (int index, Coolection c) // add the index to this position a set of elements in the collection
      • Object get (int index) // Returns the elements in the collection at the specified position
      • List subList (int fromIndex, int toIndex) // returns the set of elements and between fromIndex toIndex
  • List implementation class

    • ArrayList [focus]

      • Array structure to achieve, fast queries, additions and deletions slow
      • JDK1.2 version of the operating efficiency of fast, thread-safe
      • Open space must be continuous, fast queries, additions and deletions slow
    • Vector

      • Array structure to achieve, fast queries, additions and deletions slow
      • JDK1.0 version, operating efficiency is slow, thread-safe
    • LinkedList

      • Linked list structure to achieve, additions and deletions fast, slow queries
      • No need to continuously open up space, slow queries, additions and deletions fast

Pan Collections

  • Concept: parameterized types, type-safe collections, forced collection of elements must be the same type

  • Feature

    • You can check at compile time, rather than runtime exception is thrown
    • Access, do not have to type conversion (unboxing)
    • Different generic references can not be assigned to each other, the absence of generic polymorphic

Colletions Tools

  • Concepts: collection tools, defines a common set of access methods other than

  • method

    • public static void reverse (List <?> list) // reverse the order of elements in a set
    • public static void shuffle (List <?> list) // reset randomly set the order of elements
    • public static void sort (List list) // ascending order (element type must implement Comparable interface)

Set collection

  • Set sub-interface

    • Features: disordered, no subscript elements can not be repeated
    • Methods: All methods inherited from the Collection
  • Set implementation class

    • Hash Set [focus]

      • Based HashCode implementation elements will not be repeated
      • When the hash code into elements at the same phase, calls equals confirmed as the result is true, the latter deposit rejected
    • LinkedHashSet

      • HashSet list achieved, according to the list is stored, to retain the insertion order
    • TreeSet

      • Based on the sort order elements will not be repeated
      • Implements SortedSet interface automatically order the collection elements
      • Type element object must implement the Comparable interface, specify a collation
      • Determine whether duplication via Method CompareTo

Map collection system

  • hashMap

  • SortedMap

  • Features map interface

    • For storing an arbitrary key-value pairs (Key - value)
    • Key: disorderly, no subscript is not allowed to repeat (the only)
    • Found: disordered, no subscript, allowing repeat
  • Map parent Interface

    • Feature

      • Storing a pair of data (key-Value), disordered, no subscript key is not repeated, the value may be repeated
    • method:

      • V put (K key, V value) // stored objects to the collection. The associated key. key repeat coverage at cost
      • Object get (Object key) // Get the value according to the key corresponding to
      • Set // returns all worthwhile collection Collection
      • Set <Map.Entry <K, V >> // Set matched set of keys
  • Map collection implementation class

    • HashMap [Key]:

      • JDK1,2 version, thread-safe, high operating efficiency; allows null as a key or value
    • Hashtable

      • JDK1.0 version, thread safety, operational efficiency slow; do not allow null as a key or value
    • Properties

      • Hashtable subclass, and value are key requirements String. Typically used to read the configuration file
    • TreeMap

      • SortedMap implements the interface (Interface Map is a child of), can automatically sort the Key

to sum up

The concept of the collection

  • Container object, object store objects, define common ways of operating a plurality of objects

List collection

  • Orderly, subscript element can be repeated. (ArrayList, LinkedList, Vector)

Set collection

  • Disorder, no subscript elements can not be repeated. (HashSet, LinkedList, TreeSet)

Map collection

  • Storing a pair of data, disorder, no subscript key is not repeated, the value may be repeated. (Hashmap, HashTable)

Collections

  • Collection tools, defines a set of common access method other than
Published 32 original articles · won praise 9 · views 3140

Guess you like

Origin blog.csdn.net/weixin_43501566/article/details/104760266
Recommended