static変数
static変数の共有
コンストラクタ
staticイニシャライズ
オーバーロード
アクセス制限
継承
super
this
abstract
interface
感想
参考文献・HP
static変数
class Sum { int total; Sum() { // コンストラクタ(クラス名と同名) total = 0; // 0 クリア } void add(int x) { total += x; } } class SumSample { public static void main(String args[]) { Sum obj1 = new Sum(); // クラスSumのオブジェクトobj1生成 Sum obj2 = new Sum(); // クラスSumのオブジェクトobj2生成 obj1.add(10); obj1.add(20); obj2.add(30); obj2.add(40); System.out.println("obj1.total = " + obj1.total); System.out.println("obj2.total = " + obj2.total); } } ※実行結果 obj1.total = 30 obj2.total = 70 |
static変数の共有
class Sum { static int total; // 値を共有させる Sum() { // コンストラクタ(クラス名と同名) total = 0; // 0 クリア } void add(int x) { total += x; } } class SumSample { public static void main(String args[]) { Sum obj1 = new Sum(); // クラスSumのオブジェクトobj1生成 Sum obj2 = new Sum(); // クラスSumのオブジェクトobj2生成 obj1.add(10); obj1.add(20); obj2.add(30); obj2.add(40); System.out.println("obj1.total = " + obj1.total); System.out.println("obj2.total = " + obj2.total); } } ※実行結果 obj1.total = 100 obj2.total = 100 |
コンストラクタ
class Const { double x; int y; Const(double x, int y) { //コンストラクタ this.x = x; this.y = y; } Const(double x) { //コンストラクタのオーバーロード this.x = x; this.y = 15; } Const() { //コンストラクタのオーバーロード this.x = 10; this.y = 15; } double getMean() { //メソッド double z = this.x/this.y; return z; } } class TestConstructor { public static void main(String args[]) { double a,b; Const obj1; //インスタンス変数 Const obj2; obj1 = new Const(1059.0, 12); //インスタンスの呼び出し obj2 = new Const(1298.0); a = obj1.getMean(); //メソッドのよびだし b = obj2.getMean(); System.out.println("obj1: " + a); System.out.println("obj2: " + b); } } ※実行結果 obj1: 88.25 obj2: 86.53333333333333 |
●コンストラクタとは
コンストラクタは、クラスをインスタンス化してオブジェクトを作成するときに、必ず実行されています。 オブジェクトの初期化という重要な役割を果たします。コンストラクタは、クラス名と同じ名前を持ち、 戻り値を持たない特殊なメソッドです。引数の異なるコンストラクタを同時に定義することもできこれをオーバーロードと呼びます。 コンストラクタの働きとしては、オブジェクトの初期化です。staticイニシャライズ
class Disp { static { System.out.println("initialize"); } Disp() { System.out.println("construct"); } } class StaticSample { public static void main(String args[]) { Disp obj1 = new Disp(); Disp obj2 = new Disp(); } } ※実行結果 initialize construct construct |
オーバーロード
class Overload { double x; void mean(double a) { //メソッド1 x = a / 10; } void mean(double a, int b) { //メソッド2 x = a / b; } } class OverLoadTest { public static void main(String[] args) { Overload obj = new Overload(); //インスタンス化 System.out.println("メソッド1(引数が1)"); obj.mean(870.0); //メソッド1の引数 System.out.println("obj1=" + obj.x); System.out.println("メソッド2(引数が2)"); obj.mean(972.0, 11); //メソッド2の2つの引数 System.out.println("obj2=" + obj.x); } } ※実行結果 メソッド1(引数が1) obj1=87.0 メソッド2(引数が2) obj2=88.36363636363636 |
●オーバーロードとは
Java では、メソッドを識別するのに、メソッド名と引数リストの組を使います。 同じメソッド名でも、引数リストの異なるものは、別のものとして扱われます。 同じメソッド名で、引数リストの異なるメソッドを同時に定義することを、メソッドのオーバーロードと呼びます。アクセス制限
class acs { static int tanka; private float rate; // private変数rateの宣言 acs() { // コンストラクタ tanka = 1000; rate = 0.05f; } int keisan(int kazu) { int kingaku; kingaku = (int)(tanka * kazu * (1.0 + rate)); return kingaku; } } class PrvSample { public static void main(String args[]) { acs obj1 = new acs(); int kazu = 3; int kingaku = obj1.keisan(kazu); System.out.println("tanka = " + obj1.tanka); System.out.println("kazu = " + kazu); System.out.println("kingaku = " + kingaku); /*System.out.println("rate = " + obj1.rate);*/ /*private変数のためrateはこのクラスでの参照は不可*/ } } ※実行結果 tanka = 1000 kazu = 3 kingaku = 3150 |
●アクセス制御
大きくわけて次の三つの修飾詞があるpublic | ほかのクラスからアクセスできるコンストラクタ。(宣言しなければこれ) |
private | 同じクラスからしかアクセスできない。 |
protect | サブクラスまたは同じパッケージ内からしかアクセスできない。 |
継承
class succession { public static void main(String[] args) { Sub1 ko1Obj=new Sub1(); //インスタンス化 System.out.println("-------継承1-------"); ko1Obj.Sub1M(); ko1Obj.SuperM1(); // スーパークラスのメソッド Sub2 ko2Obj=new Sub2(); System.out.println("-------継承2-------"); ko2Obj.Sub2M(); ko2Obj.SuperM1(); // スーパークラスのメソッド } } class Super { void SuperM1() { System.out.println("スーパークラスメソッド1"); } } class Sub1 extends Super { void Sub1M() { System.out.println("サブクラス1メソッド"); } } class Sub2 extends Super { void Sub2M() { System.out.println("サブクラス2メソッド"); } void SuperM1() { System.out.println("SuperM1のオーバーライド"); } } ※実行結果 -------継承1------- サブクラス1メソッド スーパークラスメソッド1 -------継承2------- サブクラス2メソッド SuperM1のオーバーライド |
Superクラス | |継承 | | Sub1クラス← →Sub2クラス |
super
class Display { //スーパークラスDisplay void Disp() { System.out.println("Hello!"); } } class SuperSample extends Display { //サブクラス void Disp() { //メソッドのオーバーライド super.Disp(); //スーパークラスのDisplayを実行。 System.out.println("Hello! Java!!"); } public static void main(String args[]) { SuperSample obj = new SuperSample(); obj.Disp(); //サブクラスのDisplayを実行。 } } ※実行結果 Hello! Hello! Java!! |
this
class ThisD { int x; int y; int z; ThisD (int x) { this(x, 0, 1); } ThisD (int x, int y) { this(x, y, 1); } ThisD (int x, int y, int z) { this.x = x; this.y = y; this.z = z; } } class This { public static void main(String args[]) { ThisD obj = new ThisD(5, 10, 11); System.out.println("x=" + obj.x); System.out.println("y=" + obj.y); System.out.println("z=" +obj.z); } } ※実行結果 x=5 y=10 z=11 |
abstract
abstract class PreDifine { public abstract int add(int x, int y); public int sub(int x, int y) { return x - y; } } class AbstractSample extends PreDifine { public int add(int x, int y) { return x + y; } public static void main(String args[]) { AbstractSample obj = new AbstractSample(); System.out.println("add = " + obj.add(100, 80)); System.out.println("sub = " + obj.sub(100, 80)); } } ※実行結果 add = 180 sub = 20 |
interface
interface PreDefine { int add(int x, int y); int sub(int x, int y); } class InterfaceSample implements PreDefine { public int add(int x, int y) { return x + y; } public int sub(int x, int y) { return x - y; } public static void main(String args[]) { InterfaceSample obj = new InterfaceSample(); System.out.println("add = " + obj.add(100,80)); System.out.println("sub = " + obj.sub(100,80)); } } ※実行結果 add = 180 sub = 20 |
感想
まず長かったです。レポート5の二倍以上に時間をかけました。理解にも時間を使ったし、 なによりながいんで集中力が続きませんでした…次からは集中して早めに終わらせたいです。 かなり遅れてすいませんでしたm(_ _)m参考文献・HP
・独習Java第2版 『ジョゼフ・オニール著』