import java.util.*;
public class MainFile{
public static void main(String[] args){
ArrayList<TypeExpences> listOfTypes = new ArrayList<TypeExpences>();
Water waterExp = new Water(0, "fafasd", 10, "cubic meters", 2, 1);
listOfTypes.add(waterExp);
System.out.println((listOfTypes.get(0)).getFixed());
}
}
public class Water extends UseExpences{
int pricePer2;
public Water(int kod, String descr, int fixed, String unit, int
pricePer, int pricePer2){
super(kod, descr, fixed, unit, pricePer);
this.pricePer2 = pricePer2;
}
public int getFixed(){
return super.getFixedPrice(fixedPrice);
}
}
public class UseExpences extends TypeExpences{
protected int fixedPrice;
protected String unitOfMeasurement;
protected int pricePerMeasurementUnit;
public UseExpences(int kod, String descr, int fixed, String unit, int
pricePer){
super(kod, descr);
fixedPrice = fixed;
unitOfMeasurement = unit;
pricePerMeasurementUnit = pricePer;
}
public int getFixedPrice(int fixedPrice){
return fixedPrice;
}
}
public class TypeExpences {
protected int code;
protected String description;
public TypeExpences(int kod, String descr){
code = kod;
description = descr;
}
protected int getCode(int code){
return code;
}
protected String getDescription(String description){
return description;
}
}
Error:
错误:
C:\Users\laptopara\Desktop\ask>javac MainFile.java
MainFile.java:39: error: cannot find symbol
System.out.println((listOfTypes.get(0)).getFixed());
^
symbol: method getFixed()
location: class TypeExpences
1 error
If I do System.out.println(waterExp.getFixed()); it works.
如果我做System.out.println(waterExp.getFixed());有用。
How can I make it work with System.out.println((listOfTypes.get(0)).getFixed());?
如何使它与System.out.println一起使用((listOfTypes.get(0))。getFixed());?
1 个解决方案
#1
0
Ah yea I think your problem is, that you use polymorphic programming... You have a object ArrayList<TypeExpences> listOfTypes
, an ArrayList
which stores objects of class TypeExpences
. Thanks to polymorphic programming it's possible to store also objects of sub-classes of TypeExpences
like Water
.
啊啊,我认为你的问题是,你使用多态编程...你有一个对象ArrayList
Your problem is now, that you can't use methods which are declared in the sub-class Water
! You can only use methods which are already declared in your base-class TypeExpences
and I guess the method public int getFixed()
isn't declared.
你现在的问题是,你不能使用在子类Water中声明的方法!您只能使用已经在基类TypeExpences中声明的方法,我想方法public int getFixed()没有声明。
Change your code to this...
将您的代码更改为此...
ArrayList <Water> listOfTypes = new ArrayList<Water>();
...to fix this issue. Alternate you can implement the method public int getFixed()
in base-class TypeExpences
and override it in the sub-class.
...解决这个问题。或者,您可以在基类TypeExpences中实现方法public int getFixed()并在子类中覆盖它。
Maybe also take a look on this tutorial or something like that...
也许还看看这个教程或类似的东西......
EDIT
编辑
You can use an interface like this
你可以使用这样的界面
public interface AllMyObjects
{
public abstract int getFixed();
}
Now implement this interface in each class you use like this:
现在在您使用的每个类中实现此接口,如下所示:
public class Water implements AllMyObjects
{
...
@Override public int getFixed()
{
return super.getFixedPrice(fixedPrice);
}
}
...
public class Phone implements AllMyObjects
{
@Override public int getFixed()
{
...
}
}
After this change your array list like this:
在此更改后,您的数组列表如下所示:
ArrayList <AllMyObjects> listOfTypes = new ArrayList<>();
Now its should work if not, write a comment...
现在它应该工作,如果没有,写评论......
#1
0
Ah yea I think your problem is, that you use polymorphic programming... You have a object ArrayList<TypeExpences> listOfTypes
, an ArrayList
which stores objects of class TypeExpences
. Thanks to polymorphic programming it's possible to store also objects of sub-classes of TypeExpences
like Water
.
啊啊,我认为你的问题是,你使用多态编程...你有一个对象ArrayList
Your problem is now, that you can't use methods which are declared in the sub-class Water
! You can only use methods which are already declared in your base-class TypeExpences
and I guess the method public int getFixed()
isn't declared.
你现在的问题是,你不能使用在子类Water中声明的方法!您只能使用已经在基类TypeExpences中声明的方法,我想方法public int getFixed()没有声明。
Change your code to this...
将您的代码更改为此...
ArrayList <Water> listOfTypes = new ArrayList<Water>();
...to fix this issue. Alternate you can implement the method public int getFixed()
in base-class TypeExpences
and override it in the sub-class.
...解决这个问题。或者,您可以在基类TypeExpences中实现方法public int getFixed()并在子类中覆盖它。
Maybe also take a look on this tutorial or something like that...
也许还看看这个教程或类似的东西......
EDIT
编辑
You can use an interface like this
你可以使用这样的界面
public interface AllMyObjects
{
public abstract int getFixed();
}
Now implement this interface in each class you use like this:
现在在您使用的每个类中实现此接口,如下所示:
public class Water implements AllMyObjects
{
...
@Override public int getFixed()
{
return super.getFixedPrice(fixedPrice);
}
}
...
public class Phone implements AllMyObjects
{
@Override public int getFixed()
{
...
}
}
After this change your array list like this:
在此更改后,您的数组列表如下所示:
ArrayList <AllMyObjects> listOfTypes = new ArrayList<>();
Now its should work if not, write a comment...
现在它应该工作,如果没有,写评论......