如何为所有方法创建一次对象?

时间:2016-12-25 06:15:51

标签: php oop

我有这样的代码结构:

public class SubMenu extends AppCompatActivity{
    // Declare Variables
    JSONObject jsonobject;
    JSONArray jsonarray;
    ListView listview;
    ListViewAdapter adapter;
    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;
    static String RANK = "id";
    static String COUNTRY = "name";

    static String FLAG = "image";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_sub_menu);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);




        getSupportActionBar().setDisplayHomeAsUpEnabled(true);



        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        // Get the view from listview_main.xml

        // Execute DownloadJSON AsyncTask
        new DownloadJSON().execute();
    }

    // DownloadJSON AsyncTask
    private class DownloadJSON extends AsyncTask<Void, Void, Void> {

        // @Override
        //  protected void onPreExecute() {
        //  super.onPreExecute();
        // Create a progressdialog
        //   mProgressDialog = new ProgressDialog(SubMenu.this);
        // Set progressdialog title
        //   mProgressDialog.setTitle("Categories of Main categories.....");
        // Set progressdialog message
        //  mProgressDialog.setMessage("Loading...");
        //  mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        //  mProgressDialog.show();
        // }

        @Override
        protected Void doInBackground(Void... params) {
            // Create an array
            arraylist = new ArrayList<HashMap<String, String>>();
            // Retrieve JSON Objects from the given URL address
            jsonarray = JsonFunctions
                    .getJSONfromURL("http://cloud.granddubai.com/broccoli/menu_typeitem.php");

            try {
                // Locate the array name in JSON
//                    jsonarray = jsonobject.getJSONArray("main_menu_items");


                for (int i = 0; i < jsonarray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();

                    jsonobject = jsonarray.getJSONObject(i);
                    // Retrive JSON Objects
                   // map.put("id", jsonobject.getString("id"));
                    map.put("name", jsonobject.getString("name"));

                    map.put("image", jsonobject.getString("image"));
                    // Set the JSON Objects into the array
                    arraylist.add(map);
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.list1);
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(SubMenu.this, arraylist);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);
            // Close the progressdialog
            // mProgressDialog.dismiss();
        }
    }
}

实际上,我有12种方法需要在其中大部分时间内创建class myclass{ use App/classes/Log public function myfunc1 () { $log_obj = new Log; $log_obj->log('something1'); } public function myfunc2 () { $log_obj = new Log; $log_obj->log('something2'); } public function myfunc3 () { $log_obj = new Log; $log_obj->log('something3'); } } 类的对象。现在我想知道,有没有更好的方法来做(制作一个对象)一次?例如,使用Log属性并将对象设置为它或其他..

1 个答案:

答案 0 :(得分:1)

您可以使用__construct将Log实例分配给myclass的属性。以下是访问另一个类中的类方法的示例:

class Test {
    public $var = 'test';

    public function show_var() {
        echo $this->var;
    }
}

class Test_2 {
    protected $test;

    public function __construct() {
        $this->test = new Test;
    }

    public function show_test() {
        $this->test->show_var();
    }

}

$test_2 = new Test_2;

$test_2->show_test();

请参阅here实际操作。