samedi 31 décembre 2016

Reflection in Java

I am new to reflection in java and I thought that since I provided the arguments to the EnemyShip constructor that will be preserved. And After changing just the name of the ship, only that will be changed. However, I got wentworth is travelling at 0 instead of what I expected : wentworth is travelling at at 3242

Last line of code should in reflectionTest be main focus

I have 2 classes:

package com.reflectionapi.demo;

import java.lang.reflect.*;

public class reflectionTest {

public static void main(String[] args) {


    Class shipClass = EnemyShip.class;
    int shipMod= shipClass.getModifiers();
    String shipModStat="";

    switch(shipMod){
    case Modifier.PUBLIC: shipModStat="Public";
            break;
    case Modifier.PRIVATE: shipModStat="Private";

    }


    System.out.println(shipClass.getName()+" "+shipModStat
            );//prints class of ship




    Method[] methods = shipClass.getMethods();


    Class[] parameters;


    for (Method method: methods){
        String methodName = method.getName();
        parameters= method.getParameterTypes();// assign method parameters to paramters array

        if (methodName.startsWith("get"))
                    System.out.println(methodName+" is a Getter Method "+
                            "it returns "+ method.getReturnType()+"\n");    

        else 
            if (methodName.startsWith("set"))

                    for (Class param: parameters){
                System.out.println(methodName+" is a Setter Method and "+
                "takes parameters "+ param.getName()+"\n");
                    }

        }

    Class superClass = shipClass.getSuperclass();
    System.out.println(shipClass+" is a subclass of "+ superClass.getName()+"\n");


    Constructor[] constructors = shipClass.getConstructors();   
    //Constructor constructors = shipClass.getConstructor(new Class[] {EnemyShip.class} ); not iterable

    Object constructorItem =null;

    for (Constructor construct: constructors){
        System.out.println(construct);

    }


    try {
         constructorItem= shipClass.getConstructor(String.class , int.class ).newInstance("XT-1800", 5000);
    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
            | NoSuchMethodException | SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    EnemyShip ship = new EnemyShip("Carl", 3242);

    ship.setName("Wentworth");
    //ship.setSpeed(1230420);

    System.out.println(ship.getName()+
            " is travelling at "+
            ship.getSpeed());



    }

}

Second class

 package com.reflectionapi.demo;

 public class EnemyShip {



public EnemyShip (String s, int m){// s= name m= speed
    /*
    name =s;
    speed=m;
 try later */

    System.out.println(s+
             " is travelling at :"+ m);
}

public EnemyShip (String s, int m, double j){// s= name m= speed

    System.out.println(s+
             " is travelling at :"+ m);
}


public String getName (){   
    return name ;
}

public void setName(String s){

    name =s;
}

public void setSpeed(int s){

    speed =s;
}

public int getSpeed(){
    return speed;
}

    private String name =""; 
    private int speed=0;

}





Aucun commentaire:

Enregistrer un commentaire