2015-05-21 29 views
1

我有兩個類這幾乎實現了兩種不同的數值類型相同的操作(除了getHexadecimalValue()法):組合兩個類似的數字類實現

public class IntegerType 
{ 
    private int value; 

    public IntegerType() 
    { 
     value = 0; 
    } 

    public void setValue(int value) 
    { 
     this.value = value; 
    } 

    public int getValue() 
    { 
     return value; 
    } 

    public String getHexadecimalValue() 
    { 
     int integerValue = (int) getValue(); 

     String hexadecimal = ValueConversions.toHexadecimal(integerValue); 

     return hexadecimal; 
    } 
} 

public class FloatingPointType 
{ 
    private float value; 

    public FloatingPointType() 
    { 
     value = 0; 
    } 

    public void setValue(float value) 
    { 
     this.value = value; 
    } 

    public float getValue() 
    { 
     return value; 
    } 

    public String getHexadecimalValue() 
    { 
     float floatingValue = (float) getValue(); 

     int intBits = Float.floatToRawIntBits(floatingValue); 

     return ValueConversions.toHexadecimal(intBits); 
    } 
} 

我我想知道通過如何減少冗餘的最好方法是什麼定義一個超類叫做NumberType這樣的:

public abstract class NumberType 
{ 
    protected Number value; 

    public NumberType() 
    { 
     setValue(0); 
    } 

    public void setValue(Number value) 
    { 
     this.value = value; 
    } 

    public Number getValue() 
    { 
     return value; 
    } 

    public abstract String getHexadecimalValue(); 
} 

現在的問題是,任何數量的可以傳遞給我的繼承類,但我只希望接受intsfloats分別同時仍保持冗餘降到最低:

public class IntegerType extends NumberType 
{ 
    @Override 
    public String getHexadecimalValue() 
    { 
     // Crashes on runtime if the value doesn't happen to be of the expected type 
     int integerValue = (int) getValue(); 

     String hexadecimal = ValueConversions.toHexadecimal(integerValue); 

     return hexadecimal; 
    } 
} 

這可以通過仍然保持適當的類型檢查來完成嗎?

回答

2

你可以試試這個方法。

public abstract class NumberType<T extends Number> { 
    protected T value; 

    public NumberType(T value) { 
     this.value = value; 
    } 

    public void setValue(T value) { 
     this.value = value; 
    } 

    public T getValue() { 
     return value; 
    } 

    public abstract String getHexadecimalValue(); 
} 

public class FloatingPointType extends NumberType<Float> { 
    public FloatingPointType() { 
     super(0f); 
    } 

    public String getHexadecimalValue() { 
     return ValueConversions.toHexadecimal(Float.floatToRawIntBits(value)); 
    } 
} 

注:浮點和整數,這兩個類有靜態toHexString方法,其如果你是舒適的使用他們,你可以直接使用。

  1. public static String toHexString(float f)
  2. public static String toHexString(int i)
+0

對於浮點數的'toHexString'方法沒有做我想要的,所以我需要我自己的(32位十六進制表示)。是否也可以在超類中對'value'進行0初始化?我想不是,但也許我錯過了一些東西。另請注意,劇組現在是多餘的。謝謝:) – BullyWiiPlaza

+0

我不認爲有可能在超類中進行'0'初始化。但我已經更新了一些代碼。 –

1

這可以超載

例如做:

public abstract class NumberType 
{ 
    private Number value; 

    public NumberType() 
    { 
     setValue(0); 
    } 

    public void setValue(float value) 
    { 
     this.value = value; 
    } 

    public void setValue(int value) 
    { 
     this.value = value; 
    } 

    public Number getValue() 
    { 
     return value; 
    } 

    public abstract String getHexadecimalValue(); 
} 

您還可以添加,則:

public int getIntValue() 
{ 
     return value.intValue(); 
} 

public float getFloatValue() 
{ 
     return value.floatValue(); 
} 
+0

的(數字值)是這樣的請告訴我做'NumberType'的2個不同的子類,然後點?除了使用你的方法,'new IntegerType()。setValue(2);'是可能的,這將是原始問題中的編譯錯誤。 –

+0

也許我理解錯的問題,但我想他想擺脫IntegerType和FloatType類。在一個類中將它們組合在一起:NumberType –

1

理想情況下,setValue方法(數字值)不能容許輸入任何值,但是浮在FloatingPointType和setValue方法(數字值)不能容許輸入任何值但在IntegerType中爲int。您可以使用類Number中的intValue()和floatValue()方法進行檢查,並在輸入了不合適的值時拋出異常。 Number class methods

這將是的setValue IntegerType

if(value.intValue()!= value) throw new IllegalArgumentException()