๋ฐ˜์‘ํ˜•

Java 3

[Java] ์ƒ์†๊ณผ ์ƒ์„ฑ์ž

์ด ๊ธ€์€ ์ƒํ™œ์ฝ”๋”ฉ์˜ 'Java' ๊ฐ•์ขŒ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. ์ƒ์†๊ณผ ์ƒ์„ฑ์ž package org.opentutorials.javatutorials.Inheritance.example4; public class ConstructorDemo { public static void main(String[] args) { ConstructorDemo c = new ConstructorDemo(); } } ์œ„ ์ฝ”๋“œ๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ConstructorDemo ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์ž๋™์œผ๋กœ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. package org.opentutorials.javatutorials.Inheritance.example4; public class ConstructorDemo { public C..

Programming/Java 2022.10.25

[Java] ์ƒ์†

์ด ๊ธ€์€ ์ƒํ™œ์ฝ”๋”ฉ์˜ 'Java' ๊ฐ•์ขŒ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. ์ƒ์†์ด๋ž€? ๊ฐ์ฒด ์ง€ํ–ฅ์—์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋ชฉํ‘œ ์ค‘ ํ•˜๋‚˜๋Š” ๋ฐ”๋กœ ์žฌํ™œ์šฉ์„ฑ์ž…๋‹ˆ๋‹ค. ์ƒ์†์€ ๊ฐ์ฒด์ง€ํ–ฅ์˜ ์žฌํ™œ์šฉ์„ฑ์„ ๊ทน๋Œ€ํ™”์‹œํ‚จ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ๋ฒ•์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋™์‹œ์— ๊ฐ์ฒด ์ง€ํ–ฅ์„ ๋ณต์žกํ•˜๊ฒŒ ํ•˜๋Š” ์ฃผ์š” ์›์ธ์ด๋ผ๊ณ ๋„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ƒ์†(Inheritance)์ด๋ž€ ๋ฌผ๋ ค์ค€๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์–ด๋–ค ๊ฐ์ฒด๊ฐ€ ์žˆ์„ ๋•Œ ๊ทธ ๊ฐ์ฒด์˜ ํ•„๋“œ(๋ณ€์ˆ˜)์™€ ๋ฉ”์†Œ๋“œ๋ฅผ ๋‹ค๋ฅธ ๊ฐ์ฒด๊ฐ€ ๋ฌผ๋ ค๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ƒ์†์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. package org.opentutorials.javatutorials.object; class Calculator{ int left, right; public void setOprands(int left, int right){ this.left = l..

Programming/Java 2022.10.24

[Java] ์ดˆ๊ธฐํ™”์™€ ์ƒ์„ฑ์ž

์ด ๊ธ€์€ ์ƒํ™œ์ฝ”๋”ฉ์˜ 'Java' ๊ฐ•์ขŒ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. ์ดˆ๊ธฐํ™” ์–ด๋–ค ์ผ์„ ์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ์ค€๋น„ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ์ด๋ฅผ ์ดˆ๊ธฐํ™”๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ๋„ ์ดˆ๊ธฐํ™”์— ํ•ด๋‹นํ•˜๋Š” ๊ธฐ๋Šฅ์ด ์ œ๊ณต๋˜๋Š”๋ฐ ์ด๋ฅผ ์ƒ์„ฑ์ž (constructor)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์•ž์„œ ์‚ดํŽด๋ดค๋˜ ๊ณ„์‚ฐ๊ธฐ ์˜ˆ์ œ๋ฅผ ๋‹ค์‹œ ๋ด…์‹œ๋‹ค. Calculator c1 = new Calculator(); c1.setOprands(10, 20); c1.sum(); c1.avg(); ๋ฉ”์†Œ๋“œ setOperands์˜ ๊ฐ’์œผ๋กœ 10๊ณผ 20์„ ์ง€์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๊ฐ์ฒด ๋‚ด๋ถ€์—์„œ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ left๊ณผ right์˜ ๊ฐ’์œผ๋กœ ์„ค์ •๋˜์–ด ์œ ์ง€๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฉ”์†Œ๋“œ setOperands ํ˜ธ์ถœํ•˜๊ธฐ ์ „์— sum๊ณผ avg๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค๋ฉด ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. C..

Programming/Java 2022.10.24
๋ฐ˜์‘ํ˜•