Presentation is loading. Please wait.

Presentation is loading. Please wait.

オブジェクト指向入門.

Similar presentations


Presentation on theme: "オブジェクト指向入門."— Presentation transcript:

1 オブジェクト指向入門

2 インスタンス変数 public class Student { public String name; public int id; }

3 インスタンスの生成 class StudentTest { public static void main(String[] args) {
Student s = new Student(); s.name = “Hagiya”; s.id = 1; System.out.println(s.name); s = new Student(); s.name = “Turing”; s.id = 2; }

4 インスタンス・メソッド public class Student { public String name; public int id;
public void setName(String n) { this.name = n; // もしくは name = n; }

5 インスタンス・メソッドの呼び出し class StudentTest {
public static void main(String[] args) { Student s = new Student(); s.setName(“Hagiya”); System.out.println(s.name); }

6 スタティック変数(クラス変数) public class Student { public String name;
public int id; static int nextid = 1; public void setName(String n) { name = n; id = nextid; nextid++; }

7 インスタンス・メソッドの呼び出し class StudentTest {
public static void main(String[] args) { Student s = new Student(); s.setName(“Hagiya”); s = new Student(); s.setName(“Turing”); System.out.println(s.id); }

8 コンストラクタ public class Student { public String name; public int id;
static int nextid = 1; public Student(String n) { name = n; id = nextid; nextid++; }

9 コンストラクタの呼び出し class StudentTest {
public static void main(String[] args) { Student s = new Student(“Hagiya”); s = new Student(“Turing”); System.out.println(s.id); }

10 参照(reference) public class Cell { public int value; public Cell next;
public void print() { System.out.println(value); if (next != null) next.print(); }

11 リンク構造(リスト)の作成 class CellTest {
public static void main(String[] args) { Cell c = new Cell(); c.value = 1; c.next = new Cell(); c.next.value = 2; c.next.next = new Cell(); c.next.next.value = 3; c.next.next.next = null; // not necessary c.print(); }

12 c : value : 1 next : value : 2 next : value : 3 next : null

13 継承 public class ForeignStudent extends Student {
public String country; public void setCountry(String c) { country = c; } Student は次のスライドの定義を用いる。

14 public class Student { public String name; public int id; static int nextid = 1; public void setName(String n) { name = n; id = nextid; nextid++; } public Student(String n) { public Student() {}

15 サブクラスのインスタンス class StudentTest {
public static void main(String[] args) { ForeignStudent s = new ForeignStudent(); s.setName(“Potter”); s.setCountry(“USA”); System.out.println(s.name); System.out.println(s.country); }

16 型と継承 class StudentTest { public static void main(String[] args) {
Student ss = new ForeignStudent(); ss.setName(“Potter”); System.out.println(ss.name); }

17 演習 様々な方法でStudentオブジェクトを 生成してみよ。 Cellオブジェクトを用いて実際に リンク構造(リスト)を作ってみよ。

18 グラフィック・オブジェクト (タートル・グラフィクス)

19 public class T21 { public static void main(String[] args){ TurtleFrame f; //変数 f の型宣言 f = new TurtleFrame(); //TurtleFrameを作成し //fに代入 Turtle m = new Turtle(); //Turtle を作成し, //m の初期値として代入 Turtle m1 = new Turtle(); //もう1つ作成し, //m1 の初期値として代入 f.add(m); //f に m を追加 f.add(m1); //f に m1 を追加 m.fd(100); //m よ前に 100 進め m.rt(90); //m よ右に 90 度回れ m.fd(150); //m よ前に 150 進め m1.rt(90); //m1 よ右に 90 度回れ m1.fd(100); //m1 よ前に 150 進め }

20 public class T45 { public static void main(String[] args){ TurtleFrame f = new TurtleFrame(); Turtle m = new Turtle(); f.add(m); for(int j = 0; j < 8; j++){ for(int i = 0; i < 5; i++){ m.fd(50); m.lt(72); } m.rt(45);

21 public class House extends Turtle { //Turtle を拡張する
public House() { super(); } public House(int x, int y, int a) { super(x,y,a); public void polygon(int n, int s){ //polygonメソッドの定義 int a = 360/n; //曲がる角度を求めておく for(int j = 0; j < n; j++){ //n 回繰り返す fd(s); //s 前に進んで rt(a); //a 曲がるのを public void house(int s){ //house メソッドの定義 polygon(4,s); // polygon を利用 fd(s); rt(30); polygon(3,s); lt(30); bk(s); //元の場所に戻しておく

22 public class T61 { public static void main(String[] args){ TurtleFrame f = new TurtleFrame(); House m = new House(); int s = 50; f.add(m); m.house(s); m.up(); m.fd(s * 2); m.down(); m.polygon(3, s / 2); m.up(); m.fd(s); m.down(); m.polygon(10, s / 5); }


Download ppt "オブジェクト指向入門."

Similar presentations


Ads by Google