継承について

グラフィック・オブジェクト
(タートル・グラフィクス)
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 進め
}
}
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.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 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);
}
}
継承について
継承
public class ForeignStudent extends Student {
public String country;
public void setCountry(String c) {
country = c;
}
}
• Student は次のスライドの定義を用いる。
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) {
name = n;
id = nextid;
nextid++;
}
public Student() {}
}
サブクラスのインスタンス
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);
}
}
型と継承
class StudentTest {
public static void main(String[] args) {
Student ss = new ForeignStudent();
ss.setName(“Potter”);
System.out.println(ss.name);
}
}
データ構造の事例
二分木
グラフ
二分木
value
left
right
0
-1
3
2
5
public class BinTree {
int value;
BinTree left, right;
void insert(int x) {
if (x == value)
return;
if (x < value)
boolean find(int x) {
if (left == null) {
if (x == value)
left = new BinTree();
return true;
if (x < value)
left.value = x;
if (left == null)
} else left.insert(x);
return false;
else
else return left.find(x);
if (right == null) {
else
right = new BinTree();
if (right == null)
return false;
right.value = x;
else return right.find(x); } else right.insert(x);
}
}
public static void main(String[] args) {
BinTree b = new BinTree();
b.value = 0;
b.insert(-1);
System.out.println(b.find(3));
b.insert(3);
b.insert(5);
b.insert(2);
System.out.println(b.find(3));
}
}
演習
• BinTreeに対してdeleteの操作を考えよ。
最短経路(再び)
• 有向グラフのstart点から与えられたgoal点までの最
短の経路を求める。ここでは、各辺の長さは1とする。
• 点の数はn。各点は0~n-1の番号で参照。
for (int i = 0; i < n; i++) step[i] = -1;
step[start] = 0;
for (int s = 0; step[goal] == -1; s++)
for (step[i]==sを満たす各iについて)
for (iと隣接する各kについて)
if (step[k] == -1) {
step[k] = s+1;
prev[k] = i;
}
public class Node() {
int value;
Node next;
public Node(int x, Node n) {
value = x; next = n;
}
}
public class Shortest {
static int[][] edges = ...;
static int n = edges.length;
static int[] step = new int[n];
static int[] prev = new int[n];
static Node q0, q1;
static void shortest(int start, int goal) {
for (int i = 0; i < n; i++) step[i] = -1;
step[start] = 0; q0 = new Node(start, null);
for (int s = 0; step[goal] == -1; s++) {
while (q0 != null) {
int i = q0.value; int[] e = edges[i];
for (int j = 0; j < e.length; j++) {
int k = e[j];
if (step[k] == -1) {
step[k] = s+1;
q1 = new Node(k, q1);
prev[k] = i;
}
}
q0 = q0.next;
}
q0 = q1;
}
}
public static void main(String[] args) {
shortest(0, 9);
for (int i = 9; i != 0; i = prev[i])
System.out.println(i);
}
}
3
4
0
2
5
1
7
9
6
8
static int[][] edges = {
{1,2,3},
{6},
{4,5,6},
{2},
{7},
{9},
{9},
{8,9},
{7,9},
{}};
3
4
s=0
0
2
5
1
7
9
6
8
3
4
s=1
0
2
5
1
7
9
6
8
3
4
s=2
0
2
5
1
7
9
6
8
3
4
s=3
0
2
5
1
7
9
6
8
配列の配列
• 配列の配列は次のように宣言・生成する。
int[][] a = new int[5][3];
• 個々のa[i]は配列である。
int[] b;
b = a[3];
• 従って、各要素はa[i][j]
と参照する。
配列の配列(続き)
• 個々のa[i]は空(null)でもよい。
int[][] a = new int[5][];
• 個々のa[i]には、
大きさの異なる配列も
設定できる。
a[1] = new int[3];
a[2] = new int[2];
配列の配列(最後)
• 配列は生成時に初期化できる。
int[][] a = {null, {1,2,3}, {4,5}, null, null};
1
2
4
5
3