关于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); /*通过成员函数进行类的实例对象的调用。输出语句不能直接放 到一个类的定义下吗? */ } } 2、以下示例让我们了解类名的定义,须定义为$,_,字母开头的字符串,其长度没有限制, 也就是受限于你的操作系统文件名的长度。文件确实可以超长,已做实验。
import java.io.*; public class $Sta1 {
static String st1="我是肖大靳!"; //此处必须定义为静态。 public static void main(String args[]) { $Sta1 sta1_o; sta1_o=new $Sta1(); System.out.println("Hello the world!"+st1); } } /*文件超长的例子。类名绝对不能超长,windows98下为235个字符长。 以下所定义的类名为最长,多加一个2即会报错。 */ class _beidy1212121212121212121212121212122323434433245234524534523452452452452452452452452524524245245245245245245245424242452424242424524524245245245245245243524524524524352345243524245242424234524352432222222222222222222222222222222222222 { public void init() { 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); //_newobj.init(); _beidy1212121212121212121212121212122323434433245234524534523452452452452452452452452524524245245245245245245245424242452424242424524524245245245245245243524524524524352345243524245242424234524352432222222222222222222222222222222222222 _newobj=new _beidy1212121212121212121212121212122323434433245234524534523452452452452452452452452524524245245245245245245245424242452424242424524524245245245245245243524524524524352345243524245242424234524352432222222222222222222222222222222222222(); _newobj.init(); } } 3、以下示例可以很好的解释类的封装性,即通过类名进行访问其变量或调用方法。 class _beidy { public void init() { 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); //_newobj.init(); }
} 
|