关于java语言程序的问题 1、对于java应用程序来说,static静态变量和方法的含义。 import java.io.*; public class Iamxiao { static String st1="中国将收复台湾!"; /*此处必须定义为静态,否则系统会提示在main主程序中找不到st1变量。 定义为public也不行,这是为什么呀? */ public static void main(String args[]) { Iamxiao2 Iamxiao1; Iamxiao1=new Iamxiao2(); System.out.println("Hello the world!"+st1); } } 以下再写一个应用程序进行实验,此程序依旧运行时报错。 class _beidy { String st1="中国将收复台湾!"; System.out.println("Hello the world!"+st1); } public class _diaoyz { public static void main(String args[]) { _beidy _newobj=new _beidy(); } } 以下再写成小应用程序进行实验。 class _beidy { /*总算明白了,这正是java语言要求的封装性。 */ public void _beidyp() { String st1="中国将收复台湾!"; System.out.println("Hello the world!"+st1); } } public class _diaoyz { public static void main(String args[]) { _beidy _newobj=new _beidy(); _newobj._beidyp(); } } 以下再进行测试。 class _beidy { String st1="中国将收复台湾!"; //System.out.println("Hello the world!"+st1); } public class _diaoyz { public static void main(String args[]) { _beidy _newobj=new _beidy(); System.out.println("Hello the world!"+_newobj.st1); /*通过成员函数进行类的实例对象的调用。输出语句不能直接放 到一个类的定义下吗? */
|