Core java succession study notes

  Inheritance 

  1. First step in the succession of 

  1.    Syntax: 

  Class child extends father ( 

  ) 

  2.    On the super () 

  Unless subclass constructor function meet in the super-category default constructor function settings, or it must be matched with appropriate parameters explicit call super method.    Call super must subclass constructor function in the first line. 

  Public void raiseSalary (double byPercent) ( 

  Day today = new Day (); 

  Double bonus = 0.5 * (today.getYear () - hireYear ()); 

  Super.raiseSalary (byPercent + bonus); 

  ) 

  3. C + + Note: 

  Java and C + + is similar to the inheritance. 

  Java does not support multiple inheritance. 

  4. Use subclass 

  Object can be assigned to the category of super-type variable 

  Such as: 

  Employee [] = new Employee staff [3]; 

  Manager boss = new Manager ( "Cral Cracker," 75000, the new Day (1987,12,15)); 

  Staff [0] = boss; 

  / / Employee category is the father, is the son of Manager, and staff [0] boss at the memory of the same region.    However, the compiler that only staff [0] is the target employees. 

  Subclass objects can be transmitted to any category of super-parameters.    Conversely general not to set up, usually can not assign super-type object subclass objects. 

  For example, the following assignment is not legitimate: 

  Boss staff [0] = / / wrong 

  Subclass and ultra-type object at least as many objects of the data.    Because of the succession, can only increase domain, can not be deleted domain. 

  5. Polymorphism 

  (1) sub-category with the same name and check whether the same parameters.    If so, call on the method.    Otherwise, the father of Java will be to find the same name and same parameters.    If so, call on the method. 

  (2) If the definition of sub-category method and the method of ancestors of the same name, and have the same parameter table.    Then the sub-class method of hiding their ancestors in the way of the same name. 

  (3) the name and method parameter table method commonly referred to as the signature.    In Java, or if the method of super-category and sub-category method has the same signature and return type different, would result in compile-time error. 

  For example, not in the category of employees (father) There is a void raiseSalary (double) in the manager category (a) are int raiseSalary (double) method 

  (4), the key is to achieve polymorphism late binding is late binding. 

  (5) C + + Note: In Java, the method will be no need for the false statement, which is the default behavior.    If you do not want to approach a virtual method, the method can be added to the final Xiuchifu. 

  6. Prohibit inheritance: final 

  (1) Sometimes, users need to prevent others from their own class derived from the new category.    Not a category known as the father of the final category. 

  (Final class Card 

  ) 

  (2) can also be a description of the kind of the ultimate solution.    If this be the case, any sub-category method can not cover the (final category of all methods are automatically become the ultimate method). 

  (3) a class or method is that for the final for the following reasons: 

  — The efficiency of dynamic than static for the need for more additional costs, slower than running virtual methods. 

  Security 

  (4) in the C + +, there was no way to stop derived class coverage of function.    In C + +, can be prepared can no longer be derived class, but it requires hard skills. 

  2.    Conversion 

  1. Grammar 

  Manager boss = (Manager) staff [0]; 

  Good programming style in a certain type of conversion prior to certification of whether the target is another example.    This completed by the instanceof operator.    For example: 

  If ([1] instanceof staff (Manager)) ( 

  Boss = (Manager) staff [1]; 

  ) 

  Window w = (Window) staff [1] will be unsuccessful because the window class is not an employee of the sub-class. 

  2. Attention 

  In fact, a type conversion is not usually a good idea. 

  3.    Abstract class 

  1. Grammar 

  (Public abstract class Message 

  Public abstract void play () ( 

  ) 

  ) 

  An abstract class may have some specific data and methods.    The key is, in addition to ordinary methods, the abstract class should at least contain an abstract method.    Abstract methods require all derived from the abstract class of non-abstract class will be the realization of the abstract methods. 

  ****** ****** Anomaly on the capture 

  Syntax: 

  Try ( 

  Abnormal might be thrown code 

  ) 

  Catch (abnormal type e) ( 

  Emergency operation 

  ) 

  Description: 

  Is a very complex issue, anomalies, anomaly is not good practices ignored, Java and C + + abnormal mechanism is similar. 

  4. Interface 

  1. Use interface reasons: 

  Java interface designers choose not inherit because more and more of succession to become very complex compiler (such as C + +), or efficiency is very low (such as Eiffel), the interface will also allow Java to achieve "callback function." 

  2. Interface definition: 

  For example: To create a category called sort of interface can be any sort of category to the interface can be used. 

  (Public interface Sortable 

  Public int compare (Sortable b); 

  ) 

  3. Achieve Interface: 

  Inherited the former, in the post-Interface 

  Class Title extends Rectangle implements Sortable ( 

  Public int compare (Sortable b) ( 

  Tile tb = (Tile) b; 

  Return z - tb.z 

  ) 

  Private int z; 

  ) 

  4. Interface characteristics: 

  Interface can not be used to instantiate new 

  Sortable x = new Tile (……); 

  Tile y = new Tile (……); 

  Interface also can be extended to create another interface.    This has created a number of interface inheritance chain. 

  (Public interface Moveable 

  Public void move (double x, double y); 

  ) 

  Public interface Powered extends Moveable ( 

  Public String poweredSource (); 

  ) 

  Although not in the interface definition in the domain examples, but can be defined constants. 

  Such as: 

  Public interface Powered extends Moveable ( 

  Public String poweredSource (PoweredVehicle); 

  Public final int speedLimit = 95; 

  ) 

  Class multiple interfaces can be realized.    This category of acts in the definition, extremely convenient, highly adaptive. 

  Java has a band within an important interface, called replicable category (Cloneable) interface.    If certain types of replication can be achieved interface, object class in the clone method can be the objects of the type of copy.    To enable class can be reproduced, should realize interface Sortable. 

  5. Interface and callback 

  In Java, to achieve callback function interface is the only way 

  5.    Object of the original super-category 

  1. Object class 

  Object class is the base class for the most original, Java, the expansion of both categories of each object class, generally do not have to write the following code: 

  Class Employee extends Object 

  If no explicit statement of the father of certain categories, like his father, which is Object class. 

  2. API on the Object class description 

  Boolean equals (Object obj) 

  A method to test whether the equivalent of another object, that is, whether the judge two points in the same region of memory 

  Java layer in the other categories in order to effectively compare be free coverage equals method.    Users often need to cover category equals method 

  Object clone () 

  Create Object a copy.    Examples of the new Java memory allocation, and the current type of memory copy of the contents to a new examples of memory. 

  String toString () 

  Back to that the current value of the target string.    To print the current status of almost all of this method are covered. 

  Can be used "and" + x replace x.toString () effect of the two identical. 

  3. Object packaging 

  All the basic types have corresponding category.    For example, the integer type Integer type int relatively basic and should be.    These types of packaging commonly known as objects. 

  Packaging categories: 

  4 derived from digital Number: integer type Integer long integer type Long double-precision floating-point type of Double Float 

  Character Boolean type of character Boolean 

  Packaging is the final category, that is, can no longer be inherited. 

  String into integer int x = Integer.parseInt (s); 

  Precision dual string into double x = new Double (s). DoubleValue (); 

  NOTE: 

  In the actual programming, we must deal with a string precursor or at the end of spaces, or non-numeric string of characters.    Correct usage are as follows: 

  Try ( 

  X = new Double (s). DoubleValue (); 

  ) 

  Catch (NumberFormatException e) ( 

  X = 0; 

  ) 

  Java.lang.Integer 

  Int intValue () returns the integer value of the whole object 

  Static String toString (int i) Top 10 band integer i representatives of the new String object. 

  Static int parseInt (String s) return to the integer value 

  Static Integer valueOf (String s) Return to the new integer object 

  6.    Class definition of categories, namely Class category 

  1. Joint use forname and newInstance method can be stored in the string, according to the type of object will be created. 

  String s = "Manager"; 

  Manager m = (Manager) Class.forName (s). NewInstance (); 

  2. System.out.println (e.getClass (). GetName () + "" + e.getName ()); 

  If e is an employee, Print Employee Harry Hacker 

  If e is the manager, Print Manager ssHarry Hacker 

  3. API Notes: java.lang.Class 

  String getName () to return to class name 

  Class getSuperclass () to return to the Super such categories as well as a Class object 

  Class [] getInterfaces () returns an array of Class object, the realization of such an array designated Interface 

  Boolean isInterface () If such is a interface, it returns true or false return. 

  String toString () returns the name of class or interface. 

  Static Class forName (String className) return to such examples of the new 

  7.    Protected access protected 

  1. C + + Note: 

  In C + +, only subclass can visit protected domain, and Java, as well as all sub-categories in the same package in the other category is the domain can access protected. 

  2. Clone method is protected 

  3. Listed below Java Xiuchifu visit of the four, their control visibility. 

  (1) category can see only private private ———— 

  (2) Java environment that ———— public public 

  (3) package and that all sub-categories protected ———— protection 

  (4) packets that ———— default, no Xiuchifu 

  8.    Design tips on succession 

  1. Public operation and the public domain classified super-category 

  2. Succession simulation with a "yes" relations 

  3. Not to use inheritance, unless all available methods of succession 

  4. Use polymorphism, instead of types of information 

  When found the following code, 

  If (x is of type1) action1 (x); 

  Else if (x is of type2) action2 (x); 

  Should consider the use of polymorphism.    To the definition of type1 and type2 father of a public or interface, action () method, then just call: 

  X.action (); 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags:

Releated Java Articles

Comments

Leave a Reply