Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- .xml
- 어노테이션
- @RequestMapping
- 바인딩변수
- @Valid
- @ResponseBody
- springmvc
- gradle
- application.properties
- @
- spring
- frontController
- 의존주입
- 스프링
- 비즈니스레이어
- 서비스레이어
- Java
- after-throwing
- PointCut
- 생성자주입
- AOP
- @RequestParam
- springjdbc
- c:if
- 유효성검사
- MVC
- produces
- Model
- jointpoint
- SpringBoot
Archives
- Today
- Total
메모장
[실습문제] 상속 (예제 만들기) 본문
728x90
반응형
클래스 구현하기
1) 멤버변수 3개이상
클래스변수는 포함시켜도되고,안해도되고
2) 메서드 2개이상
오버라이딩 1개포함해서 메서드 2개이상
3) 생성자 오버로딩 사용
this 활용
super 활용
main()에서
부모클래스 객체 1개,
자식클래스 객체 2개 생성하여 사용!
나는 동물로 클래스를 만들어 보았다 !
한글 코딩 먼저 ~~~~~!
▶동물 클래스{
String 이름
int 나이
String 타입
동물(이름 , 나이 , 타입){ // 생성자
이름
나이
타입
}
나이출력(){ // 메서드
}
울음소리(){
}
동작(){
}
}
▶강아지 클래스{
강아지(이름, 나이, 타입){
super(name , age, type);
}
나이 출력(){
}
울음소리(){
}
동작(){
}
}
▶고양이 클래스{
고양이(이름,나이 ,타입){
super(name, age, type);
}
나이 출력(){
}
울음소리(){
}
동작(){
}
}
main(){
동물 a = new 동물();
강아지 d = new 강아지();
고양이 c = new 고양이();
}
계획을 세운 후 코딩 진행 !
package class04;
class Animal { // 이름, 종, 나이
String name; // 이름
String type; // 종
int age; // 나이
Animal(String name, String type) { // 오버로딩
this(name, type, 0);
}
Animal(String name, String type, int age) { // 이름, 종, 나이
this.name = name;
this.type = type;
this.age = age;
}
void printAge() { // 나이출력
System.out.print(this.name + "의 종은 " + this.type + " 이고, ");
System.out.println(this.age + "살 입니다.");
}
void sound() { // 울음소리
System.out.println(this.name + "의 울음소리 : 우엉우엉");
}
void action() { // 행동
System.out.println(this.name + "는 앉기, 뛰기 가능합니다.");
}
}
class Dog extends Animal { // 강아지 클래스
Dog(String name, String type, int age) { // 상속
super(name, type, age); // 부모클래스 가져오기
}
void sound() { // 울음소리 // 오버라이딩
System.out.println(this.name + "의 울음소리 : 멍멍 !");
}
void action() { // 행동 // 오버라이딩
System.out.println(this.name + "는 앉기, 뛰기, 구르기, 호출 가능합니다.");
}
}
class Cat extends Animal { // 고양이 클래스
Cat(String name, String type, int age) { // 상속
super(name, type, age); // 부모클래스 가져오기
}
void sound() { // 울음소리 // 오버라이딩
System.out.println(this.name + "의 울음소리 : 야옹 !");
}
void action() { // 행동 // 오버라이딩
System.out.println(this.name + "는 앉기 , 할퀴기 가능합니다.");
}
}
public class Test04 {
public static void main(String[] args) {
Animal a = new Animal("뽀삐", "시츄", 2);
Dog d = new Dog("초코", "말티즈", 1);
Cat c = new Cat("나비", "페르시안고양이", 3);
a.printAge();
d.printAge();
c.printAge();
System.out.println();
a.sound();
d.sound();
c.sound();
System.out.println();
a.action();
d.action();
c.action();
}
}

출력 하면 짠 !
728x90
반응형
'JAVA > [실습문제]' 카테고리의 다른 글
| [실습문제] 포켓몬(추상클래스, private) (0) | 2024.04.08 |
|---|---|
| [실습문제] 상속 (응용문제) (0) | 2024.04.08 |
| [실습 문제] 상속 (포켓몬) (0) | 2024.04.08 |
| [자판기 프로그램] (0) | 2024.04.08 |
| 포켓몬 문제 ( class 만들기 ) (0) | 2024.04.08 |