Day 9(8/11) extends, @Override, frame
2016. 8. 16. 10:40ㆍProgramming/Java
오늘 배운 것
1) 상속(extends), @Override(재정의)
2) 프레임 만들기, x 버튼 작동시키기
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 |
//조상, 부모, 자식 class
class Ancestor{}
class Parents extends Ancestor{
String name = "부모";
String p_name = "p부모";
public void p_method(){
System.out.println(name+"함수 호출");
}
}
class Child extends Parents{
String name = "자식";
public void c_method(){
System.out.println(name+"함수 호출");
}
@Override // 부모 또는 조상의 함수를 재정의(하극상?)
public void p_method(){
System.out.println(name+"함수에서 ");
super.p_method();
}
}
public class L01Extends {
public static void main(String[] args) {
Child c = new Child();
c.c_method();
c.p_method(); //상속 받아서 쓸 수 있음
System.out.println(c.name); //부모의 name이 자식의 필드로 덮여짐
System.out.println(c.p_name); //상속 받아 부모 자원 사용 가능
}//main
}//class |
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
49
50
51
52
53
54
55
56 |
//Cat, Dog가 Animal을 상속
import sun.applet.Main;
class Animal{
String name, food;
public Animal(String name, String food){
this.name = name;//this 통해 필드에 매개변수(parameter) 저장할 수 있게
this.food = food;
}
public void eat(){
System.out.println(name+"이(가)"+food+"을(를) 먹는다.");
}
public void walk(){
System.out.println(name+"이(가)"+"걷는다.");
}
}
class Cat extends Animal{
String catWalk = "살금살금";
public Cat(String name, String food){
super(name, food); //부모의 생성자 호출
}//마우스 우클릭 -> Source -> Override/Implements -> Animal.walk();
@Override //왜 Override가 필요?
public void walk() {
System.out.print(catWalk);
super.walk();//그냥 두면 재정의, 삭제하면 초기화
}
}
class Dog extends Animal{
String dogWalk = "터벅터벅";
public Dog(String name, String food){
super(name, food);
}
@Override
public void walk() {
System.out.print(dogWalk);
super.walk();
}
}
public class L02Animal {
public void main(String[] args){
Cat cat = new Cat("페르시안 고양이", "닭가슴살");
cat.eat();
cat.walk();
Dog dog = new Dog("골든 리트리버", "사료");
dog.eat();
dog.walk();
}
} |
cs |
//프로그래머가 하나님
//동물 클래스 (Animal)
//name, food
//eat(), walk(), breath()
//Animal 클래스를 이용해 다른 동물들을 쉽게 만들 계획
//Animal을 이용해서 고양이와 강아지를 만들어보자.
//system.out.println("살금살금 걷는다."); -> 동작
//고양이는 페르시안, 살금살금 walk, 닭가슴살 eat
//강아지는 골든리트리버, 터벅터벅 walk, 혀를 낼름 breath, 사료 eat
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 |
import java.awt.event.*;
import java.awt.Frame;
import java.awt.Color;
import java.awt.Font;
import java.awt.Panel;
import java.awt.Label;
class FrameDemo extends Frame{
Panel p;
Label l1;
Label l2;
Label l3;
public FrameDemo(String frameName){ //생성자 = 설계도?
super(frameName);
p = new Panel();
p.setBackground(Color.LIGHT_GRAY); //static은 객체 생성 없이, JVM 작동할 때 바로 호출 가능
l1 = new Label("1.Label");
l2 = new Label("2.Label", Label.CENTER);
l3 = new Label("3.Label", Label.RIGHT);
l1.setBackground(Color.blue);
l2.setBackground(Color.DARK_GRAY);
l3.setBackground(Color.GREEN);
//font 바꾸기, 글씨 색 바꾸기
l1.setFont(new Font("",Font.BOLD,60));
l2.setForeground(Color.red);
//frame x 버튼이 작동하도록
p.add(l1);
p.add(l2);
p.add(l3);
this.add(p);
this.setSize(600,400);
super.setVisible(true); //this는 자기자신, super는 슈퍼 클래스 지정
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}); //괄호 형태 주의
}//생성자 end
}//class end
public class L03FrameEx {
public static void main(String[] args) {
new FrameDemo("첫번째 frame");
}//main
}//class |
cs |
'Programming > Java' 카테고리의 다른 글
Day 11(8/15) Object, hashCode (0) | 2016.08.23 |
---|---|
Day 10(8/12) Abstract, Interface (0) | 2016.08.16 |
Day 8(8/10) Review (Constructor, Instance) (0) | 2016.08.16 |
Day 7(8/9) MainMethod (0) | 2016.08.09 |
Day 6(8/8) DoWhile, BreakLoop, Method, Instance (0) | 2016.08.08 |