今天和vandalor讨论到多态的思想和好处,自己对多态也有了进一步的认识。再来想了想前面一篇笔记中的代码,想到如果用户能够在不知道到底是applet还是窗体的情况能够跑起来的话,那应该更符合封装的思想了。所得代码如下:
public static String title(Object obj)
str = obj.getClass().toString();
if(str.indexOf("class")!=-1)
public static void run(JFrame frame,int width,int height)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle(title(frame));
frame.setSize(width,height);
public static void run(JApplet applet,int width,int height)
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle(title(applet));
frame.setSize(width,height);
frame.getContentPane().add(applet);
public static void run(JPanel panel,int width,int height)
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle(title(panel));
frame.setSize(width,height);
frame.getContentPane().add(panel);
public class Test extends JApplet
JLabel lb1 = new JLabel("Hello,World");
this.getContentPane().add(lb1);
public static void main(String[] args)
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2006/06/01/414397.html,如需转载请自行联系原作者