Separate the side that implements the method and the side that uses the method, and associate both with an instance of the implementation class
-The implementation side defines and embodies the method
- The user owns the implementation side instance in the field and uses the implementation side method via the instance </ font>
Check with the following class structure
class | Explanation | |
---|---|---|
Mounting side | abstract class superSam |
Define method |
Mounting side | sam.class | Implemented superSam |
User side | useSam.class | Use the method on the implementation side |
Create a class below
superSam.class
abstract class superSam{
String str;
superSam(String str){this.str=str;}
abstract void todo();
}
sam.class
class sam extends superSam{
sam(String str){super(str);}
void todo(){System.out.println(super.str);}
}
useSam.class
class useSam{
sam sam;
useSam(sam sam){this.sam=sam;}
void exec(){sam.todo();}
}
user(Main.class)
public static void main(String[] args){
useSam use1 = new useSam (new sam("Hello java"));
use1.exec();
}}
Recommended Posts