简体   繁体   中英

How can I use abstract method in J2ME?

I implemented some code that has an alert class which is type of Canvas, and it has a keyPress method which must work different for each class that using alert class. For example the keyPress method has 2 buttons that the function of each button is different for each class! Could anyone tell me How can I implement that ?

public class myAlert extends Canvas implements CommandListener{

public myAlert(Midlet midlet,int width,int height,String str,String left_str,String right_str){ } protected void paint(Graphics g) { g.setColor(0, 0,0); g.drawRoundRect(5, 180, width-10, height, 8, 8); g.drawRoundRect(15, 185, width-15, height-5, 7, 7); g.setColor(0xbfd4f6); g.fillRoundRect(0, 180, width, height, 8, 8); /////////////// } protected void keyPressed(int keyCode) { switch(keyCode){ case -6: // select button
// break; case -7: //close button

        break;
}

} } Public class M(){
protected void keyPressed(int keyCode) {

    switch (state) {
 case STATE_INPUT_LEFT:
      // do work for focus on the left
      if (keyCode >= KEY_NUM0 && keyCode <= KEY_NUM9 && digitsEntered < 10) {
        digits_left[digitsEntered] = (char) ('0' + (keyCode - KEY_NUM0));
        digitsEntered++;
        repaint();
        if(digitsEntered>=8){
         state=STATE_INPUT_RIGHT;
            m= new myAlert(midlet,w, 150,"Ok " ,"exit",”system”);

//I want that if user click on Ok button the main page must be shown to user Display.getDisplay(midlet).setCurrent( m); break; } } } Public class N(){ public void commandAction(Command c, Displayable d) { String label = c.getLabel(); if(label.equals("Exit")){ midlet.exit(); } else if(label.equals("Enter")){ m= new myAlert(midlet,w, 150,”yes”,”No”,”Print” ); // I want here that if user clicks on ok another message be shown to user Display.getDisplay(midlet).setCurrent( m);

}
}

here is part of my code! as you see in M class and N class function of left button of Alert class is diffrent from each other! Now Could you help me?

In the parent, have the handler detect the specific button and delegate to two separate abstract methods. In the child, implement the methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM