aquaballoon

Java + Android

Mar 14th, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import android.widget.Button;
  5. import android.widget.Toast;
  6.  
  7. public class MainActivity extends Activity {
  8.    
  9.     @Override
  10.     protected void onCreate(Bundle savedInstanceState) {
  11.         //Extends
  12.         super.onCreate(savedInstanceState);                                            
  13.         setContentView(R.layout.activity_main);
  14.        
  15.         Button bt = (Button) findViewById(R.id.button1);
  16.         //Anonymous Class / Inner Class
  17.         bt.setOnClickListener(new View.OnClickListener() {     
  18.             //Callback Function / Override
  19.             public void onClick(View v) {  
  20.                 //Method Chaining
  21.                 Toast.makeText(getBaseContext(), "Clicked", Toast.LENGTH_LONG).show();  
  22.             }
  23.         });
  24.     }
  25. }
  26.  
  27. Extends
  28. public class Init {
  29.     public static void main(String[] args) {
  30.         new Son();
  31.     }
  32. }
  33.  
  34. class Son extends Father {
  35.     Son(){
  36.         eat();
  37.     }
  38. }
  39.  
  40. class Father {
  41.     public void eat(){
  42.         System.out.println("Orange");
  43.     }
  44. }
  45.  
  46. Override
  47. public class Init {
  48.     public static void main(String[] args) {
  49.  
  50.         new Son();
  51.     }
  52. }
  53.  
  54. class Son implements Father {
  55.     Son(){
  56.        
  57.     }
  58.    
  59.     @Override
  60.     public void eat(){
  61.         System.out.println("Apple");
  62.     }
  63. }
  64.  
  65. interface Father {
  66.     public void eat();
  67. }
  68.  
  69. Callback Function
  70. public class Init {
  71.     public static void main(String[] args) {
  72.  
  73.         Son cup = new Son();
  74.         cup.drink(new Father(){     //Anonymous Class
  75.             void eat(){         //Callback Function
  76.                 System.out.println("Apple");
  77.             }
  78.         });
  79.     }
  80. }
  81.  
  82. class Son {
  83.    
  84.     void drink(Father cup){
  85.         cup.eat();
  86.     }
  87. }
  88.  
  89. class Father {
  90.    
  91.     void eat(){
  92.     }
  93. }
  94. Anonymous Class
  95. public class Init {
  96.     public static void main(String[] args) {
  97.  
  98.         new Son();    //Anonymous class
  99.     }
  100. }
  101.  
  102. class Son {
  103.     Son(){
  104.         //drink("Coffee", new Father());
  105.         drink("Coffee", new Father(){        //Anonymous class
  106.             void eat(){
  107.                 System.out.println("Orange");
  108.             }
  109.         });
  110.     }
  111.    
  112.     void drink(String cup1, Father cup2){
  113.         System.out.println(cup2.Father(cup1));
  114.         cup2.eat();
  115.     }
  116. }
  117.  
  118. class Father {
  119.     String Father(String cup){
  120.         return cup;
  121.     }
  122.    
  123.     void eat(){
  124.         System.out.println("Banana");
  125.     }
  126. }
  127.  
  128.  
  129. Inner Class
  130. public class Init {
  131.     public static void main(String[] args) {
  132.  
  133.         new Son();
  134.         new Son.Sson().eat();  // inner class
  135.     }
  136. }
  137.  
  138. class Son {
  139.     Son(){
  140.         System.out.println("Banana");
  141.     }
  142.     static class Sson{
  143.         void eat(){
  144.             System.out.println("Apple");
  145.         }
  146.     }
  147. }
  148.  
  149.  
  150. Method Chaining
  151. public class Init {
  152.     public static void main(String[] args) {
  153.  
  154.         new Son().drink().eat().desert();  //Method Chaining
  155.     }
  156. }
  157.  
  158. class Son {
  159.     Son(){
  160.     }
  161.    
  162.     Son drink(){
  163.         System.out.println("Milk");
  164.         return this;
  165.     }
  166.    
  167.     Son eat(){
  168.         System.out.println("Beef");
  169.         return this;
  170.     }
  171.    
  172.     void desert(){
  173.         System.out.println("Cake");
  174.     }  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment