(day7)Template Design Mode in Abstract Class

1.An abstract,sa its name suggest,is something abstract.But we still can realize methods in it.
2.An abstract method in abstract class can't be realized when defined,we can only realize it in its subclass

What is a template

The defination rom Internet:
完成一件事情,有固定的数个步骤,但是每个步骤根据对象的不同,而实现细节不同;
就可以子啊父类中定义一个完成该事情的总方法。每个步骤的具体实现,由子类完成。

Abstract class

Can't instantiate
There are conventional methods,but an abstract method can only be implemented in its subclass.
When we need a template,we should create an abstract class,and realize its functions in the subclass. -- Start -> pause->stop->exit

Using abstract class template to realize the method of building a house:

public abstract class Drawings{
    public void buildHouse{
    String type = getHouseType();
    String color = getHouseColor();
    String material = getHousematerial();
    System.out.println("You will build a "+color+type" using "+material):
    }

    public abstract String getHousetype();
    public abstract String getHouseColor();
    publice abstract String getHouseMaterial();
}
public class Workers extends Drawings{
    @Override
    public abstract String getHousetype(){
        return "Villa"
}

    @Override
    public abstract String getHouseColor(){
        return "Colourful"
}
    @Override
    publice abstract String getHouseMaterial(){
      return "Ferroconcrete"
}
}
public class MyClass{
    Woker xw = new Woker();
    xw.buildHouse();
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容