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

Java107

[Java] ๋ณ€์ˆ˜ ์œ ํšจ ๋ฒ”์œ„ โ–ถ ๋ณ€์ˆ˜ ์œ ํšจ ๋ฒ”์œ„ ์ง€์—ญ ๋ณ€์ˆ˜ { } ์ง€์—ญ ๋ณ€์ˆ˜๋Š” ํ•จ์ˆ˜๋‚˜ ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์— ์„ ์–ธํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•จ์ˆ˜ ๋ฐ–์—์„œ๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. โ€‹ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜=์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋Š” ํด๋ž˜์Šค์˜ ์–ด๋Š ๋ฉ”์„œ๋“œ์—์„œ๋‚˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ํด๋ž˜์Šค ๋‚ด๋ถ€์˜ ์—ฌ๋Ÿฌ ๋ฉ”์„œ๋“œ์—์„œ ์‚ฌ์šฉํ•  ๋ณ€์ˆ˜๋Š” ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. โ€‹ static ๋ณ€์ˆ˜์˜ ์œ ํšจ ๋ฒ”์œ„ ์‚ฌ์šฉ์ž๊ฐ€ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ์— ํ”„๋กœ๊ทธ๋žจ์ด ์ƒ์ฃผํ•œ๋‹ค. ์ด๋•Œ ํ”„๋กœ๊ทธ๋žจ ์˜์—ญ ์ค‘์— ๋ฐ์ดํ„ฐ ์˜์—ญ์ด ์žˆ๋‹ค. ์ด ์˜์—ญ์—๋Š” ์ƒ์ˆ˜๋‚˜ ๋ฌธ์ž์—ด, static ๋ณ€์ˆ˜๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค. ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋Š” ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๋ฌธ์žฅ ์ฆ‰ new๊ฐ€ ๋˜์–ด์•ผ ์ƒ์„ฑ๋˜์ง€๋งŒ, static ๋ณ€์ˆ˜๋Š” ํด๋ž˜์Šค ์ƒ์„ฑ๊ณผ ์ƒ๊ด€ ์—†์ด ์ฒ˜์Œ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ ์˜์—ญ ๋ฉ”๋ชจ๋ฆฌ์— ์ƒ์„ฑ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์™€ static ๋ณ€์ˆ˜๋Š” ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋‹ค๋ฅด๋‹ค 2021. 2. 19.
[Java] ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ โ–ถ ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ static ๋ณ€์ˆ˜๋ฅผ ์œ„ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ 'static ๋ฉ”์„œ๋“œ' ๋˜๋Š” 'ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ' ๋ผ๊ณ  ํ•œ๋‹ค. package staticex; public class Student2 { private static int serialNum = 1000; int studentID; String studentName; int grade; String address; public Student2() { serialNum++; studentID = serialNum; } public String getStudentName() { return studentName; } public void setStudentName(String name) { studentName = name; } public static int .. 2021. 2. 18.
[Java] Static ๋ณ€์ˆ˜ โ–ถ Static ๋ณ€์ˆ˜ package staticex; public class Student { public static int serialNum = 1000; // static ๋ณ€์ˆ˜๋Š” ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ๊ณผ ์ƒ๊ด€์—†์ด ๋จผ์ € ์ƒ์„ฑ๋จ public int studentID; public String studentName; public int grade; public String address; public String getStudentName() { return studentName; } public void setStudentName(String name) { studentName = name; } } package staticex; public class StudentTest1 { public static voi.. 2021. 2. 18.
[Java] ๊ฐ์ฒด ๊ฐ„ ํ˜‘๋ ฅ โ–ถ ๊ฐ์ฒด ๊ฐ„ ํ˜‘๋ ฅ package cooperation; public class Student { public String studentName; // ํ•™์ƒ ์ด๋ฆ„ public int grade; // ํ•™๋…„ public int money; // ํ•™์ƒ์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ˆ public Student(String studentName, int money) { // ์ด๋ฆ„๊ณผ ๋ˆ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ this.studentName = studentName; this.money = money; } public void takeBus(Bus bus) { bus.take(1000); this.money -= 1000; } public void takeSubway(Subway subway) { subway.take(1500); this... 2021. 2. 18.
[Java] this ์˜ˆ์•ฝ์–ด โ–ถ this ์˜ˆ์•ฝ์–ด this๋Š” ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค ์Šค์Šค๋กœ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์˜ˆ์•ฝ์–ด. package thisex; class BirthDay { int day; int month; int year; public void setYear(int year) { // ํƒœ์–ด๋‚œ ์—ฐ๋„๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ฉ”์„œ๋“œ this.year = year; } public void printThis() { // this ์ถœ๋ ฅ ๋ฉ”์„œ๋“œ System.out.println(this); // this ์ถœ๋ ฅํ•ด๋ณด๊ธฐ } } public class ThisExmaple { public static void main(String[] args) { BirthDay bDay = new BirthDay(); bDay.setYear(2000); // ํƒœ์–ด๋‚œ ์—ฐ๋„๋ฅผ 2000์œผ๋กœ.. 2021. 2. 18.
[Java] ์—ฐ์Šต๋ฌธ์ œ + ์‹ค์Šต Q4) 05-7์—์„œ ์˜ˆ์ œ๋กœ ๋‚˜์˜จ MyDate์™€ MyDateTest ํด๋ž˜์Šค๋ฅผ ์™„์„ฑํ•ด ๋ด…์‹œ๋‹ค. โ€‹ - day, month, year ๋ณ€์ˆ˜๋Š” private์œผ๋กœ ์„ ์–ธํ•œ๋‹ค - ๊ฐ ๋ณ€์ˆ˜์˜ get, set ๋ฉ”์„œ๋“œ๋ฅผ public์œผ๋กœ ๋งŒ๋“ ๋‹ค - MyDate(int day, int month, int year) ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ ๋‹ค - public boolean isValid( ) ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด ๋‚ ์งœ๊ฐ€ ์œ ํšจํ•œ์ง€ ํ™•์ธ - MyDateTest ํด๋ž˜์Šค์—์„œ ์ƒ์„ฑํ•œ MyDate ๋‚ ์งœ๊ฐ€ ์œ ํšจํ•œ์ง€ ํ™•์ธํ•œ๋‹ค package test; public class MyDate { private int day; private int month; private int year; boolean valid = true; public MyDate(int d.. 2021. 2. 18.
๋ฐ˜์‘ํ˜•