views:

255

answers:

4

I was very confused about the reflection and wrapper, I know that reflection can reflect the object into another object type, wrapper can convert the primitive type into object. Is this correct?

+1  A: 

Your concept of reflection is wrong. Reflection lets a program investigate its own classes at runtime. For example, you get an obect of an (at compile time) unknown class and find out which fields and methods it has.

A wrapper is simply a class that takes an object and wraps it, i.e. it adds (almost) no new functionality, but exposes a different interface than the original class. A special case are the wrappers for primitive types; since primitive types are not objects in some languages, e.g. Java, wrapper classes for those primitive classes allow for treating those primitve types like objects.

ammoQ
Whixh mean wrapper can wrap an object to another object type?
kenny-david: the wrapper doesn't change the wrapped object, but only uses it to do the real work while exposing a different interface (e.g. other method names, parameter order etc.) than the wrapped class. Imagine you have two classes A and B that generally do the same thing, but have different names for similar methods etc. A wrapper might wrap A, but expose the method signatures of B, so for the rest of the code, a wrapped A looks like a B - but it isn't magically turned into a B, it's just inside a wrapper that mimics B's appearance.
ammoQ
+1  A: 

wrapper can convert the primitive type into object

Ehm, you seem to be confused. A "wrapper" is usually some code that hides an API ("wraps" it), to simplify calls etc. . You are probably thinking about autoboxing, which allows you to use primitive types like their corresponding objects and vice versa.

reflection can reflect the object into another object type

No, reflection allows you to retrieve information about available classes and their members at runtime, and to invoke their functionality, even if they're not available at compile time.

See http://java.sun.com/docs/books/tutorial/reflect/ for details. Please read this, then come back if you still are confused, and ask a new question.

sleske
Ya i really very confused with this two, can wrapper wrap object to another object type instead of primitive type?
"wrapper" is a fairly general term, which can mean different things depending on context. It can also wrap fish -).
sleske
A: 

A wrapper will wrap around another object which may hide some of the complexities of using the original object / provide diffrent naming conventions etc.

forgive the C# syntax and the fairly contrived example

class SimplePerson{

    ComplexPerson _person;

    public void WalkForward (int steps){

        for (int i = 0; i < steps; i ++){
            _person.LeftFoot ();
            _person.MoveFoot ();
            _person.PlaceFoot ();
        }
    }
    // More methods
}

Reflection on the other hand can be used to retrieve methods / fields / properties and metadata in general from an object.

Again forgive the C#

SimplePerson _person;

Console.WriteLine ("Class {0} has the following methods:", _person.GetType().Name); 

foreach (var method in _person.GetType().GetMethods()){
    Console.WriteLine ("\t {0}", method.Name);
}

which should give output something like (depending on the class obviously)

Class SimplePerson has the following methods:

    Eat         
    WalkForward
    RunForward
Courtney de Lautour
I got this piece of code from google, this is why i thought that reflection is to convert an object type into another class object type. Does wrapper provide this as well?public static <A, B> B convert(A instance,Class<B> targetClass) throws Exception { B target = (B)targetClass.newInstance(); for (Field targetField : targetClass.getDeclaredFields()) { targetField.setAccessible(true); Field field = instance.getClass().getDeclaredField(targetField.getName()); field.setAccessible(true); targetField.set(target, field.get(instance)); } return target;}
This copies the value of a field in one object to another class for every field which has the same name. I wouldn't I say that it converts the objects either, but attempts to duplicate one inside of another. However it is not a wrapper
Courtney de Lautour
So does wrapper can wrap an object into another object type instead of primitive type? Is it need to implement an interface ?
I think from other comments you want an interface. If you can't modify the class (it doesn't have to be a primitive) which you want the interface on then yes you will need to wrap it - no need for reflection though.
Courtney de Lautour
Okay thanks, but if implements interface both class must extends it right?
Yeap - that is correct (15 chars)
Courtney de Lautour
A: 

I know that reflection can reflect the object into another object type

Not really. Reflection is used to dynamically access/modify objects at runtime.

Suppose you have the class Foo you can create an instance programatically with:

Object o = new Foo();

But you can also create an instance at runtime only with the name:

 public Object createInstanceOf( String className ) 
              throws InstantiationException,
                      IllegalAccessException { 
       return Class.forName( className ).newInstance();
 }

 Object o = createInstanceOf( "Foo" ) ;

That's basically what reflection is all about.

wrapper can convert the primitive type into object

In general terms a wrappers simply ... well wraps another object. In particular for Java primitives you're right, Java wrappers do wrap the primitives so they can be used as any other regular object, for instance to pass an int to an object that receive an object you'll have to use a wrapper:

public void somethingWith( Object o ) {}

....

int i = 1234;

somethingWith( new Integer( i ) );

Prior to java 1.5 you could not invoke the method somethingWith with out having to explicitly create the wrapper instance.

Today with autoboxing the compiler does that for you and you can invoke directly:

somethingWith( 1234 ) ;

I hope this helps and doesn't confuse you more.

OscarRyz
Thanks for your explanation, if I want to pass in a class object into a parameter which take in another class object izzit possible? Assume this two class have same attributes.