型 メソッド名(型 変数名,…) {
文;
…
return 文;
}
メソッドを記述する場合は、必ずクラスの中に記述しなければいけません(プログラミング規則)。| Sample6_3_1.java |
|---|
// 社員クラス
class Employee {
// 表示用の文字列を返す
String getFormatView(String name, String affiliationName) {
String view = "氏名は" + name + "です\n" + "所属先は" + affiliationName + "です";
return view;
}
}
public class Sample6_3_1 {
public static void main(String[] args) {
Employee employee = new Employee(); // クラスの宣言及びインスタンス化
// 社員情報を出力する
String view = employee.getFormatView("サミー", "営業部"); // 表示用に変換して取得する
System.out.println(view);
}
}
|
氏名はサミーです 所属先は営業部です |