我有一种类是GeneralProduct,它看起来如下:
public class GeneralProduct()
{
String label;
Object obj;
public GeneralProduct(String label, Object obj)
{
this.label = label;
this.obj = obj;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我有两个不同的类,ProductA
和ProductB
.这两个类都有一个叫做的通用方法getPrice()
.另一方面,我有一个名为auxList
:
ArrayList<GeneralProduct> auxList = new ArrayList<GeneralProduct>();
auxList.add(new GeneralProduct(new ProductA(), "ProductA"));
auxList.add(new GeneralProduct(new ProductB(), "ProductB"));
Run Code Online (Sandbox Code Playgroud)
现在的问题是我无法访问getPrice()
类ProductA
和ProductB
来自auxList
.我怎么能管理这个?我应该使用这样的东西吗?如果是这样,我怎样才能继承孩子们的方法getPrice()?
public class ProductA extends GeneralProduct
Run Code Online (Sandbox Code Playgroud) 我知道类似的东西已经被问过了,但是我遇到一些麻烦来得到一个来自键盘的十进制数字.
我在onCreate方法中的Java代码应该是:
textS0 = (EditText)findViewById(R.id.editS0);
Button btn_S0 = (Button)findViewById(R.id.getS0);
btn_S0.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
//how should I get here the number from keyboard?
//I think I should be something like
//double S0=textS0.getText()....
}
});
Run Code Online (Sandbox Code Playgroud)
这就是我的XML文件所包含的内容
<EditText
android:id="@+id/editS0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/S0"
android:inputType="numberDecimal" />
<Button
android:id="@+id/getS0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/setS0" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)