我的java學習筆記version0.1

- 中國WEB開發者網絡 (http://www.webasp.net)
-- 技術教程 (http://www.webasp.net/article/)
--- 我的java學習筆記version0.1 (http://www.webasp.net/article/18/17464.htm)
-- 作者:未知
-- 發佈日期: 2005-04-13
 

關於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();
    }

}



webasp.net