๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฐ˜์‘ํ˜•

Java107

[Java] ๋ฌต์‹œ์  ํด๋ž˜์Šค ํ˜• ๋ณ€ํ™˜๊ณผ ๋ฉ”์„œ๋“œ ์žฌ์ •์˜ โ–ถ ๋ฌต์‹œ์  ํด๋ž˜์Šค ํ˜• ๋ณ€ํ™˜๊ณผ ๋ฉ”์„œ๋“œ ์žฌ์ •์˜ package inheritance; public class OverridingTest2 { public static void main(String[] args) { Customer vc = new VIPCustomer(10030, "๋‚˜๋ชฐ๋ผ", 2000); // VIP ๊ณ ๊ฐ ์ƒ์„ฑ vc.bonusPoint = 1000; System.out.println(vc.getCustomerName() + " ๋‹˜์ด ์ง€๋ถˆํ•ด์•ผ ํ•˜๋Š” ๊ธˆ์•ก์€ " + vc.calcPrice(10000) + "์›์ž…๋‹ˆ๋‹ค."); } } 2021. 2. 19.
[Java] ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ โ–ถ ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ์ƒ์œ„ ํด๋ž˜์Šค์— ์ •์˜ํ•œ ๋ฉ”์„œ๋“œ๊ฐ€ ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ๊ตฌํ˜„ํ•  ๋‚ด์šฉ๊ณผ ๋งž์ง€ ์•Š์„ ๊ฒฝ์šฐ์— ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ์ด๋ผ ํ•œ๋‹ค. @Override public int calcPrice(int price) { // ์žฌ์ •์˜ํ•œ ๋ฉ”์„œ๋“œ bonusPoint += price * bonusRatio; // ๋ณด๋„ˆ์Šค ํฌ์ธํŠธ ์ ๋ฆฝ return price - (int)(price * saleRatio); // ํ• ์ธ๋œ ๊ฐ€๊ฒฉ์„ ๊ณ„์‚ฐํ•˜์—ฌ ๋ฐ˜ํ™˜ } package inheritance; public class OverridingTest1 { public static void main(String[] args) { Customer customerLee = new Customer(1.. 2021. 2. 19.
[Java] ๋ถ€๋ชจ๋ฅผ ๋ถ€๋ฅด๋Š” ์˜ˆ์•ฝ์–ด, super โ–ถ ๋ถ€๋ชจ๋ฅผ ๋ถ€๋ฅด๋Š” ์˜ˆ์•ฝ์–ด, super super ์˜ˆ์•ฝ์–ด๋Š” ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ์ƒ์œ„ ํด๋ž˜์Šค๋กœ ์ ‘๊ทผํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. this๊ฐ€ ์ž๊ธฐ ์ž์‹ ์˜ ์ฐธ์กฐ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. โ€‹ package inheritance; public class CustomerTest2 { public static void main(String[] args) { VIPCustomer customerKim = new VIPCustomer(10020, "๊น€์œ ์‹ "); // ํ•˜์œ„ ํด๋ž˜์Šค ์ƒ์„ฑ //customerKim.setCustomerID(10020); //customerKim.setCustomerName("๊น€์œ ์‹ "); customerKim.bonusPoint = 10000; System.out.println(custome.. 2021. 2. 19.
[Java] ์ƒ์† โ–ถ ์ƒ์†์ด๋ž€ ? ์ƒ์†์€ ์šฐ๋ฆฌ๊ฐ€ ์ผ๋ฐ˜์ ์œผ๋กœ ์•Œ ๋“ฏ ๋ฌด์—‡์ธ๊ฐ€๋ฅผ ๋ฌผ๋ ค๋ฐ›๋Š”๋‹ค๋Š” ์˜๋ฏธ. Bํด๋ž˜์Šค๊ฐ€ Aํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์œผ๋ฉด Bํด๋ž˜์Šค๋Š” Aํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์™€ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋žจ์€ ์œ ์ง€ ๋ณด์ˆ˜ํ•˜๊ธฐ ํŽธํ•˜๊ณ  ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜ ์ƒˆ๋กœ์šด ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์œ ์—ฐํ•œ๋ฐ, ๊ทธ ๊ธฐ๋ฐ˜์ด ๋˜๋Š” ๊ธฐ์ˆ ์ด ๋ฐ”๋กœ ์ƒ์†์ด๋‹ค. ์ž๋ฐ” ๋ฌธ๋ฒ•์œผ๋กœ ์ƒ์†์„ ๊ตฌํ˜„ํ•  ๋•Œ๋Š” โ€‹extends ์˜ˆ์•ฝ์–ด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. โ€‹ โ€‹ โ€‹ โ–ถ ์ƒ์†์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ณ ๊ฐ ๊ด€๋ฆฌ ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„ํ•˜๊ธฐ package inheritance; public class Customer { // ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ private int customerID; // ๊ณ ๊ฐ ์•„์ด๋”” private String customerName; // ๊ณ ๊ฐ ์ด๋ฆ„ private String customerG.. 2021. 2. 19.
[Java] ์—ฐ์Šต๋ฌธ์ œ ์—ฐ์Šต๋ฌธ์ œ Q2) 206์ชฝ์˜ ์•ŒํŒŒ๋ฒณ ์ถœ๋ ฅ ์˜ˆ์ œ์—์„œ ๊ฐ ๋ฐฐ์—ด ์š”์†Œ ๊ฐ’์„ ์†Œ๋ฌธ์ž์—์„œ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•ด ์ถœ๋ ฅํ•˜๋ผ. -์‰ฌ์› ์Œ. โ€‹ โ€‹ โ€‹ โ€‹โ€‹ ์—ฐ์Šต๋ฌธ์ œ Q3) ๋ฐฐ์—ด ๊ธธ์ด๊ฐ€ 5์ธ ์ •์ˆ˜ํ˜• ๋ฐฐ์—ด์„ ์„ ์–ธํ•˜๊ณ , 1~10 ์ค‘ ์ง์ˆ˜๋งŒ์„ ๋ฐฐ์—ด์— ์ €์žฅํ•œ ํ›„ ๊ทธ ํ•ฉ์„ ์ถœ๋ ฅํ•˜๋ผ. -๊ณ„์† ๊ฒฐ๊ณผ๊ฐ’์ด 0์ด ๋‚˜์™”๋‹ค. ๋ญ๊ฐ€ ๋ฌธ์ œ์ธ์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค โ€‹ ๋”๋ณด๊ธฐ โ€‹โ€‹ โ€‹ ์—ฐ์Šต๋ฌธ์ œ Q4) ๋‹ค์Œ๊ณผ ๊ฐ™์ด Dog ํด๋ž˜์Šค๊ฐ€ ์žˆ๋‹ค. DogTest ํด๋ž˜์Šค์™€ ๋ฐฐ์—ด ๊ธธ์ด๊ฐ€ 5์ธ Dog[ ] ๋ฐฐ์—ด์„ ๋งŒ๋“  ํ›„ Dog ์ธ์Šคํ„ด์Šค๋ฅผ 5๊ฐœ ์ƒ์„ฑํ•˜์—ฌ ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•œ๋‹ค. for๋ฌธ๊ณผ ํ–ฅ์ƒ๋œ for๋ฌธ์—์„œ Dog ํด๋ž˜์Šค์˜ showDogInfo( ) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•œ Dog ์ •๋ณด๋ฅผ ๋ชจ๋‘ ์ถœ๋ ฅํ•˜๋ผ. *์กฐ๊ฑด : ๋ฐ‘ ํด๋ž˜์Šค๋ฅผ ๋ฒ ์ด์Šค๋กœ ๋งŒ๋“ค ๊ฒƒโ–ผโ–ผโ–ผโ–ผโ–ผ โ€‹ -์ฒ˜์Œ์—” ArrayList๋กœ.. 2021. 2. 19.
[Java] ArrayList ํด๋ž˜์Šค โ–ถ ArrayList ํด๋ž˜์Šค ๊ธฐ์กด ๋ฐฐ์—ด์˜ ๋‹จ์ ์„ ๋ณด์™„ํ•จ. ex) ArrayList library = new ArrayList( ); package array; import java.util.ArrayList; // ArrayList ํด๋ž˜์Šค import public class ArrayListTest { public static void main(String[] args) { ArrayList library = new ArrayList(); // ArrayList ์„ ์–ธ library.add(new Book("ํƒœ๋ฐฑ์‚ฐ๋งฅ", "์กฐ์ •๋ž˜")); library.add(new Book("๋ฐ๋ฏธ์•ˆ", "ํ—ค๋ฅด๋งŒ ํ—ค์„ธ")); library.add(new Book("์–ด๋–ป๊ฒŒ ์‚ด ๊ฒƒ์ธ๊ฐ€", "์œ ์‹œ๋ฏผ")); library.add(n.. 2021. 2. 19.
๋ฐ˜์‘ํ˜•