20170322
package com.example.wata.myapplication; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Cat cat =Cat.getInstance(); cat.run(); } } class Cat { static Cat instance = new Cat(); private Cat() { System.out.println("Cretae Instance"); } public static Cat getInstance() { return instance; } public void run(){ System.out.println("Run ! Run!"); } }
20170321
package com.example.wata.myapplication; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try { Class c =Class.forName("com.example.wata.myapplication.Cat"); Cat cat=(Cat)c.newInstance(); cat.run(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } class Cat { public void run () { System.out.println ("Run... Run...Run"); } }