java 构造函数访问权限 问题

来源:百度知道 编辑:UC知道 时间:2024/06/01 04:31:27
下面的一个小练习,在构造函数MyFrame(String s, int x, int y, int width, int height) 中 写成public MyFrame(String s, int x, int y, int width, int height) 编译就会报错。为什么啊?
希望高手详细解释一下。谢谢!!

import java.awt.*;
import java.awt.event.*;

public class TestAnonymous {
public static void main(String[] args) {
new MyFrame("TestAnonymous",100,100,300,200);
}
}

class MyFrame extends Frame {
MyFrame(String s, int x, int y, int width, int height) {
super(s);
setLayout(null);
setBounds(x,y,width,height);
setBackground(Color.GREEN);

addWindowListener(new MyWindowMoniter());//内部类实现windowClosing()
/*
addWindowListener(new WindowAdapter(){ //匿名内部类实现windowClosing()
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
*/

setVisible(true);
}

class MyWindowMoniter extends WindowAdapter {
public void

。。我刚刚试过,没有问题啊

没有错误!

设置访问权限的问题,对于初学者来说可以暂时不深究。以后涉及到写项目时自然会慢慢理解。至于你说在构造方法前设置public访问权限会编译出错,这是不应该的。据本人所知,在这种toy程序里边,你随便怎么设置构造方法的访问权限也不会编译出错。所以,请你仔细检查后再行编译。