Presentation is loading. Please wait.

Presentation is loading. Please wait.

復習ー I (General Review I) クラスとオブジェクトの概念 Concepts of class and object クラスの宣言とオブジェクトの生成 Definition of a class and creation of an object コンストラクタとメソッドのオーバーロー.

Similar presentations


Presentation on theme: "復習ー I (General Review I) クラスとオブジェクトの概念 Concepts of class and object クラスの宣言とオブジェクトの生成 Definition of a class and creation of an object コンストラクタとメソッドのオーバーロー."— Presentation transcript:

1 復習ー I (General Review I) クラスとオブジェクトの概念 Concepts of class and object クラスの宣言とオブジェクトの生成 Definition of a class and creation of an object コンストラクタとメソッドのオーバーロー Constructor and method overloading インスタンスとクラスの変数とメソッド Instance and static/class variable & method スーパークラスとサブクラス Super-class and sub-class 継承とメソッドオーバーライド Inheritance and method overriding アクセス制御修飾子 Access control: default, public, private, protect 抽象クラスとインタフェース abstract class and interface クラスとオブジェクトの概念 Concepts of class and object クラスの宣言とオブジェクトの生成 Definition of a class and creation of an object コンストラクタとメソッドのオーバーロー Constructor and method overloading インスタンスとクラスの変数とメソッド Instance and static/class variable & method スーパークラスとサブクラス Super-class and sub-class 継承とメソッドオーバーライド Inheritance and method overriding アクセス制御修飾子 Access control: default, public, private, protect 抽象クラスとインタフェース abstract class and interface

2 クラス( class )とオブジェクト (object)/ インスタンス ( instance ) インスタンス(オブジェクト)は具体的な特定のもの 同じクラスのインスタンス(オブジェクト)は共通の 性質を持つ class object instance StringProfessor Hello Java Ohmori, Kunii Rectangle

