The relationship between Java classes and class summary

The relationship between class and class

  • a is-a b Generalization (inherited implementation)
  • a has-a b comprising (a combination of aggregate association)
  • a use-a b 依赖 (need-a)

Four kinds of rights modifiers: public protected default does not write private (their permission descending)

Inheritance

  1. Subclass inherits the parent class using the keyword extends
  2. Object subclass can call (public protected) properties and methods of the parent class, as their use
    2.1 constructor say counted as a subclass inherited the strict sense
    simply default to call the parent when the child class constructor mobilize the class constructor
    2.2 blocks in the strict sense say counted as a subclass inherited
    block subclass can not directly call the default call is automatically executed before the constructor parent class constructor of the parent class implementation of the parent class before the subclass constructor is executed block
  3. Subclasses can add your own exclusive property or method
  4. May subclass inherited from the parent class can not meet the needs subclass, the parent class can be overridden in a subclass
  5. Each class has inherited class, if you do not write the extends keyword, the default inherit the Object class
    so the Object class is very important! It is to be understood that any reference to a type of the parent class (directly or indirectly inherits Object) Object class has no parent
  6. java is inherited (single inheritance, implement) the existence of every single class can only inherit a class (a class can only be written after the extends keyword) c ++ multiple inheritance is a
    java language is to allow single inheritance class safer, simpler. But java can achieve the effect of multiple inheritance by way of transfer
  7. Inherited forms of storage in memory
    Here Insert Picture Description
  8. Use this and super and super about this is that the object pronouns instead of
    this (instead of the current object that calls a method or property is not necessarily the object of the current class)
    objects super (instead of the current execution method when the parent class object)
    they can call the general properties and general method can be placed anywhere class members (attributes building block approach)
    Note that when calling a general method can be called back and forth (the wording may be useful compilation, execution may cause problems StackOverflowError )
    can call the constructor (the constructor in the first row)
    constructor call when the call can not be round (not easy to use the compiler)
    can not occur at the time of this and super constructor call in another class constructor row

Object class method:

  • * HashCode () The object address in memory after a calculated integer int
public native int hashCode();
  • * Equals () to compare the contents of two objects Object Defaults are ==
public boolean equals(Object obj){
	return (this==obj);
}

Summary: == can compare basic types (compare that value), you can compare reference types (comparison of address)
the equals () method is the Object class inherited methods, comparison is the default address, if you want to change it rules can be overridden method

  • * ToString () will output the print object becomes String String
public String toString(){
	return getClass().getName()+"@"+Integer.toHexString(hashCode());
}
  • getClass () Gets the class map corresponding to the object class (reflection)
  • wait () suspends the thread enters the wait state it exists method overloading
  • notify () wake-up thread
  • notifyAll () wakes up all
  • finalize () method of the default rights protected modifier is executed and corresponding to the object when the destructor is recovered by GC

Constructor: create objects
destructor: be recovered

T questions often test: the difference between final finally finalize the fact that they look similar to the name has no association

protected void finalize(){
}
  • clone () is protected modifier permission to clone

This method requires 9: mnemonic name of the method parameter method returns the value of the existence of overloaded methods commonly used several methods need to know how its source code implementation

If the method signature contains native and the absence of Procedure: Note that it is not an abstract method, it is to do things. I do not see the underlying source code in java them.
It calls the c ++ language to the core mechanism we write.
So after seeing native: Sorry, java language coming to an end, native is called procedural language written in c ++.

The method of rewriting and method overloading difference:

\ Method overrides override Method overloading overload
class It produces two classes of inheritance A class of a set of methods
The method of the parent class subclass overrides
Competence Subclasses may be greater than or equal parent There is no requirement
feature final static abstract There is no requirement
Parent class is a subclass can override final
Parent class is a subclass overrides static absent
The parent class method is abstract subclass must override (or sub-class is an abstract class)
return value Subclasses can be less than or equal parent There is no requirement
Method name Subclass is consistent with the parent class A lot of class method name must match
parameter list Subclass is consistent with the parent class Parameters for each method must match (order number type)
abnormal Runtime? Compile time? There is no requirement
If the parent class method throws an exception run
Subclasses may be disregarded
If the parent class method throws an exception compiler
Subclass thrown both the number and the type of the parent class or less
The method body SUMMARY parent class and subclass inconsistency Each method overloading of inconsistent execution

a has-a b comprising (a combination of aggregate association)

These three relationships are not the same in terms of the closeness of the relationship more relaxed
combination ----> relationship between man and the heart and the brain (part of the overall relationship and indivisible whole appears to have emerged to disappear are dying)
polymerization ----> (when there is a whole part of creation and the relationship may be separated so there is no combination of polymeric close relationship) and car wheels and computer motherboards
associate ----> people who have cars have a computer relationship (whole and parts the split may be formed together later)
will be described java program from such a relationship: as the property of another class through an object to store a class of

a use-a b-dependent (need-a) (most common)

Integral part of the relationship and not a temporary thing had a relationship together this thing done once the relationship disbanded
butcher butcher a class (butcher) can do one thing: a feminist required conditions: a pig (just do butcher pigs when the need for it, to do other things need to emphasize temporary)
to describe this relationship from a java program: a class of methods used to target another class
relational principles designed to follow the class: a high poly low coupling
coupling: generalization> contains> dependent

Published 46 original articles · won praise 16 · views 2630

Guess you like

Origin blog.csdn.net/qq_43598138/article/details/104175464