Created
July 28, 2010 09:37
-
-
Save johobemax/493871 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 animal; //animalパッケージ | |
public class Animal | |
{ | |
protected String name; //protectedなので、packageが違っていても、サブクラスなら参照可能 | |
protected String voice; //protectedなので、packageが違っていても、サブクラスなら参照可能 | |
public Animal() | |
{ | |
name = "動物"; | |
voice = "がおー"; | |
} | |
public String shout() | |
{ | |
return voice; | |
} | |
} |
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
import animal.Animal; | |
import dog.Dog; | |
public class AnimalTest | |
{ | |
public static void main(String[] args) | |
{ | |
Animal a = new Dog(); | |
System.out.println(a.shout()); | |
} | |
} |
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 dog; //dogパッケージ | |
import animal.Animal; | |
public class Dog extends Animal //Animalを継承するDogクラス | |
{ | |
public Dog() //オーバーライドされたコンストラクタ | |
{ | |
name = "犬"; //Animalクラスの、protected String name; を参照している | |
voice = "わん"; //Animalクラスの、protected String voice; を参照している | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
パッケージと、継承と、protectedのサンプル。
結果は==> わん