Implementation of callbacks for Java programs

Source: Internet
Author: User

1. Callback (CallBack) or in C language called the hook function (hook) is also called: Template method mode
Code implementation:
Code 1
------------------------------------------------------------
Package edu.neuq.oop.callback;


Import Java.awt.Frame;




public class Paintframe {
public static void Drawframe (MyFrame f) {
SYSTEM.OUT.PRINTLN ("Boot thread");
SYSTEM.OUT.PRINTLN ("increased circulation");
SYSTEM.OUT.PRINTLN ("View message stack");

F.paint ();//Draw the window here is the hook function, through the concept of polymorphism can be implemented to draw a variety of sub-class window

System.out.println ("Start cache, increase efficiency");
}
public static void Main (string[] args) {
Drawframe (New GameFrame02 ());
}
}
Class GameFrame01 extends myframe{
public void paint () {
System.out.println ("Gameframe01.paint ()");
System.out.println ("Picture Window");
}
}
Class GameFrame02 extends myframe{
public void paint () {
System.out.println ("Gameframe02.paint ()");
System.out.println ("Picture Window");
}
}
Code 2
--------------------------------------------------------------------------
Package edu.neuq.oop.callback;


Import Java.awt.Graphics;


public class MyFrame {
public void paint () {
System.out.println ("Draw my Window");
}
}


---------------------------------------------------------------

Implementation of callbacks for Java programs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.