java-based - the abstract class and Template Method design pattern

An abstract class and create an anonymous subclass object

abstracttest Package; 

/ * 
 * anonymous subclasses of the abstract class 
 * 
 * * / 

abstract  class the Person { 
    String name; 
    int Age; 
    
    public the Person (String name, int Age) { 
        Super (); 
        the this .name = name;
         the this .age = Age; 
    } 
    public the person () { 
        Super (); 
    } 
    
    public  abstract  void EAT (); 
    
    public  void walk () { 
        . the System OUT .println ( " man walking ");
    }
}
class Student extends Person{
    int score;
    
    
    public Student() {
        super();
    }
    public Student(String name, int age) {
        super(name, age);
    }
    public Student(String name, int age, int score) {
        super(name, age);
        this.score = score;
    }

    public void eat() {
        System.out.println("student eat");
    }
}

public  class AbstractTest {
     public  static  void main (String [] args) { 
        
        // the p-create an object of an anonymous subclass name of the object called p, within the object to override the abstract methods achieved 
        the Person = the p- new new the Person () { 
            
            @Override 
            public  void EAT () { 
                . the System OUT .println ( " . 1 " ); 
            } 
        }; 
        Method (P); 
        
    } 
    public  static  void Method (the Person P) { 
        p.eat (); 
        p.walk (); 
    } 
}

Template Method design pattern: template method is an abstract class, which is part of the template method first common written, and then declare a hook method, let the child class to implement, new sub-class objects when you call the template method, it will automatically call the hook method the

TemplateMethod Package Penalty for; 

Import java.util.ArrayList; 

/ * 
 * 
 * abstract class applications: Template Method design pattern of 
 * concepts: hook method, like a hook, the following abstract class which subclass objects hanging, hook that is called a subclass implementation method 
 * 
 * * / 

public  class TemplateTest {
     public  static  void main (String [] args) { 
        SubTemplate T = new new SubTemplate (); 
        
        t.spendTime (); 
    } 
    
    
} 

abstract  class Template { 
    
    // calculation takes for a piece of code execution time 
    public  void spendTime () { 
        
        Long Start = System.currentTimeMillis (); 
        
        the this.code ();     // indeterminate portion 
        
        Long End = System.currentTimeMillis (); 
    
        the System. OUT .println ( " take time " + (END- Start)); 
    } 
    
    public  abstract  void code (); 
    
} 

class SubTemplate Template {the extends 
    
    @Override 
    public  void code () { 
        the ArrayList <Integer> = the arrayList new new the ArrayList <Integer> ();
         for ( int I = 2 ; I <= 100000 ; I ++ )
            arrayList.add(i);
        System.out.println(arrayList.size());
            
    }
    
}

Guess you like

Origin www.cnblogs.com/zsben991126/p/12148118.html