3 class クラス名 { 変数 (variable) の宣言 メソッド (method) の宣言 } class Rectangle {// 長方形 int width;// 変数 width (幅)の宣言 int height;// 変数 height (高さ)の宣言 void setSize( int w, int h) {// setSize メソッドの宣言 width = w;// width (幅)の設定 height = h; // height (高さ)の設定 } int getArea() {// getArea メソッドの宣言 return width * height;// 面積を計算して返す } 値を返さ ない int 型の値 を返す 引数 arguments クラス宣言 – Definition of a class

4 インスタンス ( オブジェクト ) の作り方 (create objects) Rectangle r = new Rectangle(); r.width = 123; // r.setSize(123, 45); でもよい r.height = 45; r r.width r.height 123 45 void setSize(int w, int h) { this.width = w; this.height = h; } this はインスタンス自 身 r.setSize(123, 45) の呼 び出しの中では this と r は同じ メソッドの呼 出し 実引数 0 0 object reference new 引数なし

5 コンストラク (constructor) – create objects class Rectangle {.... Rectangle( ) { setSize(10, 20); } Rectangle(int w, int h) { width = w;// setSize(w, h); でも height = h; }.... } Rectangle r1 = new Rectangle(); Rectangle r2 = new Rectangle(12, 56); 10 20 12 56 r1 r2 メソッドとの違い ・コンストラクタの名前 はクラス名と同じ ・戻り値の型がない コンストラクタの多重定義 ( overload ) 名前は同じで引数の 型や個数が違う インスタンスを生 成し 変数の初期化を行 う

6 クラス変数( class variable / static variable ) class Rectangle {.... static int counter = 0; int number; Rectangle( ) { width=10; height=20; counter++; number = counter; } Rectangle r1 = new Rectangle(); Rectangle r2 = new Rectangle(); Rectangle r3 = new Rectangle(); 10 20 r3 r2.number 3 10 20 r2 r2.number 2 10 20 r1 r1.number 1 Rectangle Rectangle.counter 3 インスタンス変数はインスタンスご と クラス変数はクラスに1つ、 can’t be accessed via object クラス変 数 インスタン ス 変数 r1.counter (x) r2.counter (x) r3.counter (x)

7 クラスメソッド ( class/static method ) class Rectangle {.... static int counter = 0; int number;.... static int getCounter( ) { return counter;} int getArea() { return width*height; }.... } Rectangle r1 = new Rectangle(); int i = Rectangle.getCounter(); int j = r1.getArea(); 10 20 r1 number 1 Rectangle counter 3 インスタンスメソッドはインスタンスごと クラスメソッドはクラスに1つ(インスタンス変数に直接アクセ スは不可) クラス メソッド クラス変 数 getCounter getArea インスタン ス メソッド r1.counter (X) r1.getCounter() (x) Rectangle.counter Rectangle.getCounter()

8 class Rectangle { int width; int height; …… int getArea( ) { return width * height; } static int getArea(Rectangle r ) { return r.width * r.height; } } Rectangle r1 = new Rectangle(); int a1 = r1.getArea(); インスタン ス メソッド Rectangle r2 = new Rectangle(); int a2 = Rectangle.getArea(r2); クラス メソッド インスタンスメソッドとクラスメソッド instance method and class/static method

9 final (定数) class Rectangle { final int INITIAL_WIDTH = 10; final int INITIAL_HEIGHT = 20; int width; int height; Rectangle( ) { width = INITIAL_WIDTH; height = INITIAL_HEIGHT; }.... } INITIAL_WIDTH = 10; //defined INITIAL_WIDTH = 100; // エラー INITIAL_WIDTH = 10; //defined INITIAL_WIDTH = 100; // エラー 一般に final と宣言されたものの内容 を変更することはできない。 ・ final 変数 (constant) final と宣言された変数 に代入はできない。 すなわち、定数である。 ・ final メソッド final と宣言されたメソッド をサブクラスで オーバーライドできない。 ・ final クラス final と宣言されたクラス のサブクラスを作ること はできない。

10 スーパークラスとサブクラス (super class & subclass) class Rectangle {.... } class NamedRectangle extends Rectangle {.... } Rectangle NamedRectangle スーパークラス サブクラス classA classBclassD classCclassEclassF クラス階層 class hierarchy

11 継承( inheritance ) class Rectangle { int width; int height; void setSize(int w, int h) {.... int getArea( ) {.... } class NamedRectangle extends Rectangle {.... } サブクラスは スーパークラス の変数やメソッド を継承する スーパークラス の持つ変数や メソッドを サブクラスも 持っている NamedRectangle nr = new NamedRectangle(); nr.setSize(123, 45); System.out.println(“width=“ + nr.width);

12 スーパークラスとサブクラスのコンストラクタ Constructors in super class and subclass class Rectangle { …… Rectangle( ) { setSize(10, 20); } Rectangle(int w, int h) { setSize(w, h); } } class NamedRectangle extends Rectangle { String name; NamedRectangle( ) { name = “NO NAME”; } NamedRectangle(String s, int w, int h ) { super(w, h); name = s; } コンストラクタは 継承されない スーパークラスの コンストラクタの 呼出しには super(...); を使う スーパークラスのコンストラクタの呼出 し 自動的に super(); が挿入され る 自分のクラスの コンストラクタの 呼出しには this(...); を使う

13 継承( is-a 関係)と合成( has-a 関係) class NamedRectangle extends Rectangle { String name; // width and height are inherited from Rectangle class NamedRectangle() { super(); name=“NO NAME”; }.….. } class NamedRectangle { String name; Rectangle r; // has a rectangle //object as a variable NamedRectangle() { r = new Rectangle(); name= “NO NAME”; }.... } (すでに存在する)クラスの利用 法 ・継承( is-a 関係) そのクラスのサブクラスを作る サブクラスはスーパークラスの 性質を継承する サブクラスはスーパークラスの 一種である ・合成( has-a 関係) そのクラスのインスタンスを持つ クラスを作る

14 メソッドのオーバーライド(上書き定 義) class Rectangle {.... void setSize(int w, int h) {.... }// (1) void setSize() {.... } // (2) } class NamedRectangle extends Rectangle {.... void setSize(int w, int h) {.... }// (3) } class Rectangle {.... void setSize(int w, int h) {.... }// (1) void setSize() {.... } // (2) } class NamedRectangle extends Rectangle {.... void setSize(int w, int h) {.... }// (3) } オーバーライド ( override ) 継承したメソッド を変更する (名前も引数の 型と個数も同じ) 例: (1) を (3) で オーバーロード ( overload ) 同じ名前で引数 の型や個数が 違う。例: (1) と (2) Rectangle r = new Rectangle(); NamedRectangle nr = new NamedRectangle(); r.setSize(123, 45);// (1) nr.setSize(123, 45);// (3) r = nr; // Rectangle クラスとして見る。 r.name はエラー r.setSize(123, 45); //(3) Rectangle r = new Rectangle(); NamedRectangle nr = new NamedRectangle(); r.setSize(123, 45);// (1) nr.setSize(123, 45);// (3) r = nr; // Rectangle クラスとして見る。 r.name はエラー r.setSize(123, 45); //(3) 多態性(多相性) polymorphism 多様な型の値をとる。多様な型として見る 実行時に呼ばれるメソッドはインスタンスで 決る

15 private (アクセス制御 /access control :情報隠 蔽) private と宣言されているものは 他のクラスから(サブクラスからも) 直接は使えない ( アクセスできない ) class Rectangle { private int width; private int height; void setSize(int w, int h) { width = w; height = h; } int getWidth() { return width; } int getHeight() { return height; } } class NamedRectangle extends Rectangle {.... int i = width; // コンパイルエラー int j = getHeight(); // ok } class Rectangle { private int width; private int height; void setSize(int w, int h) { width = w; height = h; } int getWidth() { return width; } int getHeight() { return height; } } class NamedRectangle extends Rectangle {.... int i = width; // コンパイルエラー int j = getHeight(); // ok } protected: サブクラス及び 同じ package 内のクラス 以外からはアクセスできない 宣言がなければ protected public: どのクラス からも アクセス できる アクセス メソッド (アクセ ス 用の メソッ ド)

16 抽象クラス ( abstract class ) abstract class Shape{ public double x, y; public Shape(double x, double y) { this.x = x; this.y = y;} abstract String getName(); } abstract class Shape{ public double x, y; public Shape(double x, double y) { this.x = x; this.y = y;} abstract String getName(); } 抽象クラス宣言 のキーワード 抽象メソッド宣 言のキーワード Public class Rectangle extends Shape{ double w, h; Rectangle(double x, double y, double w, double h { super(x,y); this.w=w;this.h=h; } String getName(){return “Rectangle”;} } Public class Rectangle extends Shape{ double w, h; Rectangle(double x, double y, double w, double h { super(x,y); this.w=w;this.h=h; } String getName(){return “Rectangle”;} } 抽象メソッドの再定義 class Test1{ public static void main(String args[]){ Shape s1 = new Rectangle(2, 5, 10, 20); sysetm.out.println(“s1=“+s1.getName()); } class Test1{ public static void main(String args[]){ Shape s1 = new Rectangle(2, 5, 10, 20); sysetm.out.println(“s1=“+s1.getName()); } メソッドの呼び出し 実行時に呼ばれるメ ソッドはインスタンス で決る Output: s1=Rectangle

17 インタフェース ( interface) interface Calculation{ double PI = 3.14; double area(); } interface Calculation{ double PI = 3.14; double area(); } インタフェーす 宣言のキーワー ド 抽象メソッド宣 言のキーワード を省略する 抽象メソッ ドの再定義 出力: This object r1 is a Rectangle, area = 200.0 インタフェー スの使い方 サブクラス のコンスト ラクタ メソッド の呼び出 し final ( 定数 ) キーワード を省略する public abstract class Shape{ public double x, y; public Shape(double x, double y) { this.x = x; this.y = y;} public abstract String getName(); } public class Rectangle extends Shape implements Calculation{ public double w, h; public Rectangle(double x, double y, double w, double h){ super(x, y); this.w = w; this.h = h; } public String getName(){ return “ Rectangle ” ; } public double area(){ return w*h; } } public class Test2{ public static void main(String args[]){ Rectangle r1 = new Rectangle(100, 100, 10, 20); Shape s1 = r1; Calculation c1 = r1; System.out.println( “ This object r1 is a ” + s1.getName() + “, area = ” + c1.area()); } インタ フェースの 実装

18 課題 (Exercise) 1. 次のクラスの宣言を完成しなさい。 2. 次のサブクラスの宣言を完成しなさい。 class Rectangle{ private int width; private int height; private int number; static int counter=0; Rectangle(){ ??? } Rectangle(int w, int h){ ??? } void setSize(int w, int h){ ??? } int getWidth() { ??? } int getHeight() {??? } int getArea() { ??? } public String toString(){ ??? // number,width, height, area } } class Rectangle{ private int width; private int height; private int number; static int counter=0; Rectangle(){ ??? } Rectangle(int w, int h){ ??? } void setSize(int w, int h){ ??? } int getWidth() { ??? } int getHeight() {??? } int getArea() { ??? } public String toString(){ ??? // number,width, height, area } } class NamedRectangle extends Rectangle{ String name; NamedRectangle(){ ??? // no name } NamedRectangle(String s, int w, int h){ ??? } void scaleSize(int s){ ??? // 拡大又は縮小 s 倍 } public String toString(){ ??? //name, number, width, height, area を出力 } class NamedRectangle extends Rectangle{ String name; NamedRectangle(){ ??? // no name } NamedRectangle(String s, int w, int h){ ??? } void scaleSize(int s){ ??? // 拡大又は縮小 s 倍 } public String toString(){ ??? //name, number, width, height, area を出力 } public class RectangleCreationApplication { ??? // main メソッド ??? // Rectangle() コンストラクタ 使って、 Rectangle のオブジェクトを生成します ??? // Rectangle(int w, int h) コンストラクタ 使って、 Rectangle のオブジェクトを生成します ??? // NameRectangle() コンストラクタ 使って、 NamedRectangle のオブジェクトを生成します ??? // NamedRectangle オブジェクトを二倍で拡大します ??? // NamedRectangle(String s, int w, int h) コンストラクタ 使って、 Rectangle のオブジェクトを生成し ます ??? // 生成したオブジェクトを出力しなさい ??? // static 変数 counter を使って、生成したオブジェクトの数を出力しなさい } public class RectangleCreationApplication { ??? // main メソッド ??? // Rectangle() コンストラクタ 使って、 Rectangle のオブジェクトを生成します ??? // Rectangle(int w, int h) コンストラクタ 使って、 Rectangle のオブジェクトを生成します ??? // NameRectangle() コンストラクタ 使って、 NamedRectangle のオブジェクトを生成します ??? // NamedRectangle オブジェクトを二倍で拡大します ??? // NamedRectangle(String s, int w, int h) コンストラクタ 使って、 Rectangle のオブジェクトを生成し ます ??? // 生成したオブジェクトを出力しなさい ??? // static 変数 counter を使って、生成したオブジェクトの数を出力しなさい } 3. Rectangle と NamedRectangle のいくつかのオブジェクトの生成と生成したオブジェ クトの出力の Java application プログラムを作成しなさい。


Download ppt "復習ー I (General Review I) クラスとオブジェクトの概念 Concepts of class and object クラスの宣言とオブジェクトの生成 Definition of a class and creation of an object コンストラクタとメソッドのオーバーロー."

Similar presentations


Ads by Google