Java-- abstract: abstract

3.4 abstract: abstract

3.4.1 What is an abstract class?

With the definition of an inheritance hierarchy a new sub-class, the class becomes more and more concrete, while the parent class is more general, more universal. Class should be designed to ensure that the parent and child classes to share features. Sometimes designed to be a very abstract parent class, that it is not a specific example, such a class called abstract class.

 

 

3.4.2 abstract class basic grammar

① When using the abstract keyword to modify a class, the class is called abstract class; when used to modify a abstract method called abstract method.

② how to define abstract class?

Plus abstract before the class keyword. Properties, private methods, constructors, static methods, final methods can not be modified by abstract.

③ 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, is still an abstract class.

④ Although there is no way to instantiate an abstract class, but there are also abstract class constructor, the constructor is used to create the object subclasses.

⑤ abstract class can define abstract methods.

Syntax abstract methods: only declarations of methods, there is no way to achieve. Add modifier abstract keyword list method and the method should be abstract. ";" End, not with "{}." For example: public abstract void m1 ();

⑥ abstract approach is not necessarily an abstract class, but an abstract method must appear in an abstract class. Abstract class containing the method must be declared as an abstract class.

⑦ a non-abstract class that inherits the abstract class, an abstract method of the abstract class must be covered to achieve rewriting.

⑧ final abstract class that can not be modified, final abstract methods can not be modified.

3.4.3 Examples abstract class

abstract class A{  

       abstract void m1( );

       public void m2( ){

           System.out.println ( "m2 methods defined in the class A");

       }

}

class B extends A{

       void m1( ){

            System.out.println ( "m1 method defined in class B");

       }

}

public class Test{

       public static void main( String args[ ] ){

            A a = new B( );

            a.m1( );

            a.m2( );

       }

}

3.4.4 Thinking

① Why not use the final keyword abstract class declaration?

abstract class must be inherited, and the final class can not be inherited

② can define an abstract class constructor do?

// abstract not be used to modify the properties, constructors, private, final, static

public class TestAbstract1 {

      

}

abstract class A1{

       //abstract int name;

       // constructor can not be rewritten because

//     public abstract A1(){

//     }

       // subclass can not override (or rewritten) declared as the private.

       //private abstract void method1();

       // final modification of the method can not be overridden      

//     public abstract final void method2();

//     public abstract static void method3();

// static methods can be modified to adjust the class, but the abstract methods and no method body. . .

}

note:

1) When we design a class, do not need to create an instance of time, you can consider setting after the abstract, its subclass implementation of the abstract method of this class, the line instantiated.

2) The method of retaining only the abstract function method, and the specific implementation, to subclasses of the abstract class, an abstract method of this subclass override.

3) If the subclass inherits the abstract class, and rewrite all the abstract methods, then such is an "entity type", i.e. can be instantiated.

4) If the subclass inherits the abstract class that does not override all the abstract methods, means that this class is still abstract method, the class must be declared as abstract!

③ Template Method design pattern       

抽象类体现的就是一种模板模式的设计,抽象类作为多个子类的通用模板,子类在抽象类的基础上进行扩展、改造,但子类总体上会保留抽象类的行为方式。

解决的问题

当功能内部一部分实现是确定,一部分实现是不确定的。这时可以把不确定的部分暴露出去,让子类去实现。

编写一个抽象父类,父类提供了多个子类的通用方法,并把一个或多个方法留给其子类实现,就是一种模板模式。

public class TestTemplate {

       public static void main(String[] args) {

              new SubTemplate().spendTime();

       }

}

abstract class Template {

       public abstract void code();

       public void spendTime() {

              long start = System.currentTimeMillis();

              this.code();

              long end = System.currentTimeMillis();

              System.out.println("花费的时间为:" + (end - start));

       }

}

class SubTemplate extends Template {

       public void code() {

              boolean flag = false;

              for(int i = 2;i <= 10000;i++){

                     for(int j = 2;j <= Math.sqrt(i);j++){

                            if(i % j == 0){

                                   flag = true;

                                   break;

                            }

                     }

                     if(!flag){

                            System.out.println(i);

                     }

                     flag = false;

              }

       }

}

 

Guess you like

Origin www.cnblogs.com/superjishere/p/11829637.html