2016-12-31 88 views
-2

我是新來的java反射,我認爲,因爲我提供的參數將保留EnemyShip構造函數。改變船名後,只會改變。但是,我得到了溫特沃斯在0行駛,而不是我所期待的:溫特沃斯在旅行在3242Java中的反射

最後一行的代碼應該在reflectionTest是主要焦點

我有2類:

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()); 



    } 

} 

二等

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; 

} 
+1

你的EnemyShip構造函數沒有做他們應該做的事情:他們沒有設置相應的字段。如果他們不這樣做,你怎麼期望通過調用構造函數來改變字段? –

+0

我發現0改變的唯一方法是當我明確地調用setSpeed(); – BattleDrum

+0

氣墊船充滿了鰻魚但我確實提供了物體施工所需的參數 – BattleDrum

回答

1

你EnemyShip構造函數什麼都不做 - 他們不設置他們應該設置字段等美其名曰,與正確的論點是沒有意義的。您必須使用它們來設置EnemyShip字段或者是否有超級Ship類,然後通過超級構造函數調用來設置它的字段。

所以取消這個註釋代碼:

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

這樣:

public EnemyShip (String s, int m){ 
    name =s; 
    speed=m; 
} 
0

你Enemyship在做什麼,你必須指定值

name=s; 
speed=m; 

希望它有助於。