28일차 - 2023.2.3

문우림·2023년 2월 3일
0

Java

목록 보기
13/23

1. 2차원 배열

1-1. 2차원 배열의 생성

  • int[][] arr = new int[3][4];
    int[형][열]로 구분된다.
  • 다수의 1차원 배열을 엮어서 구성이 되는 2차원 배열

2차원 배열의 예제

public class ArrayDouble {

	public static void main(String[] args) {
		int[][] arr = new int[3][4];
		int num = 1;

		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 4; j++) {
				arr[i][j] = num;
				num++;
			}
		}

		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 4; j++) {
				System.out.println(arr[i][j] + "\t");
			}
			System.out.println();
		}
	}

}

1-2. 2차원 배열의 초기화 예

public class ArrayDouble2 {

	public static void main(String[] args) {
		int[][] arr = { { 11 }, { 22, 33 }, { 44, 55, 66 } };

		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr[i].length; j++) {
				System.out.print(arr[i][j] + "\t");
			}
			System.out.println();
		}

	}

}

2. 상속

2-1. 상속의 가장 기본적인 특성

  • 부모 클래스의 멤버나 메소드를 자식 클래스에게 물려줄 수 있다.(부모 클래스 활용)
  • class 자식 클래스이름 extends 부모 클래스이름
  • 자바에서는 다중다속❌ ➡ extends뒤에 단 하나의 부모 클래스만 올 수 있다.
  • 부모 클래스의 생성자가 만들어지는 순간 메모리에 먼저 올라간다.
class Man {
	String name;

	public void tellYourname() {
		System.out.println("My name is " + name);
	}
}

class BusinessMan extends Man {
	String company;
	String position;

	public BusinessMan(String name, String company, String position) {
		this.name = name;
		this.company = company;
		this.position = position;
	}

	public void tellYourInfo() {
		System.out.println("My company is " + company);
		System.out.println("My position is " + position);
		tellYourname();
	}
}

public class Inheritance {

	public static void main(String[] args) {
		BusinessMan man = new BusinessMan("YOON", "Hybrid ELD", "Staff Eng.");

		man.tellYourInfo();

	}

}

부모 클래스 함수, 메소드 가져와서 사용할 때는 this. 대신 super. 을 붙여도 된다.

public BusinessMan(String name, String company, String position) {
		super.name = name;
		this.company = company;
		this.position = position;
	}

부모 클래스에서 부모 생성자를 생성하고 멤버 변수 초기화시키면 super( )를 사용해 호출.

public BusinessMan(String name, String company, String position) {
		super(name);
		this.company = company;
		this.position = position;
	}

2-1. 생성자 호출 관계 파악

class SuperCLS {
	public SuperCLS() {
		System.out.println("I'm Super Class");
	}
}

class SubCLS extends SuperCLS {
	public SubCLS() {
		System.out.println("I'm Sub Class");
	}
}

public class Inheritance2 {

	public static void main(String[] args) {

		new SubCLS();

	}

}

상속 예제
다음 TV 클래스를 출력

class TV{
   private int size;
   public TV(int size) { this.size = size; }
   protected int getSize() { return size; }
}

[1] 다음 main() 메소드와 실행 결과를 참고하여 
TV를 상속받은 ColorTV 클래스를 작성하라.

public static void main(String[] args) {
   ColorTV myTV = new ColorTV(32, 1024);
   myTV.printProperty();
}
[결과]32인치 1024컬러
class TV {
	int size;

	public TV(int size) {
		this.size = size;
	}

	protected int getSize() {
		return size;
	}
}

class ColorTV extends TV {
	int color;

	public ColorTV(int size, int color) {
		super(size);
		this.color = color;
	}

	public void printProperty() {
		System.out.println(super.getSize() + "인치 " + this.color + "컬러");
	}
}

public class TVTest {

	public static void main(String[] args) {
		ColorTV myTV = new ColorTV(32, 1024);
		myTV.printProperty();

	}

}
  • protected : 같은 패키지 혹은 다른 패키지라도 상속받은 클래스에서 접근 가능.

2. 자바 컴파일러가 공짜로 해주는 거 2가지

디폴트 생성자
생성자가 하나도 없으면 컴파일러가 자동으로 (디폴트)생성자를 만들어준다.
super( )
자식 클래스 생성자에서 부모 클래스의 생성자를 호출하기 위해서 사용된다.

2-1. 컴파일러가 넣어주는 기능의 예제

class A{
}
class B extends A{
}
B b = new B(); 했을시 컴파일러가 넣는 코드를 완성하시오.

class A {
	public A() {
		System.out.println("A 클래스");
	}
}

class B extends A {
	public B() {
		super();
		System.out.println("B 클래스");
	}
}

public class Test {

	public static void main(String[] args) {
		B b = new B();

	}

}
  • public A( ){ } = 디폴트 생성자를 컴파일러가 자동으로 생성해줌.
    자식 클래스를 호출할 때 반드시 상속된 부모 클래스도 (먼저)호출이 되기때문에 부모 클래스 생성자가 없으면 자동으로 만들어준다.

  • super( )
    상속한 부모 클래스의 생성자를 사용하기 위해 컴파일러가 자동으로 만들어줌.

3. 상속을 UML로 표기하시오.

0개의 댓글