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
- 안드로이드
- 상속
- FIle
- Spinner
- SQL
- 버전일치
- git
- kotlin
- parcel
- 시큐어코딩
- 숫자
- snowpack
- java
- 쿠키
- 미니게임
- Android
- 왕초보
- sub query
- 게시판
- 코틀린
- 오버라이드
- React
- Spring
- 스프링
- 스타일보험
- java#왕초보
- webpack
- 함수
- 답글
- degit
Archives
- Today
- Total
YSHUSH
Name card(interface) 본문
Main class
public class MainClass {
public static void main(String[] args) {
// 8.
NameCard namecard = new NameCard("홍길동", "123-4567", "hgd@naver.com");
// 9.
namecard.setPrintCard(new PrintNameCard());
namecard.print();
// 11.
namecard.setPrintCard(new PrintNamePhoneCard());
namecard.print();
}
}
Print interface
public interface PrintInterface {
// 3.
public void print(NameCard nc);
}
Name card
public class NameCard {
// 1.
String name;
String phone;
String email;
// 5. interface
PrintInterface printInterface;
// 2.
public NameCard(String name, String phone, String email) {
this.name = name;
this.phone = phone;
this.email = email;
}
// 6.
public void setPrintCard(PrintInterface p) {
printInterface = p;
}
// 7.
public void print() {
printInterface.print(this);
}
}
Name card
public class PrintNameCard implements PrintInterface {
// 4.
@Override
public void print(NameCard nc) {
System.out.println("이름: " + nc.name);
}
}
Name card
public class PrintNamePhoneCard implements PrintInterface {
// 10.
@Override
public void print(NameCard nc) {
System.out.println("이름: " + nc.name);
System.out.println("전화번호: " + nc.phone);
}
}'Coding > Java' 카테고리의 다른 글
| Static (0) | 2021.12.17 |
|---|---|
| Final (0) | 2021.12.17 |
| Interface (0) | 2021.12.17 |
| Abstract class (0) | 2021.12.17 |
| Over ride3 (0) | 2021.12.16 |