728x90
728x170
1. MainActivity.java
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 |
package ...
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Person person01 = new Person("철수", this);
person01.walk(10);
Person person02 = new Person("영희", this);
person02.walk(10);
Person person03 = new Person("민희", this);
person03.walk(10);
Person person04 = person03;
person04.walk(10);
String outName = person04.name;
System.out.println("person04.name : " + outName);
person03.name = "민정";
System.out.println("person04.name : " + outName);
Leg outLeg = person04.leg;
System.out.println("person04.leg : " + outLeg.left);
person03.leg.left = "왼쪽 다리";
System.out.println("person04.leg : " + outLeg.left);
Leg outLeg2 = person04.getLeg();
System.out.println("person04.leg : " + outLeg2.right);
person03.leg.right = "오른쪽 다리";
System.out.println("person04.leg : " + outLeg2.right);
}
}
class Person {
String name;
Leg leg = new Leg();
Activity activity; //*
public Person() { // Default Constructor
}
public Person(String inName, MainActivity inActivity) { //*
name = inName;
activity = inActivity; //*
}
public void walk(int speed) {
Toast.makeText(activity.getApplicationContext(), name + "이(가) " + speed + "km 속도로 걸어갑니다.", Toast.LENGTH_LONG).show(); //*
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setLeg(Leg leg) {
this.leg = leg;
}
public void run(int speed) {
System.out.println(name + "이(가) " + speed + "km 속도로 뛰어갑니다.");
}
public Leg getLeg() {
return leg;
}
}
class Leg {
String left = "왼쪽";
String right = "오른쪽";
} |
cs |
728x90
그리드형(광고전용)
'Programming > Android' 카테고리의 다른 글
[안드로이드] kotlinx.android.synthetic.main.activity_main.* 를 불러올 수 없는 경우 (0) | 2021.04.08 |
---|---|
인터페이스 (Interface) & 추상 클래스(Abstract Class)와 어댑터(Adapter) (0) | 2017.05.09 |
스피너 (Spinner) (0) | 2017.05.06 |
Getter, Setter 함수 (0) | 2017.05.05 |
이벤트 처리 방법 (0) | 2017.05.05 |
상수 (Constant) (0) | 2017.05.03 |
숫자와 문자열을 서로 바꿔주는 메서드 (0) | 2017.05.03 |
색 (Color) (0) | 2017.05.03 |