質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

1回答

1273閲覧

run-time type と compile-time type の違い

rakkn

総合スコア6

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2020/05/31 04:08

編集2020/05/31 08:56

目的

run-time type と compile-time type の違いを理解する。

該当のソースコード

java

1class Parent { 2 public static void myStaticMethod() { 3 System.out.println("A"); 4 } 5 public void myInstanceMethod() { 6 System.out.println("B"); 7 } 8} // End of the Parent class 9public class Child extends Parent { 10 public static void myStaticMethod() { 11 System.out.println("C"); 12 } 13 public void myInstanceMethod() { 14 System.out.println("D"); 15 } 16 public static void main(String[] args) { 17 Parent o1 = new Parent(); 18 Parent o2 = new Child(); 19 Child o3 = new Child(); 20 Parent.myStaticMethod();// A 21 Child.myStaticMethod(); // C 22 o1.myStaticMethod(); // A 23 o1.myInstanceMethod(); // B 24 o2.myStaticMethod(); // A ★ 25 o2.myInstanceMethod(); // D ☆ 26 o3.myStaticMethod(); // C 27 o3.myInstanceMethod(); // D 28 myStaticMethod(); // C 29 } // End of main method 30} // End of the Child class

###説明文
Notice that o2.myStaticMethod invokes Parent.myStaticMethod(). If this method were truly overridden,we should have invoked Child.myStaticMethod, but we didn't. Rather, when you invoke a static method, even if you invoke it on an instance, you really invoke the method associated with the "compile-time type" of the variable. In this case, the compile-time type of o2 is Parent. Therefore, we invoke Parent.myStaticMethod(). However, when we execute the line o2.myInstanceMethod(), we really invoke the method Child.myInstanceMethod(). That's because, unlike static methods, instance methods CAN be overridden. In such a case, we invoke the method associated with the run-time type of the object. Even though the compile time type of o2 is Parent, the run-time type (the type of the object o2 references) is Child. Therefore, we invoke Child.myInstanceMethod rather than Parent.myInstanceMethod().

質問

上記の説明に、★はcompile-time type を見ているから出力が”A"になると書いてあり、☆はrun-time type を見ているから”D"になると書いてあるのですが、このcompile-time type とrun-time typeの明確な違いがわかりません。これらの明確な違いを教えてください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

momon-ga

2020/05/31 06:18 編集

もう少し、他者が誤解しないように質問してくれませんか? コンパイル時と実行時は、明らかに違うもので、質問者さんがその違いをわからない人だと思えません。
javahack

2020/05/31 08:26

説明の引用元を質問に追記してください。
rakkn

2020/05/31 08:59

説明の引用元を質問に追記してください。 >申し訳ないのですが、大学の資料なので公開はできません。説明文は追記させていただきました。
guest

回答1

0

ベストアンサー

大まかに以下の方法でクラス情報を取得するのが仕様のため

Parent.myStaticMethod();// A : クラス名を指定してる Child.myStaticMethod(); // C : クラス名を指定してる o1.myStaticMethod(); // A : 宣言のクラス名 (Parent o1)で実行 o1.myInstanceMethod(); // B : 変数のクラス名 (new Parent)で実行 o2.myStaticMethod(); // A : 宣言のクラス名 (Parent 02)で実行 ★ o2.myInstanceMethod(); // D : 変数のクラス名 (new Child)で実行 ☆ o3.myStaticMethod(); // C : 宣言のクラス名 (Child 03)で実行  o3.myInstanceMethod(); // D : 変数のクラス名 (new Child)で実行  myStaticMethod(); // C : 実行クラス名  (Child#main)で実行 

compile-time type -> 宣言
run-time type -> 実装

こうなるため static method は 変数から実行するなとなる。

投稿2020/05/31 09:14

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

rakkn

2020/05/31 12:58

ご回答ありがとうございます。そういうことだったんですね!改めてありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問