Java learning day21- abstract class (abstract class) and template design mode (TemplateMethod)

An abstract class (abstract class)

1. With the definition of a new inheritance hierarchy subclass, class become more and more concrete, the parent class is more general, and more versatile. Class should be designed to ensure that the parent and child classes to share features. Sometimes a parent needs to be designed so abstract that it does not have a specific example, such a class is called an abstract class.

For example, Animal class is no way to describe the specific way of moving, only the Animal class and all its methods are abstract treatment, that is, do not write the specific implementation. Wait until the succession to a specific animal, then the method body concrete realization.

2. When using the abstract keyword to modify a class that is called an abstract class.

3. Similarly, when used to modify a abstract method is called abstract methods. (Abstract method: The only way of declaration, there is no implementation of the ";" End example: abstract int abstractMethod (int a);.)

4. An abstract class can not be instantiated. An abstract class is used to be inherited, subclasses of the abstract class must override the abstract parent class method, and a method thereof. If not override all abstract methods, it is still an abstract class that as long as there is an abstract class method, then this class must be an abstract class.

The private method, constructor, static methods, final properties by modifying the method can not be abstract,.

package day15;

public  abstract  class Animal {
     public  abstract  void Test (); // long as there is a class abstract method, then this class is an abstract class must
    
    public abstract void move();
}

class Dog extends Animal{
    public void test() {
        // TODO Auto-generated method stub
        
    }
    
    public void move() {
        System.out.println ( "moves the dog is running" );
        
    }        
}

class Fish extends Animal{
    public void test() {
        // TODO Auto-generated method stub
        
    }
    
    public void move() {
        System.out.println ( "moves the fish swim" );
        
    }
}

abstract  class Bird the extends Animal { // abstract class can inherit an abstract class 
    public  void Move () {
         // the TODO Auto-Generated Method Stub
        
    }
    
    public abstract void test();
}
package day15;

public class Test {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.move();
        
        Fish f = new Fish();
        f.move();
    }
}

Print results:

Moves the dog is running
fish swim moves

 

Questions: 1. Why abstract classes can not use the keyword final statement?

A: final modified class is final class can not be inherited. An abstract class can not be instantiated, are made to be inherited, and subclasses of the abstract class must override the abstract parent class method, and a method thereof.

Question: 2 You can define an abstract class constructor do?

A: An abstract class can have a constructor, but can not create an instance of an abstract class it directly.

 

Exercise: Write an Employee class, declared as an abstract class that contains the following three properties: name, id, salay. To provide the necessary structure and abstract methods: work (). For the Manager class, he not only employees, as well as bonuses (bonus) properties. Please use the idea of ​​inheritance, classes and design CommonEmployee Manager class, the class required to provide the necessary property access methods.

package day15;

public  abstract  class the Employee {
     public the Employee () { // display Builder
        
    }
    
    int id;
    String name;
    double salary;
    
    public abstract void work();
}


class CommonEmployee extends Employee{
    
    public void setCommonEmployeeInFo(int id,String name,double salary){
        super.id = id;
        super.name = name;
        super.salary = salary;
    }
    
    public void getCommonEmployeeInFo(){
        System.out.println(super.id);
        System.out.println(super.name);
        System.out.println(super.salary);
    }
    
    public  void Work () {
         // TODO Auto-Generated Stub Method, 
        System.out.println ( "This is an ordinary employee" );
    }
}


class Manager extends Employee{
    double bonus;
    
    public  void setManagerInFo ( int ID, String name, Double the salary, Double Bonus) {
         Super .id = ID;
         Super .name = name;
         Super a .salary = the salary;
         the this .bonus = Bonus; // this is the only part of the leading properties Therefore with the this 
    }
    
    public void getManagerInFo(){
        System.out.println(super.id);
        System.out.println(super.name);
        System.out.println(super.salary);
        System.out.println(this.bonus);
    }
    
    public  void Work () {
         // TODO Auto-Generated Stub Method, 
        System.out.println ( "This is a leadership" );
    }
}
package day15;

public class Test {
    public static void main(String[] args) {
        CommonEmployee ce = new CommonEmployee();
        ce.work();
        ce.setCommonEmployeeInFo(1007, "小明", 6000);
        ce.getCommonEmployeeInFo ();
        
        Manager m = new Manager();
        m.work();
        m.setManagerInFo ( 2371, "white", 10000, 6000 );
        m.getManagerInFo ();
    }
}

Print results:

 

Second, the Template Method design pattern (TemplateMethod)

Abstract class embodies a kind of template pattern design, abstract class as a universal template multiple sub-classes, subclasses to extend, on the basis of the transformation of the abstract class, subclass but will retain overall behavior pattern abstract class.

An abstract class is like an outline, which the abstract method is that each chapter title; subclasses based on the title of each chapter to refine these out.

 

solved problem:

1. As part of the implementation is to determine the internal function, part of the implementation is uncertain. Then you can go to the uncertainty is partially exposed, so that subclasses to achieve.

2. Write an abstract superclass, parent class provides a common method for multiple subclasses, and the one or more methods left to its subclasses to implement, is a template pattern.

 

Example:

Package Penalty for day15; 
/ **
* Template design pattern
* /
public abstract class Templete { public abstract void code(); public final void the getTime () { // used only to create a final subclasses, but the method does not allow a subclass overrides the Long Start = System.currentTimeMillis (); // returns the number of seconds of the current code (); Long = End System.currentTimeMillis (); System.out.println ( "Time code execution method:" + (End - Start)); } } class TestTmp extends Templete{ public void code() { // TODO Auto-generated method stub int k = 0; for(int i = 0; i < 500000; i++){ k += 1; } System.out.println (K); // K shows the number of executed } }
package day15;

public class Test {
    public static void main(String[] args) {
        
        TestTmp t = new TestTmp();
        t.getTime();
    }
}

Print results:

500,000
time code method performed: 1

 

Guess you like

Origin www.cnblogs.com/su-peng/p/12525962.html