ํ์ ์ ๋ณด ํด๋์ค ๊ตฌํํ๊ณ , 2๊ฐ์ ๊ฐ์ฒด ์์ฑํ๊ธฐ
- ํด๋์ค ์์ฑ
- ํด๋์ค ์ด๋ฆ: Student
- ๋ฉค๋ฒ๋ณ์: num, name, c, java, net(์ ์)
- ๋ฉ์๋
- ์์ฑ์(num, name, c, java, net ์ด๊ธฐํ)
- ์ด์ ๊ณ์ฐ(getTot())
- ์ถ๋ ฅ(printinfo())
class Student{
int num, c, java, net, tot;
String name;
Student(){ //์์ฑ์๋ก ๋ฉค๋ฒ ๋ณ์๋ฅผ ์ด๊ธฐํ
num = 0;
name = "";
c = 0;
java = 0;
net = 0;
}
Student(int num, String name, int c, int java, int net){
this.num = num;
this.name = name;
this.c = c;
this.java = java;
this.net = net;
}
void getTot() {
tot = c + java + net;
}
void printInfo() {
System.out.println(num+", "+name+", "+c+", "+java+", "+net+", "+tot);
}
}
public class ExampleEx {
public static void main(String[] args) {
Student s1 = new Student(1, "hong",99,77,88);
Student s2 = new Student(2, "kim",90,70,80);
Student s3 = new Student();
s1.getTot(); s2.getTot(); s3.getTot();
s1.printInfo(); s2.printInfo(); s3.printInfo();
}
}
this ์ฌ์ฉ๋ฒ
https://kadosholy.tistory.com/92
this.๋ณ์ = ๊ฐ์ ธ์จ ๊ฑธ๋ก ํด์ ์ด๊ธฐํ.
this() ๊ดํธ์ ๋ฃ์ ๊ฒ์ ๋ง๋ ๋ค๋ฅธ ์์ฑ์๋ฅผ ๋ถ๋ฆ.
๋์ค ํจ์๋ ํญ์ ๋งจ ์์ ์์นํด์ผํจ.
728x90
๋ฐ์ํ
0