public class Student public String name public int
インスタンス変数 public class Student { public String name; public int id; }
インスタンスの生成 class Student. Test { 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; } }
インスタンス・メソッド public class Student { public String name; public int id; public void set. Name(String n) { this. name = n; // もしくは name = n; } }
インスタンス・メソッドの呼び出し class Student. Test { public static void main(String[] args) { Student s = new Student(); s. set. Name(“Hagiya”); System. out. println(s. name); } }
スタティック変数(クラス変数) public class Student { public String name; public int id; static int nextid = 1; public void set. Name(String n) { name = n; id = nextid; nextid++; } }
インスタンス・メソッドの呼び出し class Student. Test { public static void main(String[] args) { Student s = new Student(); s. set. Name(“Hagiya”); s = new Student(); s. set. Name(“Turing”); System. out. println(s. id); } }
コンストラクタ public class Student { public String name; public int id; static int nextid = 1; public Student(String n) { name = n; id = nextid; nextid++; } }
コンストラクタの呼び出し class Student. Test { public static void main(String[] args) { Student s = new Student(“Hagiya”); s = new Student(“Turing”); System. out. println(s. id); } }
参照(reference) public class Cell { public int value; public Cell next; public void print() { System. out. println(value); if (next != null) next. print(); } }
リンク構造(リスト)の作成 class Cell. Test { public static void main(String[] args) { Cell c = new Cell(); c. value = 1; c. next = new Cell(); c. next. value = 2; c. next = new Cell(); c. next. value = 3; c. next = null; // not necessary c. print(); } }
c: value : 1 next : value : 2 next : value : 3 next : null
継承 public class Foreign. Student extends Student { public String country; public void set. Country(String c) { country = c; } } • Student は次のスライドの定義を用いる。
public class Student { public String name; public int id; static int nextid = 1; public void set. Name(String n) { name = n; id = nextid; nextid++; } public Student() {} }
サブクラスのインスタンス class Student. Test { public static void main(String[] args) { Foreign. Student s = new Foreign. Student(); s. set. Name(“Potter”); s. set. Country(“USA”); System. out. println(s. name); System. out. println(s. country); } }
型と継承 class Student. Test { public static void main(String[] args) { Student ss = new Foreign. Student(); ss. set. Name(“Potter”); System. out. println(ss. name); } }
public class T 21 { public static void main(String[] args){ Turtle. Frame f; //変数 f の型宣言 f = new Turtle. Frame(); //Turtle. Frameを作成し //fに代入 Turtle m = new Turtle(); //Turtle を作成し, //m の初期値として代入 Turtle m 1 = new Turtle(); //もう1つ作成し, //m 1 の初期値として代入 f. add(m); //f に m を追加 f. add(m 1); //f に m 1 を追加 m. fd(100); //m よ前に 100 進め m. rt(90); //m よ右に 90 度回れ m. fd(150); //m よ前に 150 進め m 1. rt(90); //m 1 よ右に 90 度回れ m 1. fd(100); //m 1 よ前に 150 進め } }
public class T 45 { public static void main(String[] args){ Turtle. Frame f = new Turtle. Frame(); 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. fd(50); m. rt(45); } } }
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); //元の場所に戻しておく } }
public class T 61 { public static void main(String[] args){ Turtle. Frame f = new Turtle. Frame(); 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); } }
- Slides: 22