如何从具有单例类的类对象中获取正确的值?

时间:2015-03-17 21:24:50

标签: java android eclipse

我正在开发一个管理Eclipse中船只列表的应用程序。我需要使用单例类来创建和保存船对象列表。我还必须为我的所有访问器创建一个常规类,即操作存储在类Boat的数据成员中的数据值的方法。我遇到的问题是当我为boat_list引用使用add方法时。它只接受位置和Boat对象,但是当我运行应用程序时,它只显示应用程序包的全名。它应该说我使用get方法存储的任何数据值。以下是我在MainActivity中的代码:

public class MainActivity extends Activity {

    public static TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Declare references and some variables
        BoatList boat_list;
        Boat boat1;
        String make;

        //Create a new Boat object
        boat1 = new Boat();

        //Input data value to some data members
        boat1.setMake("Sea Ray");
        boat1.setYear(2003);
        boat1.setRegi("D15F4");

        //Set the reference tv

        tv = (TextView) findViewById(R.id.text_main);

        //Access the singleton class, BoatList
        boat_list = BoatList.getInstance();

        //Put some strings into boat_list

        if (boat_list.isEmpty()) {
            //add item
            boat_list.add(boat_list.size(), boat1);
            boat_list.add(boat_list.size(), boat1);
            boat_list.add(boat_list.size(), boat1);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void onOption1(MenuItem i) {
        int x;
        BoatList boat_list;

        tv.setText("Show the Items in the List");

        //Get the list from BoatList
        boat_list = BoatList.getInstance();

        //Show each item in the list
        for (x = 0; x < boat_list.size(); x++)
            tv.append(boat_list.get(x) + ", ");
    }

    public void onOption2(MenuItem i) {
        tv.setText("Doing Option 2.");
    }

    public void onOption3(MenuItem i) {
        tv.setText("Doing Option 3.");
    }

    public void onOption4(MenuItem i) {
        tv.setText("Doing Option 4.");
    }

    public void onOption5(MenuItem i) {
        tv.setText("Doing Option 5.");
    }

    public void onOption6(MenuItem i) {
        tv.setText("Doing Option 6.");
    }

    public void onOption7(MenuItem i) {
        tv.setText("Doing Option 7.");
    }

    public void onOption8(MenuItem i) {
        tv.setText("...");
    }

    public void onOption9(MenuItem i) {
        tv.setText("...");
    }

    public void onOption10(MenuItem i) {
        tv.setText("...");
    }

    public void onOption11(MenuItem i) {
        tv.setText("...");
    }
}

这是我的单身人士课程的样子:

import java.util.LinkedList;

public final class BoatList extends LinkedList<Boat> {

    private static BoatList instance = null;

    private BoatList() {
        // Exists only to defeat additional instantiations.
    }

    public static BoatList getInstance() {
        if (instance == null)
            instance = new BoatList();

        return instance;
    }
}

而且,这就是我的班级船的样子:

public class Boat {

    //Declare data members
    private String make;
    private String registeration;
    private int year;
    private int length;
    private int beam;
    private String fuel;
    private double price;
    private String picURL;
    private int age;

    //Define the methods
    public String getMake() {
        return make;
    }

    public void setMake(String m) {
        make = m;
    }

    public String getRegi() {
        return registeration;
    }

    public void setRegi(String r) {
        registeration = r;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int y) {
        year = y;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int L) {
        length = L;
    }

    public int getBeam() {
        return beam;
    }

    public void setBeam(int b) {
        beam = b;
    }

    public String getFuel() {
        return fuel;
    }

    public void setFuel(String f) {
        fuel = f;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double p) {
        price = p;
    }

    public String getPicURL() {
        return picURL;
    }

    public void setPicURL(String url) {
        picURL = url;
    }

    public int getAge() {
        //Calculate the age of the boat by the current year plus
        //the year that the boat was manufactured 
        int currentYear = 2015;

        age = currentYear + year;

        return age;
    }

    public double getLuxuryTax() {
        //Declare variables
        double newPrice;
        double luxTax = .15;

        if (length >= 50 && age <= 10)
            newPrice = price * luxTax;
        else
            return price;

        return newPrice;
    }
}

1 个答案:

答案 0 :(得分:1)

单身人士不应该从任何事物延伸。如果你想让这个单例返回一个东西列表,那么给它一个列表作为成员并创建函数来访问它们。

public final class BoatList {

    private static BoatList instance = null;
    private List<Boat> list_of_boats = new ArrayList<Boat>();

    private BoatList()
    {
        // Exists only to defeat additional instantiations.
    }

    public static BoatList getInstance()
    {
        if(instance == null)
            instance = new BoatList();

        return instance;
    }

    public List<Boat> getListOfBoats()
    {
         return list_of_boats;
    }

    public void addBoat(Boat boat)
    {
        list_of_boats.add(boat);
    }
}

此外,您可能希望在打印到TextView时尝试调用Boat对象的toString()方法,如下所示:

// I added the (int) in the declaration cause it was missing
for(int x = 0; x < boat_list.size(); x++)
    tv.append(boat_list.get(x).toString() + ", ");

覆盖toString()方法还可以确保您正在打印所需的内容。