简体   繁体   English

在主要方法的类中扩展Frame类

[英]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. 今天,当我阅读讲义时,我不明白在这种情况下extends的目的是什么。 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? 为什么main方法类扩展了Frame类? Why isn't Frame created inside the main method or as an instance variable of class QueenApl ? 为什么没有在main方法内部或作为QueenApl类的实例变量创建Frame Like this: 像这样:

     Frame someobj = new Frame(argument or none); 
  2. Why can getToolkit() be used directly without appending the classname. 为什么可以直接使用getToolkit()而不附加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? 是因为此方法在Frame类中并且由于主方法类继承了Frame ,我们可以直接使用该方法?

extends is keyword which represents inheritence. extends是代表继承的关键字。

Why the main method class is extends to Frame ? 为什么主方法类扩展到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) ? 为什么不在主方法内部或作为QueenApl类的实例变量创建Frame而不是某些东西= new Frame(argument或none)?

the difference is when you extend Frame class you acquire its methods in your class. 不同之处在于,当您extend Frame class时,会在您的类中获取其方法。 ie, you can override the methods declared in the frame class in QueenApl class. 即,您可以覆盖QueenApl类的frame类中声明的方法。

for better understanding here's a in-famous Animal example: 为了更好地理解,下面是一个著名的Animal示例:

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. 请注意,您已经重写了Dog类中的eat方法,以实现特定于dog的eat操作。

2.)By the way why getToolkit() can be used directly without append the classname. 2)通过为什么可以直接使用getToolkit()而不附加类名的方式。 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 ? 是因为此方法在Frame类中并且由于主方法类继承了Frame,所以我们可以直接使用该方法?

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. 好吧,因为您的getToolKit()方法是从Frame clas继承的,因此您无需使用实例进行调用。

You don't really have to extend Frame, it';s just convenient. 您实际上不必扩展Frame,这很方便。 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. 像setSize()和getToolkit()这样的调用方法现在需要在Frame字段前加上前缀,因为不再是Frame。 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. 它继承了JFrame,因此可以覆盖paint方法,从而将图像“卡片”绘制到屏幕上。 Without overriding the paint method, there would be no way of accessing the graphics object to draw to the JFrame. 如果不重写paint方法,将无法访问图形对象以绘制到JFrame。

The paint method comes with a java.awt.Graphics parameter, which will allow you to draw to the JFrame. paint方法带有java.awt.Graphics参数,该参数允许您绘制到JFrame。 Overriding the "paint" method is the only means by which you can acquire a Graphics class to draw to a JFrame. 覆盖“ paint”方法是获取Graphics类以绘制到JFrame的唯一方法。

"getToolkit" is inherited from JFrame, directly into your "QueenApl" class. “ getToolkit”是从JFrame继承的,直接继承到您的“ QueenApl”类中。 Because of this, "getToolkit" is now technically a member of the "QueenApl" class. 因此,“ getToolkit”从技术上讲现在是“ QueenApl”类的成员。 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 . 如果你真的不明白我的意思,当我说“继承”或“扩展”,看看这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当类扩展Java中的另一个类时,在main方法中传递参数 - Pass argument in main method when class extends another class in Java 调用从主方法扩展另一个类的类 - Call a class that extends another class from main method Java:如何在主类中调用方法,而该方法在另一个类中,该类扩展了抽象类 - Java: How to call upon a method in a main class where the method is in another class which extends an abstract class A类的主菜单扩展了Applet - A main menu for class A extends Applet 由扩展第一类的类调用时,无法使一个类中的方法完全在main方法中运行 - Unable to have the methods in one class completely run in main method when called by a class that extends first class 扩展了一个类,该类扩展了Hadoop的Mapper - Extends a class that extends the Hadoop's Mapper 扩展JFrame的Frame类中提供的方法不能在main()中调用 - Methods present in Frame class, which extends JFrame, can't be invoked in main() 如果一个方法属于另一个扩展Thread的类但从主线程调用,则该方法是由主线程还是子线程执行? (JAVA) - If a method belongs to another class that extends Thread but is called from the main thread, will it be executed by main or a child thread? (Java) main()方法的Java类的包约定 - package convention for main() method`s Java class java类A扩展了类B和方法覆盖 - java class A extends class B, and method override
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM