情報構造論では、この後には、複雑なデータ構造を扱うために、Javaによるオブジェクト(あるいはC言語による構造体データ)を多用する。そこで、基本的なJavaのオブジェクトと、C言語の構造体についておさらいとする。
Javaでオブジェクト指向
オブジェクトを扱うプログラムの基本プログラムを示す。例えば、名前と年齢のデータをクラスで扱うのであれば、以下のようなコードが基本となるだろう。
import java.util.*;
class NameAge {
String name ; // インスタンス変数
int age ; // インスタンス変数
static int count = 0 ; // クラス変数
// コンストラクタ
NameAge( String s , int a ) {
this.name = s ;
this.age = a ;
count++ ;
}
// メソッド
void print() {
System.out.println( this.name + "," + this.age ) ;
System.out.println( "member = " + count ) ;
}
} ;
public class Main {
public static void main(String[] args) throws Exception {
NameAge tsaitoh = new NameAge( "tsaitoh" , 59 ) ;
tsaitoh.print() ;
System.out.println( "age = " + tsaitoh.age ) ;
NameAge tomoko = new NameAge( "tomoko" , 48 ) ;
tomoko.print() ;
}
}
実行結果
tsaitoh,59
member = 1
age = 59
tomoko,48
member = 2
- オブジェクト指向の基礎(Paiza.io)
クラスとは、データ構造(オブジェクト)とそのデータ構造を扱うための関数(メソッド)をまとめて扱う。
クラス NameAge の中で宣言されている、NameAge() の関数は、オブジェクトを初期化するための関数(メソッド)であり、特にコンストラクタと呼ばれる。
実際にデータを保存するための tsaitoh や tomoko とよばれる変数に NameAge オブジェクトの実体(インスタンス)を作る時には 「new クラス名」 とやることで、初期化ができる。
イメージでは、下図のようなデータ構造ができあがる。

でも、年齢の覚え方は、将来的に誕生日を覚えるように変化するかもしれない。この際に、Main 関数の中で age を使うと後で混乱の元になるかもしれない。こういう時は、NameAge クラス以外では中身を勝手に使わせないために、インスタンス変数などに public / private といったアクセス制限を加える。
import java.util.*;
class NameAge {
private String name ; // インスタンス変数
private int age ; // インスタンス変数
public static int count = 0 ; // クラス変数
// コンストラクタ
public NameAge( String s , int a ) {
this.name = s ;
this.age = a ;
count++ ;
}
// メソッド
public void print() {
System.out.println( this.name + "," + this.age ) ;
System.out.println( "member = " + count ) ;
}
} ;
public class Main {
public static void main(String[] args) throws Exception {
NameAge tsaitoh = new NameAge( "tsaitoh" , 59 ) ;
tsaitoh.print() ;
System.out.println( "age = " + tsaitoh.age ) ;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ここがエラーになる。NameAge::age は private
NameAge tomoko = new NameAge( "tomoko" , 48 ) ;
tomoko.print() ;
}
}
クラス自体も、public class NameAge … のように宣言することもあるが、public なクラスは 1つ の *.java ファイルの中に1つしか書けないというルールがあるので要注意。
C言語で構造体
JavaのプログラムをC言語に直した例を示す。
#include <stdio.h>
#include <string.h>
struct NameAge {
char name[ 20 ] ;
int age ;
} ;
void setNameAge( struct NameAge* this , char s[] , int a ) {
strcpy( this->name , s ) ;
this->age = a ;
}
void printNameAge( struct NameAge* this ) {
printf( "%s , %d\n" , this->name , this->age ) ;
}
int main() {
struct NameAge saitoh ;
struct NameAge tomoko ;
setNameAge( &saitoh , "tsaitoh" , 59 ) ;
setNameAge( &tomoko , "tomoko" , 48 ) ;
printNameAge( &saitoh ) ;
printNameAge( &tomoko ) ;
return 0 ;
}
Java では、プリミティブ型以外のデータはすべてオブジェクトであり、データはヒープメモリ上にある。そのデータの場所を指し示すポインタが多用される。
C言語では、データはポインタ型などを使わなければ、単純にメモリ上に保存される。

練習問題
科目(Subject)と学生(Student)の情報があり、科目を受講した成績(Result)で成績を管理している。
このデータを管理するためのクラスを宣言し、下に示すようなデータSubject,Result,Studentの配列を作り、下に示したような出力が得られるプログラムを作成せよ。
科目: Subject
id name teacher // Subject[] subject_table = {
10010 Math t-saitoh // new Subject( 10010 , "Math" , "t-saitoh" ) ,
10020 English takaku // new Subject( ....
10030 Science komatsu // } ;
成績: Result
s_id id point // Result[] result_table = {
58563 10020 83 // new Result( 58563 , 10020 , 83 ) ,
58564 10010 95 // new Result( ...
58573 10030 64 // } ;
58563 10010 89
学生: Student
s_id name age // Student[] student_table = {
58563 tsaitoh 18 // new Student( 58563 , "tsaitoh" , 18 ) ,
58564 tomoko 19 // new Student( ...
58573 mitsuki 18 // } ;
以下のようなデータが出力されること
tsaitoh 18 English takaku 83
tomoko 19 Math t-saitoh 95
mitsuki 18 Science komatsu 64
tsaitoh 18 Math t-saitoh 89
