[33]誇張
1.通帳
- public class Account結果値
アカウント:111-111
預金者名:朴吉童
残高:-7000 -ジャワの多形性
:javaの多形性を誇張して表現します.
ex)グラフィック[]arr{
新しい三角形()
新元()
新しい長方形()
}
for(int i =0; i < arr.length; i++){
arr[i].draw(); -> Javaの多形性表現.
}
- public class Account
// oop는 객체지향프로그램이라는 뜻이다.
package kosta.oop2;
//객체지향이므로,
//무조건 계좌와 관련된 내용만 들어가야한다.
public class Account {
// 계좌에 대한 객체를 만들어보자.
// Account에 대한 객체이므로, main()은 필요가 없다.
// 상태 : 계좌번호, 계좌주, 잔액 -> 변수
// 기능 : 입금하다. 출금하다. -> 메서드
// 상태 구현 -> 변수로 표현
// new로 객체를 생성해서, 메모리를 할당하는 시스템이라서 변수를 초기화하지 않아도 된다.
// 멤버변수(필드) : 초기화하지 않는다. -> 사용범위는 객체가 생성되고, 소멸될 때까지 이다.
String accuntNo; // 계좌번호. 문자열
String ownerName; // 예금주 이름. 문자열
int balance; // 잔액. 정수
// 일반적으로 변수는 디폴트값이다.
// class 내부에 있는 변수는 멤버변수
// 특정한 메서드 내부에 있는 변수는 지역변수 : 초기화가 필요하다.
// 클래스의 파라미터가 없는 경우
public Account() {}
// 생성자 : 객체를 초기화 하기 위해서
// Source - Generate Constuctor using Fields에서, 자동으로 만드는 방법도 있다.
public Account(String accuntNo, String ownerName, int balance) { // 파라미터 개수와 테이터형
this.accuntNo = accuntNo;
this.ownerName = ownerName;
this.balance = balance;
}
public Account(String accuntNo, String ownerName) {
this.accuntNo = accuntNo;
this.ownerName = ownerName;
}
// 메서드 호출() new 다른 객체() -> 아직 안배웠지만, 다른 객체도 불러올 수 있다.
// 기능 구현 -> 메서드로 표현 (기능 하나에 메서드 하나를 사용한다.)
// 1. 입금하다.
// 만약, 입금하고 메세지가 필요하다면 retrun형이 필요하지만 입금만 하므로 쓰지 않는다.
// retrun형은 void를 사용한다.
public void deposit(int amount) {
balance += amount; // 잔액에 금액을 추가한다.
}
// 2. 출금하다.
// 출금하고 메세지가 필요하므로 retrun형을 사용한다.
// if문에서 나갈려면 0을 return한다.
// retrun형은 int를 사용한다.
public int withdraw(int amount)throws Exception{
if(balance < amount) { // 잔액 < 출금액이면, 안된다.
throw new Exception("잔액 부족"); // -------- > 수정 코드
}
balance -= amount; // 잔액에서 금액을 뺀다.
return amount; // 뺀 금액을 return한다. -> 메세지 및 표현
}
// 추가 문제
// 객체의 내용을 출력하는 메서드
// 호출한 인스턴스 변수에 따라서, 주소를 찾아가므로 값이 달라진다.
public void print() {
System.out.println("계좌번호 : "+accuntNo);
System.out.println("예금주 이름 : "+ownerName);
System.out.println("잔액 : "+balance);
System.out.println();
}
}
- public class MinusAccount extends Accountpackage kosta.oop2;
import kosta.oop.Account;
public class MinusAccount extends Account {
private int creditine; // 마이너스 한도
public MinusAccount() {}
public MinusAccount(String accuntNo, String ownerName, int balance, int creditine) {
super(accuntNo, ownerName, balance);
this.creditine = creditine;
}
// Account : withdraw(x) -> MinusAccount : withdraw(o)
// 메서드 오버라이딩 : 부모의 메서드의 시그너처 일치 (리턴형, 메서드 이름, 파라미터(개수, 데이터형), 예외관련)
// public int withdraw(int amount)throws Exception
// 리턴형 : int, 메서드 이름 : withdraw,
@Override
public int withdraw(int amount)throws Exception{
if(getBalance() + creditine < amount){
throw new Exception("잔액부족");
}
int balance = getBalance();
setBalance(balance - amount);
return amount;
}
}
- public class MinusMainpackage kosta.oop2;
public class MinusMain {
public static void main(String[] args) {
MinusAccount ma = new MinusAccount("111-111", "박길동", 5000, 10000);
try {
ma.withdraw(12000); // minusAccount의 withdraw가 출력
} catch (Exception e) {
e.printStackTrace();
}
ma.print();
}
}
アカウント:111-111
預金者名:朴吉童
残高:-7000
:javaの多形性を誇張して表現します.
ex)グラフィック[]arr{
新しい三角形()
新元()
新しい長方形()
}
for(int i =0; i < arr.length; i++){
arr[i].draw(); -> Javaの多形性表現.
}
Reference
この問題について([33]誇張), 我々は、より多くの情報をここで見つけました https://velog.io/@sanggeun/33-오버라이딩テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol