package com.week5;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
//1차 객체 배열 연습
public class JButtonArray implements ActionListener{
//선언부
JFrame jf = new JFrame();
JButton jbtns[] = new JButton[4];
String jbtnsLabels[] = {"one","two","three","four"};
//생성자
JButtonArray() {
//객체 배열 초기화 하기 - 생성자는 전변의 초기화 담당함
for(int i=0;i<jbtns.length;i++) {//jbtns.length=4
jbtns[i] = new JButton(jbtnsLabels[i]);
jbtns[i].addActionListener(this);
}
}
//화면처리부
public void initDisplay() {
jf.setLayout(new GridLayout(1,4));
for(int i=0;i<jbtns.length;i++) {
jf.add(jbtns[i]);
}
jf.setTitle("객체 배열 연습1");
jf.setSize(500, 300);
jf.setVisible(true);
}
//메인메소드
public static void main(String[] args) {
JButtonArray ja = new JButtonArray();
ja.initDisplay();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stdub
}
}
(1)
package com.step5;
public class Array21 {
public static void main(String[] args) {
int[][] is = new int[2][3];
is[0] = new int[3];
is[1] = new int[3];
System.out.println(is[0].length);
System.out.println(is[1].length); // 3이다
for(int i =0; i<is.length; i++)
{
for(int j = 0; j<is[0].length; j++)
{
/*
* 1. 값을
*/
System.out.println("is[" + i + "][" + j + "]=" + is[i][j]);
}
}
}
}
---------------------------------------------------------
3
3
is[0][0]=0
is[0][1]=0
is[0][2]=0
is[1][0]=0
is[1][1]=0
is[1][2]=0
부서를 먼저 작업을 해야 -> emp를 작업 가능 & 의존관계
package com.step5;
public class DeptDTO {
private int deptno;
private String dname;
private String loc;
private EmpDTO empdto;
/*
* 부서를 먼저 작업을 해야 -> emp를 작업 가능 & 의존관계
*/
public EmpDTO getEmpDTO()
{
return empdto;
}
public void setEmpDTO()
{
this.empdto =empdto;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
package com.step5;
public class EmpDTO {
private int empni;
private String ename;
private String job;
private int deptno;
}
자바는 단일 상속만 가능하다. 다중 상속은 불가능하다. 다중상속의 단점으르 보완하기 위해 인터페이스가 제공된다.
단 인터페이스는 여러개를 implements 할 수 있다. (추상클래스, 인터페이스는 설계 관점에서 중요하다) - 컨벤션
package com.week5;
//인스턴스화 4: 인터페이스 변수명 = new 구현체클래스();
//인스턴스화 5 : 추상클래스 변수명 = new 구현체클래스();
//4,5모두 다형성을 누릴 수 있다.
public class QuackSimulation {
void methodA(Squeak squeak) {
System.out.println("methodA "+squeak);
}
void methodB(QuackBehavior quackBehavior) {
System.out.println("methodB "+ quackBehavior);
}
public static void main(String[] args) {
//선언부와 생성부에 타입이 다른거 - 권장사항 - 범위가 더 넓은 걸
//사용하면 범위가 더 좁은 여러개의 구현체 클래스를 담을 수 있으니까
//유지보수시에 코드의 수정을 최소화 하는데 역할이 있다.
QuackSimulation qs = new QuackSimulation();
QuackBehavior quackBehavior = new Squeak();
MuteQuack quackBehavior1 = new MuteQuack();
//qs.methodA(quackBehavior1);
if(quackBehavior instanceof Squeak) {
System.out.println("나는 Squak타입이다.");
}
if(quackBehavior instanceof MuteQuack) {
System.out.println("나는 MuteQuack타입이다.");
}
if(quackBehavior instanceof Quack) {
System.out.println("나는 Quack타입이다.");
}
qs.methodB(quackBehavior1);
Quack quackBehavior2 = new Quack();
Squeak quackBehavior3 = new Squeak();
quackBehavior.quack();
}
}
package com.step5;
class Super
{
int i;
Super()
{
System.out.println("Super() 호출");
}
}
class Sub extends Super{
Sub()
{
System.out.println("Sub() 호출");
i = 2;
}
}
public class Extends1 {
public static void main(String[] args) {
Sub sub = new Sub();
/*
* 이렇게 인스턴스화만 해도 부모 클래스의 Super() 생성자를 호출한다.
*/
}
}
-----------------------------------------------------------
Super() 호출
Sub() 호출
package com.step5;
class Super
{
int i;
Super()
{
System.out.println("Super() 호출");
}
void methodA()
{
System.out.println("methodA");
}
void methodA(int i)
{
System.out.println("methodA(int i)"+ i);
}
}
class Sub extends Super{
Sub()
{
System.out.println("Sub() 호출");
i = 2;
methodA(i);
}
@Override
void methodA(int i)
{
System.out.println(i);
}
}
public class Extends1 {
public static void main(String[] args) {
Sub sub = new Sub();
/*
* 이렇게 인스턴스화만 해도 부모 클래스의 Super() 생성자를 호출한다.
*/
}
}
--------------------------------------
Super() 호출
Sub() 호출
Sub methodA(int i) : 2
package com.step5;
class Super
{
int i;
Super()
{
System.out.println("Super() 호출");
}
void methodA()
{
System.out.println("methodA");
}
void methodA(int i)
{
System.out.println("methodA(int i) : "+ i);
}
}
class Sub extends Super{
Sub()
{
System.out.println("Sub() 호출");
i = 2;
methodA(i);
}
@Override
void methodA(int i)
{
super.methodA(30);
System.out.println("Sub methodA(int i) : "+i);
}
}
public class Extends1 {
public static void main(String[] args) {
Sub sub = new Sub();
/*
* 이렇게 인스턴스화만 해도 부모 클래스의 Super() 생성자를 호출한다.
*/
System.out.println();
}
}
-----------------------------------------------------------
Super() 호출
Sub() 호출
methodA(int i) : 30
Sub methodA(int i) : 2
부모가 가진 전변과 메소드 모두는 상속받은 클래스에서 호출이 가능함. 자손을 인스턴스화 하더라도 부모 생성자가 호출된다.
동일한 메소드가 부모와 자손 둘 다 있다면 부모 메소드는 shadow method가 된다.
상속받은 클래스가 더 누릴 수 있다. vs 자손 클래스가 더 누릴 수 있다.
자손 클래스가 더 누릴(변수와 메소드) 수 있다. 여러 계층의 상속관계를 가진 클래스의 경우 가급적 하위 클래스를 상속 받는 것이 더 많은 변수와 메소드를 누릴 수 있다.
package com.step5;
import javax.swing.JFrame;
public class DeptManager extends JFrame{
//선언부
// 생성자
DeptManager()
{
initDisplay();
}
//화면처리부
public void initDisplay()
{
this.setSize(500,400);
this.setVisible(true);
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
DeptManager dm = new DeptManager();
/*
* JFrame를 상속 받고 있기 때문에 부모 클래스인 JFrame 생성자를 먼저 경유하여
* 자식 클래스인 DeptManager를 방문한다. 부모클래스(JFrame/생성자) - 자식클래스(DeptManager/생성자 경유)
*/
}
}
(3) 부모 메소드 활용
package com.step5;
class Super
{
int i;
Super()
{
System.out.println("Super() 호출");
}
void methodB()
{
System.out.println("methodB");
}
void methodA()
{
System.out.println("methodA");
}
void methodA(int i)
{
System.out.println("methodA(int i) : "+ i);
}
}///////////////////////end of Super
class Sub extends Super{
Sub()
{
System.out.println("Sub() 호출");
i = 2;
methodA(i);
methodB();
}
@Override
void methodA(int i)
{
super.methodA(30);
System.out.println("Sub methodA(int i) : "+i);
}
}///////////////////////end of Sub
public class Extends1 {
public static void main(String[] args) {
Sub sub = new Sub();
/*
* 이렇게 인스턴스화만 해도 부모 클래스의 Super() 생성자를 호출한다.
*/
System.out.println("main Extends1 : "+sub.i);
}
}
---------------------
Super() 호출
Sub() 호출
methodA(int i) : 30
Sub methodA(int i) : 2
methodB
main Extends1 : 2
(4) main에서 Sub(자식 클래스) 생성 후 main에서 부모 메소드 실행
package com.step5;
class Super
{
int i;
Super()
{
System.out.println("Super() 호출");
}
void methodB()
{
System.out.println("methodB");
}
void methodA()
{
System.out.println("methodA");
}
void methodA(int i)
{
System.out.println("methodA(int i) : "+ i);
}
}///////////////////////end of Super
class Sub extends Super{
Sub()
{
System.out.println("Sub() 호출");
i = 2;
methodA(i);
methodB();
}
@Override
void methodA(int i)
{
super.methodA(30);
System.out.println("Sub methodA(int i) : "+i);
}
}///////////////////////end of Sub
public class Extends1 {
public static void main(String[] args) {
Sub sub = new Sub();
/*
* 이렇게 인스턴스화만 해도 부모 클래스의 Super() 생성자를 호출한다.
*/
sub.methodB();
System.out.println("main Extends1 : "+sub.i);
}
}
------------------------------------------------------
Super() 호출
Sub() 호출
methodA(int i) : 30
Sub methodA(int i) : 2
methodB
methodB
main Extends1 : 2