java를 java답게 코딩하기? 재사용성과 이식성을 생각하며 코딩 전개하고, 클래스 간 결합도는 줄이고, 코드양은 늘어도 복잡도는 늘지 않게 코딩한다.
생성자는? 전처리의 역할을 하고, 초기화, 재정의 등의 역할을 한다.
원시형/참조형
public static void main(String[] args) {
//원시형 1차배열
int is[] = new int[3];
is[0] = 1; //재정의
is[1] = 2;
is[2] = 3;
System.out.println(is[0]);
System.out.println(is[1]);
System.out.println(is[2]);
//참조형 1차배열
DeptVO dVOs[] = new DeptVO[3];
//향상된 for문 출력 & 진행될 때 계속 생성하여 출력
//타입(클래스) 변수명 : 배열명
for(DeptVO dVO : dVOs) {
dVO = new DeptVO();
System.out.println(dVO);
}
//일반적인 출력
System.out.println(dVOs[0]);
System.out.println(dVOs[1]);
System.out.println(dVOs[2]);
}
Java
데이터 처리하기(클래스 설계)
VO 패턴(Value Object)
DTO 패턴(Data Transfer Object)
MVC 패턴
클래스 나누기 (MVC) : 예(계산기/야구게임)
public class M {
//JFrame은
JFrame jf = new JFrame();
JButton jbtn = new JButton("조회22");
JButton jbtn2 = jbtn;
// JButton jbtn2 = new JButton("입력");
public void initDisplay() {
System.out.println(jbtn == jbtn2); //true
jf.setVisible(true);
jf.setSize(300,400);
jf.add("North",jbtn);
jf.add("Center",jbtn2);
}
public static void main(String[] args) {
M m = new M();
m.initDisplay();
}
}
public class JButtonArray implements ActionListener{
//선언부
JFrame jf = new JFrame();
JButton jbtns[] = new JButton[4];
String jbtnsLabels[] = {"one", "two", "three", "four"};
//생성자
public JButtonArray() {
//객체배열 초기화하기, 생성자 첫번째 역할은 전역변수의 초기화를 담당
for(int i=0;i<jbtns.length;i++) {
jbtns[i] = new JButton(jbtnsLabels[i]);
jbtns[i].addActionListener(this); //이 클래스를 참조
}
}
//사용자메소드
public void initDisplay() {
jf.setTitle("객체배열 연습");
jf.setVisible(true);
jf.setSize(500,300);
jf.setLayout(new GridLayout(1,4)); // 1/4로 균등분할하여 레이아웃됨
//for문 사용하여 각 버튼을 레이아웃으로 생상하여 배치함.
for(int i=0;i<jbtns.length;i++) {
jf.add(jbtns[i]);
}
}
//메인메소드
public static void main(String[] args) {
JButtonArray ja = new JButtonArray();
ja.initDisplay();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}