1 / 24

プログラミング 第11回

プログラミング 第11回. クラスとインスタンス yukita@k.hosei.ac.jp. Rectangle クラス (List 11-1, p.8). class Rectangle{ int width; int height; void setSize(int w, int h){ width=w; height=h; } int getArea(){ return width*height; } }. setSize メソッド. getArea メソッド. setSize メソッド.

alec-dalton
Download Presentation

プログラミング 第11回

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. プログラミング 第11回 クラスとインスタンス yukita@k.hosei.ac.jp プログラミング第11回

  2. Rectangle クラス(List 11-1, p.8) class Rectangle{ int width; int height; void setSize(int w, int h){ width=w; height=h; } int getArea(){ return width*height; } } setSizeメソッド getAreaメソッド プログラミング第11回

  3. setSizeメソッド • void setSize(int w, int h) • メソッド定義のヘッダ • 戻り値なし(void),仮引数が w, h でいずれもint型。 プログラミング第11回

  4. getAreaメソッド • int getArea() • メソッド定義のヘッダ • 戻り値がint型,引数はとらない。 プログラミング第11回

  5. インスタンスの作り方 • new Rectangle()  とするだけでよい。 • インスタンスを生成して終わりということはあまりないから, • Rectangle r=new Rectangle() • とすると以後,変数rがこのインスタンスへのハンドル(参照)になる。 プログラミング第11回

  6. フィールドへのアクセス • r.width = 123; • r.height = 45; • これでもよいし, • r.setSize(123,45); • でもよい。 プログラミング第11回

  7. this はどのようなときに使うか void setSize(int width, int height){ this.width=width; this.height=height; } 仮引数とインスタンスのフィールド名を同じにすることで,意味を共有できる。thisがつくとインスタンスのフィールドを参照していることになる。 プログラミング第11回

  8. コンストラクタ class Rectangle{ int width;int height; Rectangle(int w, int h){ width=w; height=h; } void setSize(int w, int h){ width=w; height=h; } int getArea(){ return width*height; } } プログラミング第11回

  9. インスタンスの初期化 • Rectangle r=new Rectangle(123, 45); • は • Rectangle r=new Rectangle(); • r.width=123; • r.height=45; • あるいは • Rectangle r=new Rectangle(); • r.setSize(123,45); • と同等。 プログラミング第11回

  10. 引数なしのコンストラクタ class Rectangle{ int width; int height; Rectangle(){ setSize(10,20); } Rectangle(int w, int h){width=w; height=h;} void setSize(int w, int h){ width=w;height=h; } int getArea(){return width*height;} } プログラミング第11回

  11. mainメソッドを設けて自立するRectangle.java(List 11-7,p.20) class Rectangle{ int width; int height; Rectangle(){ setSize(10,20); } Rectangle(int w, int h){ width=w; height=h; } プログラミング第11回

  12. Rectangle.java (2) void setSize(int w, int h){ width=w; height=h; } int getArea(){ return width*height; } プログラミング第11回

  13. Rectangle.java (3) public static void main(String[] args){ Rectangle r1=new Rectangle(); System.out.println("r1.width="+r1.width); System.out.println("r1.height="+r1.height); System.out.println( "r1.getArea()="+r1.getArea()); プログラミング第11回

  14. Rectangle.java (4) Rectangle r2=new Rectangle(123,45); System.out.println("r2.width="+r2.width); System.out.println("r2.height="+r2.height); System.out.println( "r2.getArea()="+r2.getArea()); } } プログラミング第11回

  15. 実行結果 r1.width=10 r1.height=20 r1.getArea()=200 r2.width=123 r2.height=45 r2.getArea()=5535 プログラミング第11回

  16. インスタンスはどこに • Rectangle r1; • と変数が宣言されてもインスタンスは生成されない。 • r1=new Rectangle(); • のようにnew演算子が実行されて始めてインスタンスが生成され,ハンドル(参照,ポインタ)がr1に代入される。 • インスタンスはheap領域に作られ,プログラマがこの領域を直接アクセスすることはない。 • 変数はスタック領域に確保される。 プログラミング第11回

  17. クラスフィールドとは • クラスの全インスタンスに関する情報をクラスとして管理したいことがある。 • 例えば,インスタンスには生成順に通し番号を与えたいときなど。 • インスタンス変数だけでは,この目的は達成できない。 プログラミング第11回

  18. Rectangle.java(new version) class Rectangle{ static int counter=0; int number; int width; int height; static int getCounter(){ return counter; } プログラミング第11回

  19. Rectangle.java(2) Rectangle(){ setSize(10,20); number=counter; counter++; } Rectangle(int w, int h){ width=w; height=h; number=counter; counter++; } プログラミング第11回

  20. Rectangle.java(3) void setSize(int w, int h){ width=w; height=h; } int getArea(){ return width*height; } プログラミング第11回

  21. Rectangle.java (4) public static void main(String[] args){ Rectangle r1=new Rectangle(); System.out.println("r1.width="+r1.width); System.out.println( "r1.height="+r1.height); System.out.println( "r1.getArea()="+r1.getArea()); System.out.println( "r1.number="+r1.number); プログラミング第11回

  22. Rectangle.java (5) Rectangle r2=new Rectangle(123,45); System.out.println("r2.width="+r2.width); System.out.println("r2.height="+r2.height); System.out.println( "r2.getArea()="+r2.getArea()); System.out.println("r2.number="+r2.number); System.out.println( "Rectangle.counter="+Rectangle.counter); } } プログラミング第11回

  23. 実行結果 r1.width=10 r1.height=20 r1.getArea()=200 r1.number=0 r2.width=123 r2.height=45 r2.getArea()=5535 r2.number=1 Rectangle.counter=2 プログラミング第11回

  24. 課題 • 問題11-5を正しく直して,コンパイル,実行してみよ。 • 問題11-7 • 余裕があったら,問題11-6,8.9 • Rectangle.java(new version) を試してみよ。 プログラミング第11回

More Related