简体   繁体   中英

Extends Frame class in main method's class

Today when I was reading my lecture notes, I don't understand what the purpose of extends is in this context. Consider this code:

import java.net.*;    
import java.awt.*;

public class QueenApl extends Frame{ // Why extends??
  Image card;

public static void main(String[] args){
    QueenApl f = new QueenApl();
    f.show();    
}

public QueenApl(){
    setSize(400,400);
    try{
      URL u = new URL("file:/c:/windows/desktop/carn4.jpg");
      card=getToolkit().getImage(u); //Why getToolkit can be used directly?
    }catch(MalformedURLException e){
     System.out.println("Error in URL!"); System.exit(1);
    }  
}

public void paint(Graphics g){
      g.drawImage(card,120,50,this);
    }   
}
  1. Why does the main method class extend the Frame class? Why isn't Frame created inside the main method or as an instance variable of class QueenApl ? Like this:

     Frame someobj = new Frame(argument or none); 
  2. Why can getToolkit() be used directly without appending the classname. in front of it? Is it because this method is in the class Frame and since the main method class inherits Frame , we can use the method directly?

extends is keyword which represents inheritence.

Why the main method class is extends to Frame ? Why don't Frame being created inside the main method or as the instance variable of class QueenApl Frame something = new Frame(argument or none) ?

the difference is when you extend Frame class you acquire its methods in your class. ie, you can override the methods declared in the frame class in QueenApl class.

for better understanding here's a in-famous Animal example:

class Animal {
public void eat(){
//animal eat
}
class Dog extends Animal {
@override 
public void eat(){
//dog specific eat
}
}
}

Notice that you have overridden the eat method in Dog class to implement dog specific eat operations.

2.)By the way why getToolkit() can be used directly without append the classname. in front of it ? Is it because this method is in the class Frame and since the main method class inherit Frame , we can use the method directly ?

well that's because your getToolKit() method is inherited from your Frame clas s, thus you don't need to call it with an instance.

You don't really have to extend Frame, it';s just convenient. Compare your code to this code which does not extend Frame. Instead, you need to have a Frame field.

calling methods like setSize() and getToolkit() now need to be prefixed by your Frame field, because this isn't a Frame any more. Your code does not need them because this is a frame, methods of non-private ancestor classes can be called by your class.

import java.net.*;    
import java.awt.*;

public class QueenApl { 
  Image card;
  Frame frame;

  public static void main(String[] args){
      QueenApl f = new QueenApl();
  }

  public QueenApl(){
    frame = new Frame();
    frame.setSize(400,400);
    try{
      URL u = new URL("file:/c:/windows/desktop/carn4.jpg");
      card=frame.getToolkit().getImage(u);
    } catch(MalformedURLException e){
      System.out.println("Error in URL!"); System.exit(1);
    } 
    frame.show();
  }

  public void paint(Graphics g){
    g.drawImage(card,120,50,frame);
  }   
}

It inherits JFrame so it can override the paint method, allowing the image "card" to be drawn to the screen. Without overriding the paint method, there would be no way of accessing the graphics object to draw to the JFrame.

The paint method comes with a java.awt.Graphics parameter, which will allow you to draw to the JFrame. Overriding the "paint" method is the only means by which you can acquire a Graphics class to draw to a JFrame.

"getToolkit" is inherited from JFrame, directly into your "QueenApl" class. Because of this, "getToolkit" is now technically a member of the "QueenApl" class. Therefore no member/class name must be appended before the method call.

If you don't really understand what I mean when I say "inherits" or "extends", look at this .

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