-
-
Save zephiransas/8326406e4f2cb8d20c66511642395978 to your computer and use it in GitHub Desktop.
パッケージ名がわからないクラスにあるメソッドを呼び出すプログラム。(一部抜粋)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cl1; | |
public class class1{ | |
public static void start(){ | |
System.out.println("成功!2"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cl2; | |
import cl1.class1; | |
public class class2{ | |
public static void main(String[] args){ | |
// パッケージ名が分からない、とのことだけど実際にはパッケージ名は分かるね | |
// class1.javaのpackage文から、cl1パッケージであることがわかる | |
// なのでやりたいことを正確に表現すると、「cl1パッケージにあるclass1クラスのstaticメソッド"start"を実行したい」だね | |
// ただ、現在のクラスclass2からはcl1のclass1クラスは見えないので、まずはimport文でclass1を見えるようにしてやります | |
// 3行目のimport文を参照してください。こうすることでclass1が見えるようになります | |
// あとはclass1のstaticメソッドを呼ぶので | |
class1.start(); | |
// で"成功!2"が出力されるはずです。 | |
// 最初「親クラスから子クラス〜」という話しだったけど、extendを使ってないので、親子関係はありません | |
// またクラス名は小文字で開始するのは(できるけど)推奨されません。なのでclass1はClass1とするのがベストです。ファイル名も同様にClass1.javaになります。 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cl3; | |
public class class3{ | |
public static void start(){ | |
System.out.println("成功!:3"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ありがとうございます、三宅さんが教えてくださったことを自分が作っているものにちょっと修正して、解決しました。
https://gist.github.com/yumaS19/8fc767ac1b24df76a3b65bed5fab6b51