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
- java#왕초보
- 왕초보
- 쿠키
- 버전일치
- 시큐어코딩
- FIle
- git
- webpack
- java
- 코틀린
- 상속
- SQL
- 안드로이드
- Android
- 스타일보험
- Spring
- sub query
- 숫자
- 함수
- 답글
- kotlin
- Spinner
- 미니게임
- snowpack
- 오버라이드
- 게시판
- degit
- React
- parcel
- 스프링
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 |