풀스택/java
자바 _ 상속관련 내용
lsme
2022. 4. 11. 21:46
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
//this()
//자기 자신 클래스의 다른 생성자를 호출
//여러개의 생성자에서 공통적으로 수행해야하는 작업을 특정 생성자에만 정의해서 중복코드를 제거
//요구사항
//음료수 클래스를 정의한다.
//속성은 음료수명과 가격이 있다. 음료수명은 기본값이 물, 800원.
//this()를 사용하는 규칙
//1. this()는 생성자의 첫번째 실행문으로 사용되어야 한다.
//2. this()는 recursive call 지원 안함.
class Drink{
String name;
int price;
public Drink() { //생성자는 그냥 공백에서 Ctrl+space -> enter
// TODO Auto-generated constructor stub
this("물, 800");
}
public Drink(int price) {
// TODO Auto-generated constructor stub
this("물, pirce");
/*
* this.name = "물"; this.price = price;
*/
}
public Drink(String name, int price) {
// TODO Auto-generated constructor stub
this("name, 800");
}
public Drink(String name) {
// TODO Auto-generated constructor stub
//this(name, price) //recursive call
this.name = "name";
this.price = price;
}
}
public class ThisConstructorTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
//상속.
//상속은 물려주는 것.
//부모 클래스를 확장한다.
//오버라이딩 = 재정의한다.
class Parent{
int parentVar;
void ParentMethod() {
System.out.println("parent method");
}
}
class Child extends Parent{
int childVar;
void childMethod() {
System.out.println("child method");
}
}
class GrandChild extends Child{
int grandVar;
void grandMethod() {
System.out.println("grand method");
}
}
public class ExtendsTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Child child = new Child();
child.childMethod();
child.ParentMethod();
System.out.println("child.parentVar ="+ child.parentVar);
System.out.println("child.childvar = " + child.childVar);
GrandChild grandChild = new GrandChild();
System.out.println("grandChild.parentVar = "+ grandChild.parentVar);
System.out.println("grandChild.childVar = "+ grandChild.childVar);
System.out.println("grandChild.grandVar = "+ grandChild.grandVar);
grandChild.ParentMethod();
grandChild.childMethod();
grandChild.grandMethod();
}
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
//자바에서 부모클래스에 정의된 정의나 메소드를 자식 클래스에서 재정의하면
//부모 클래스에서 정의한 변수나 메소드는 은폐된다.
class OverridingParent{
int money =100;
public void parentMethod() {
System.out.println("parent method");
}
}
class OverridingChild extends OverridingParent{
String money = "500원";
@Override //코드상에 설정을 할 수 있는 기능. 아래에 정의하는 메소드가 부모클래스에 정의되는 메소드를
오버라이딩하는 메소드라는 의미. public void parentMethod() {
// TODO Auto-generated method stub
System.out.println("overriding method");
}
}
public class OveerridingTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
OverridingChild overridingChild = new OverridingChild();
overridingChild.parentMethod();
overridingChild.money ="100원";
}
}
|
cs |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
class Employee{
String name;
String department;
int salary;
String getInformation() {
return "이름 : " + name + ", 부서: " + department + ", 급여: " + salary;
}
}
class Salesman extends Employee{
int commisstion;
String getInformation() {
/*//밑에 방법이 더 간단.
* return "이름 : " + name + ", 부서: " + department + ", 급여: " + salary + "수당: " +
* " commission";
*/
return super.getInformation() + ", 수당:" +commisstion;
}
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
class Super {
String name = "홍길동";
}
class Sub extends Super{
String name = "김영희";
String getLoclaName() {
String name = "김철수";
return name;
}
String getThisName() {
String name = "김철수";
return this.name; //위에 name이 있기에 this를 사용함. 로컬변수가 위에 있으므로 멤버변수를 사용하기 위해서 this사용.
}
String getSuperName() {
String name = "김철수";
return super.name;
}
}
public class ThisSuperTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Sub sub = new Sub();
System.out.println("sub.getLocalName = " + sub.getLoclaName() );
System.out.println("sub.getThisName = " + sub.getThisName() );
System.out.println("sub.getSuperName = " + sub.getSuperName() );
}
}
|
cs |