Java中的类实现是什么

Java中的类实现是什么

本文介绍了Java中的类实现是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次尝试使用java.awt.Graphics,但遇到了很多困惑。

I tried using java.awt.Graphics for the first time and came across so many confusions.

Graphics gp = new Graphics();

编译器抛出一个错误,指出无法实例化抽象类。
我查看了oracle java文档,然后尝试了以下操作:

and the compiler threw an error saying an abstract class cannot be instantiated.I had a look at the oracle java documentation and then tried the following:

import java.awt.Graphics;
public abstract class GUI extends Graphics{

public void painter(){
GUI gp = new GUI();
gp.drawString("Hello People!",2,10);
}
}

它仍然显示相同的错误:GUI是抽象的;无法实例化

Still it shows the same error: GUI is abstract;cannot be instantiated

我对抽象类和实现一无所知。但是,我设法在Oracle文档中获得了一些知识。但是我还是很困惑,请有人帮助我。非常感谢!!!!!!!

I do not have any idea about the abstract classes and implementation. However, I managed to get some knowledge in the Oracle's documentation. But still I'm confused, please someone help me. Thank you so much!!!!

我也尝试删除 GUI的抽象范围,但是编译器再次抛出另一个错误:GUI不能覆盖抽象方法 dispose();`java.awt.Graphics();

I also tried removing abstract scope of GUI, but the compiler again threw another error saying: GUI does not override the abstract methoddispose();`in java.awt.Graphics();

推荐答案

创建 JComponent 的子类,而不是 Graphics 的子类。然后,将 Graphics 的实例传递给 paintComponent 方法:

Create a subclass of JComponent instead of Graphics. An instance of Graphics is then passed in to the paintComponent method:

class GUI extends JComponent {
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.drawString("Hello People!", 2, 10);
    }
}

如果尝试创建<$ c $的实例c>图形或您自己的子类,那么它们将不是使用它的有用方法。

If you try creating an instance of Graphics or a subclass yourself, then they'll be no useful way to use it.

这篇关于Java中的类实现是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 03:33