Call out objects in sequence </ font>
Check with the following class structure
class | Explanation |
---|---|
abstract chain.class |
Define chain function The field next stores the next object instance set():Set the following objects doChain()Define |
chai1.class~chain3.class | Implement chain |
user(Main.class) | Operation check |
abstract_class_chain
abstract class chain{
String name = this.getClass().getName();
chain next; //Store the following objects
chain set(chain next){
this.next=next;
return this.next; //Returns the next object
}
abstract void doChain();
void print(){
System.out.println(name);}
}
chain1.class
class chain1 extends chain{
void doChain(){
print();
if(next != null){next.doChain();} //DoChain for the next object()Run
}
}
chain2.class
class chain2 extends chain{
void doChain(){
print();
if(next != null){next.doChain();}
}
}
chain3.class
class chain3 extends chain{
void doChain(){
print();
if(next != null){next.doChain();}
}
}
user(Main.class)
public static void main(String[] args){
chain1 ch1 = new chain1();
chain2 ch2 = new chain2();
chain3 ch3 = new chain3();
ch1.set(ch2).set(ch3);
ch1.doChain();
}
Recommended Posts