- 부모클래스에서 상속받아 생성자 생성하기
- 부모클래스에서 먼저 인자받는 생성자 생성 후, 아래와 같이 실행
java07
p01
Bird
package p01;
public class Bird {
String name;
String sex;
public Bird() {
}
public Bird(String name, String sex) {
this.name = name;
this.sex = sex;
}
public void eatBird( ) {};
public void flyBird(){};
public void sleepBird(){};
}
Cat
package p01;
public class Cat {
String name;
int age;
String sex;
public Cat() {
}
public Cat(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public void eatCat(){};
public void runCat(){};
public void sleepCat(){};
}
Dog
package p01;
public class Dog {
String name;
int age;
String sex;
String species;
public Dog() {
}
public Dog(String name, int age, String sex, String species) {
this.name = name;
this.age = age;
this.sex = sex;
this.species = species;
}
public void eatDog(){};
public void sleepDog(){};
}
PetTest
package p01;
public class PetTest {
public static void main(String[] args) {
Cat c = new Cat("야옹", 2, "암컷");
Dog d = new Dog("망치", 1, "수컷", "치와와");
Bird b = new Bird("참새", "수컷");
System.out.printf("Cat, 이름:%s 나이:%d 성별:%s \n", c.name, c.age, c.sex ); ;
System.out.printf("Dog, 이름:%s 나이:%d 성별:%s 종:%s \n", d.name, d.age, d.sex, d.species ); ;
System.out.printf("Bird, 이름:%s 성별:%s \n", b.name, b.sex ); ;
}
}
p02
Bird
package p02;
public class Bird extends Pet {
public Bird() {
}
public Bird(String name, String sex) {
this.name = name;
this.sex =sex;
}
public void flyBird() {
System.out.println("flyBird");
}
}
Cat
package p02;
public class Cat extends Pet{
int age;
public Cat() {}
public Cat(String name, int age, String sex) {
this.name = name;
this.sex = sex;
this.age = age;
}
public void runCat() {
System.out.println("runCat");
}
}
Dog
package p02;
public class Dog extends Pet {
int age;
String species;
public Dog() {}
public Dog(String name, int age, String sex, String species) {
this.name = name;
this.sex = sex;
this.age = age;
this.species = species;
}
}
Pet
package p02;
public class Pet {
String name;
String sex;
public Pet() {}
public Pet(String name, String sex) {
this.name = name;
this.sex = sex;
}
public void eatPet() {
System.out.println("eatPet");
}
public void sleepPet() {
System.out.println("sleepPet");
}
}
PetTest
package p02;
public class PetTest {
public static void main(String[] args) {
Cat c = new Cat("야옹", 2, "암컷");
Dog d = new Dog("망치", 1, "수컷", "치와와");
Bird b = new Bird("참새", "수컷");
System.out.printf("Cat, 이름:%s 나이:%d 성별:%s \n",
c.name, c.age, c.sex);
System.out.printf("Dog, 이름:%s 나이:%d 성별:%s 종:%s \n",
d.name, d.age, d.sex, d.species);
System.out.printf("Bird, 이름:%s 성별:%s \n", b.name, b.sex);
}
}
- 자식 객체 생성시 부모 객체가 먼저 생성되고 이후에 자식 객체가 생성된다
p03
Cat
package p03;
public class Cat extends Pet{
int age;
public Cat() {
System.out.println("Cat 생성자");
}
}
Pet
package p03;
public class Pet {
String name;
String sex;
public Pet() {
System.out.println("Pet 생성자");
}
}
PetTest
package p03;
public class PetTest {
public static void main(String[] args) {
Cat c = new Cat();
}
}
- 객체 생성 순서
Object
- 부모 객체
- 자식 객체
명시적으로 Super(값, 값2...) 요청하는 경우
- 부모의 인스턴스 변수를 자식이 아닌 부모에서 초기화하기 위함
Cat
package p03;
public class Cat extends Pet{
int age;
public Cat() {
super("AA", "BB");
System.out.println("Cat 생성자");
}
}
Pet
package p03;
public class Pet {
String name;
String sex;
public Pet(String name, String sex) {
System.out.println("Pet 생성자");
}
}
- 자동 오버라이딩 지정
p04
Cat
package p04;
public class Cat extends Pet{
int age;
public Cat() {
}
public Cat(String name, String sex, int age) {
super(name, sex);
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void runCat() {
System.out.println("runcat");
}
@Override
public String getPet() {
return name + "\t" + sex + "\t" + age;
}
}
Dog
package p04;
public class Dog extends Pet {
int age;
String species;
public Dog() {
}
public Dog(String name, String sex, int age, String species) {
super(name, sex);
this.age = age;
this.species = species;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
public String getDog() {
return name + "\t"+sex+ "\t"+ age + "\t"+species;
}
@Override
public String getPet() {
return super.getPet()+"\t"+ age + "\t"+species;
}
}
Pet
package p04;
public class Pet {
String name;
String sex;
public Pet(){
}
public Pet(String name, String sex) {
this.name = name;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void eat() {
System.out.println("Pet.eat");
}
public void sleep () {
System.out.println("Pet.sleep");
}
public String getPet() {
return name + "\t" + sex;
}
}
TestPet
package p04;
public class TestPet {
public static void main(String[] args) {
Cat c = new Cat("야용","암컷", 2);
Dog d = new Dog("망치","수컷", 1, "불독");
System.out.printf("Cat, 이름:%s 나이:%d 성별:%s \n", c.name, c.age, c.sex);
System.out.printf("Dog, 이름:%s 나이:%d 성별:%s 종:%s \n", d.name, d.age, d.sex, d.species);
System.out.println("고양이 총정보 " + c.getPet());
System.out.println("강아지 총정보 " + d.getPet());
}
}
p05
Cat
package p05;
public class Cat extends Pet{
int age;
public Cat() {
}
public Cat(String name, String sex, int age) {
super(name, sex);
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void runCat() {
System.out.println("runcat");
}
@Override
public String getPet() {
return name + "\t" + sex + "\t" + age;
}
@Override
public String toString() {
return "Cat [age=" + age + ", name=" + name + ", sex=" + sex + "]";
}
}
Dog
package p05;
public class Dog extends Pet {
int age;
String species;
public Dog() {
}
public Dog(String name, String sex, int age, String species) {
super(name, sex);
this.age = age;
this.species = species;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
public String getDog() {
return name + "\t"+sex+ "\t"+ age + "\t"+species;
}
@Override
public String getPet() {
return super.getPet()+"\t"+ age + "\t"+species;
}
}
Pet
package p05;
public class Pet {
String name;
String sex;
public Pet(){
}
public Pet(String name, String sex) {
this.name = name;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void eat() {
System.out.println("Pet.eat");
}
public void sleep () {
System.out.println("Pet.sleep");
}
public String getPet() {
return name + "\t" + sex;
}
}
TestPet
package p05;
public class TestPet {
public static void main(String[] args) {
Pet p = new Cat("야용","암컷", 2);
System.out.println("고양이 총정보 " + p.getPet());
System.out.println("고양이 총 정보"+ p.getPet());
Cat c = (Cat) p;
System.out.println(c.age);
if(p instanceof Cat) {
System.out.println(((Cat) p).age);
}
p = new Dog("망치","수컷", 1, "불독");
System.out.println("강아지 총정보 " + p.getPet());
}
}
TestPet2
package p05;
public class TestPet2 {
public static void main(String[] args) {
Pet[] pets = { new Cat("야옹이1", "암컷", 2), new Dog("망치1", "암컷", 4, "불독"), new Cat("야옹이2", "암컷", 2),
new Dog("망치2", "암컷", 1, "치와와"), new Cat("야옹이3", "암컷", 2), };
for (Pet x : pets) {
System.out.println(x.getPet());
}
System.out.println();
for (Pet pet : pets) {
if (pet instanceof Cat) {
System.out.println("Cat: " + pet.getPet());
}
}
for (Pet pet : pets) {
if (pet instanceof Cat) {
Cat c = (Cat) pet;
System.out.println("Cat: " + c.getAge());
}
}
System.out.println();
for (Pet x : pets) {
if (x instanceof Cat) {
System.out.println(((Cat) x).getPet());
System.out.println(((Cat) x).age);
}
}
}
}
TestPet3
package p05;
import java.util.Date;
public class TestPet3 {
public static void main(String[] args) {
Cat c = new Cat("야옹", "암컷", 2);
System.out.println(c);
System.out.println(c.toString());
String s = "hello";
System.out.println(s);
System.out.println(s.toString());
Date d = new Date();
System.out.println(d);
System.out.println(d.toString());
}
}
Bird
package p05;
public class Bird{
String name;
int age;
public Bird() {
}
public Bird(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Bird [name=" + name + ", age=" + age + "]";
}
}