简体   繁体   English

功能未扩展到其他类别

[英]Functions not extending to other class

I've got a class full of getters and setters, so I can make a list of watches and have it accessible to the rest of my app: 我上了一堂课,里面满是getter和setter,所以我可以列出手表,让我的应用程序的其余部分可以访问它:

Watch.java: Watch.java:

 public class Watch { private String brand, mvmt, serial, urlLoc; private int wr, year; private double price; //getters and setters } 

I want to use this to get a list: 我想使用它来获取列表:

 package com.example.pat.watchproj; import java.util.LinkedList; public class WatchList extends LinkedList { //set up the watch list so it can be accessed through the app private static WatchList wlInst = new WatchList(); public static WatchList getInstance(){ if(wlInst == null){ wlInst = new WatchList(); } return wlInst; } private WatchList(){ } public static void setInstance(WatchList watchList){ wlInst = watchList; } } 

Here is an example of how I'd add a watch 这是我如何添加手表的示例

public void addWatch(View view){
    //initilize watch list and all edit texts
    //will need to parsedouble and parseinteger
    watchList = WatchList.getInstance();
    Watch usrWatch = new Watch();
    EditText addBrand = (EditText) findViewById(R.id.addBrand);
    EditText addSerial = (EditText) findViewById(R.id.addSerial);
    EditText addMvmt = (EditText) findViewById(R.id.addMvmt);
    EditText addWr = (EditText) findViewById(R.id.addWr);
    EditText addYear = (EditText) findViewById(R.id.addYear);
    EditText addPrice = (EditText) findViewById(R.id.addPrice);
    EditText addUrl = (EditText) findViewById(R.id.addUrl);

    //add to object now
    usrWatch.setBrand(addBrand.getText().toString());
    usrWatch.setMvmt(addMvmt.getText().toString());
    usrWatch.setSerial(addSerial.getText().toString());
    usrWatch.setWr(Integer.parseInt(addWr.getText().toString()));
    usrWatch.setPrice(Double.parseDouble(addPrice.getText().toString()));
    usrWatch.setYear(Integer.parseInt(addYear.getText().toString()));
    usrWatch.setUrlLoc(addUrl.getText().toString());

    watchList.add(usrWatch);
    WatchList.setInstance(watchList);

    Toast.makeText(addWatch.this, "Watch added", Toast.LENGTH_SHORT).show();
}

So, in mainactivity, when I write this: 因此,在mainactivity中,当我编写此代码时:

public void onOption8 (MenuItem i){
    //show most expensive
    WatchList watchList = WatchList.getInstance();
    int x = 0;
    for(int a = 0; a < watchList.size(); a++) {
        if (watchList.get(x).getPrice() <  watchList.get(a).getPrice()) {
            x = a;

        }
    }
}

So after that runs, I should be able to use watchList.get(x).getSerial() to get the serial of the most expensive watch. 因此,在运行之后,我应该能够使用watchList.get(x).getSerial()来获取最昂贵手表的序列号。 But android studio is telling me that it cant resolve method getPrice in onOption8, and I'm not sure where I'm going wrong. 但是android studio告诉我它无法解析onOption8中的getPrice方法,而且我不确定我要去哪里。 Let me know if I'm not being clear/can provide more info. 让我知道我是否不清楚/可以提供更多信息。 Thanks 谢谢

You haven't provided a type for your custom LinkedList implementation; 您尚未为自定义LinkedList实现提供类型; as such get(x) is returning an Object . 这样get(x)返回一个Object Change the WatchList class declaration to: WatchList类声明更改为:

public class WatchList extends LinkedList<Watch> {
    ...
}

This error probably occurs because you havn't given the WatchList class a type parameter yet. 由于您尚未给WatchList类提供类型参数,因此可能发生此错误。 In the onOption8, the compiler doesn't know whether the WatchList you received is type < String >, < Car >, or < Watch >. 在onOption8中,编译器不知道您收到的WatchList是<String>,<Car>还是<Watch>。

Make sure that you either just create a LinkedList of type Watch, like so 确保只创建Watch类型的LinkedList,就像这样

LinkedList<Watch> ll = new LinkedList<Watch>() 

or that you specify that WatchList is in fact a LinkedList of Watch objects, and not of anything else, in your WatchList class. 或者您指定WatchList实际上是WatchList类中的Watch对象的LinkedList,而不是其他任何对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM