Skip to content

Instantly share code, notes, and snippets.

@shigemk2
Last active December 27, 2015 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shigemk2/7395111 to your computer and use it in GitHub Desktop.
Save shigemk2/7395111 to your computer and use it in GitHub Desktop.
やさしいJava 13
[submodule "7397050"]
path = 7397050
url = git@github.com:7397050.git
[submodule "pb"]
path = pb
url = git@github.com:7408778.git
[submodule "pc"]
path = pc
url = git@github.com:7409187.git
// ファイル分割
// 車クラス
class Car
{
private int num;
private double gas;
public Car()
{
num = 0;
gas = 0.0;
System.out.println("車を作成しました");
}
public void setCar(int n, double g)
{
num = n;
gas = g;
System.out.println("ナンバーを"+num+"ガソリン量を"+gas+"にしました");
}
public void show()
{
System.out.println("車のナンバーは"+this.num+"です");
System.out.println("ガソリン量は"+this.gas+"です");
}
}
  • ファイルを分割してコンパイルすることが出来る
  • クラスをパッケージに含めるにはpackage文を使う
  • クラスを異なるパッケージから利用できるようにするにはpublicを指定する
  • 異なるパッケージのクラスを利用するには、「パッケージ名.クラス名」と記述する
  • 異なるパッケージのクラスをimport文を使ってインポートすることが出来る
  • クラスライブラリのクラスは、パッケージ内に分類される
  • 同じパッケージのクラスをすべてインポートするには、import文で*を指定する
class Sample1
{
public static void main(String[] args)
{
Car car1 = new Car();
car1.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment