Java Learning (6)--Inherit tips

Source: Internet
Author: User

Auto Pack && Auto Unpacking

Sample

arraylist<integer> = new arraylist<integer> ();

List.add (3);

int i = list.get (0);

3 is automatically packaged into an integer type, which is then automatically unpacked to int.

Tips: Dealing with fixed-length array types Here, ArrayList is much less efficient than an array of underlying types because each element is added to be packaged. The packaging specification requires char, Byte, Boolean <=127, or short, int values are wrapped in fixed objects between -128~127, so you can implement the self-extracting process of comparing objects. Packing and unpacking is an endorsement, and it doesn't matter with the virtual machine.

The Java implementation only modifies the method to use the various holder in Org.omg.CORBA. such as IntHolder and so on. e.g. IntHolder X = new IntHolder (); X.value can get the value of the storage location, and the value changes at all references after the modification.

Example:

public static void Main (string[] args) {//TODO auto-generated method stub String num = "1";  int x = 2;  System.out.println (NUM+X); SYSTEM.OUT.PRINTLN (X+integer.parseint (num));}

Result:12 and 3.

Variable parameter method, that is, the method of the number of parameters is not fixed. For example System.out.printf ("%d +%d =%d", n, N, N);

The printf method accepts two parameters, one is a format string and the other is an object array;

Define a mutable function:

private int max (int ...  values) {int tempt = 0;    for (int v:values) {if (V > Tempt) tempt = V; } return tempt;}

This method can be called with a vast number of parameters.

Reflection:

Java.lang.reflect--Field && Method && Constructor

The Modifier class provides static methods and constants for decoding class and member access modifiers. Each type corresponds to a constant

Get class
Class C1 = class.forname ("test");
Get constructor
Constructor[] Constructors = c1.getdeclaredconstructors ();
Get con ' s return type:
String modifier = modifier.tostring (Con.getmodifiers ());
Get constructor con ' s parameters ' type
class[] Paratypes = con.getparametertypes ();
Get Method
method[] Methods = C1.getdeclaredmethods ();
method[] Methods2 = C1.getmethods ();
for (Method method:methods)
Method.setaccessible (TRUE);

/get Method Met1 ' s return type:
Class returntype = Met1.getreturntype ();
Get fields
Class C1 = Class.forName (c1.constructor (objects[] org). GetClass (). GetName (). toString ());

C1 c = Class.forName (C1.constructor (). GetClass (). GetName (). toString ()). newinstance (objects[] org);

Catching Exceptions:

try{   XXXXXXX   }catch (Exception e) {   yyyyyyyyyy   }

Enum Type:

The value of two enumeration types can be compared directly with "= =".

 Public classEntry { Public Static voidMain (String args[]) {System.out.println (Size.SMALL.toString ()); System.out.println (Enum.valueof (Size.class, "SMALL"));    System.out.println (SIZE.SMALL.GETABB ()); }        enumsize{SMALL (S), MEDIUM (M), LARGE (L), Extra_large ("XL"); PrivateSize (String ABB) { This. ABB =ABB; }                 PublicString Getabb () {returnABB; }                PrivateString ABB; }}

Results:

Smallsmalls

Inheritance design:

Public operations and domains are placed in the superclass; Do not use protected domains; inheritance is used only when the is-a relationship occurs; The method implementation of a subclass cannot be completely different from the inherited parent class method, except that the parent class method is a subset of the subclass method ; Use polymorphism to avoid excessive use of if and else due to different objects; do not overuse reflection (because the reflection mechanism uses only the error message during the run).

Java Learning (6)--Inherit tips

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.