オブジェクト指向




コンストラクタ

引数なし

public コンストラクタ(){
}

引数あり

public コンストラクタ(引数){
}

補足

並べることでオーバーロード可能



デストラクタ

~コンストラクタ(){
}



ステップアップ

try、catch、finally、using

例)ファイルの読み込み
+ 第1段階……tryなし
  1. String str;
  2.  
  3. StreamReader FILE = new StreamReader(ファイルパス);
  4. while((str = FILE.ReadLine()) != null){
  5. System.Console.WriteLine(str);//表示
  6. }
  7.  

+ 第2段階……try、catch、finallyあり
  1. String str;
  2.  
  3. try{
  4. StreamReader FILE = new StreamReader(ファイルパス);
  5. while((str = FILE.ReadLine()) != null){
  6. System.Console.WriteLine(str);//表示
  7. }
  8. }
  9. catch(System.IO.FileNotFoundException ex ){
  10. System.Console.WriteLine(ex.Message);
  11. System.Console.Read();//エンターで終了
  12. return -1;
  13. }
  14. finally{
  15. //後処理
  16. if (FILE != null)
  17. {
  18. FILE.Close();
  19. }
  20. }
  21.  

+ 第3段階……try、catch、usingあり
  1. String str;
  2.  
  3. try{
  4. using(StreamReader FILE = new StreamReader(ファイルパス)){
  5. while((str = FILE.ReadLine()) != null){
  6. System.Console.WriteLine(str);//表示
  7. }
  8. }
  9. }
  10. catch(System.IO.FileNotFoundException ex ){
  11. System.Console.WriteLine(ex.Message);
  12. System.Console.Read();//エンターで終了
  13. return -1;
  14. }
  15.  

クラス





参考

最終更新:2011年04月22日 18:15