Day 28 (23.02.03)

Jane·2023년 2월 3일
0

IT 수업 정리

목록 보기
29/124

1. 다차원 배열

1-1. 형태

  • int[][] arr = new int[3][4];
    [행][열] 순으로 적는다.

int[0][0] int[0][1] int[0][2] int[0][3]
int[1][0] int[1][1] int[1][2] int[1][3]
int[2][0] int[2][1] int[2][2] int[2][3]


public class JavaPractice {

	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.print(arr[i][j] + "\t");
			}
			System.out.println();
		}

	}

}

[Console]

1-2. 2차원 배열의 실제 구조

1-3. 초기화

public class JavaPractice {

	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();
		}

	}

}

[Console]

2. 클래스의 상속

2-1. extends 클래스이름

  • extends를 붙여서 상속을 한다.
  • extends를 통해 상속받은 클래스(자식 클래스)는
    extends한 클래스(부모 클래스)의 변수와 함수를 끌어다 쓸 수 있다.
  • 부모 클래스에 접근할 때에는 this도 되고, super로도 접근 가능하다.
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;
        // super.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 JavaPractice {

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

		man.tellYourInfo();

	}

}

[Console]
My company is Hybrid ELD
My position is Staff Eng.
My name is YOON

2-2. 클래스의 이름

  • class Man : 부모 클래스, 상위 클래스, 기초 클래스
  • class BusinessMan : 자식 클래스, 하위 클래스, 유도 클래스

2-3. 부모 생성자도 만들고 자식 생성자도 만들려면?

  • 부모 생성자를 만든다.
  • 자식 생성자에서는 super(멤버변수)로 호출한다.
class Man {
	String name;

	Man(String name){
		this.name = name;
	}
}

class BusinessMan extends Man {
	String company;
	String position;

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

2-4. 상속 관계의 생성자 호출

  • 자식 클래스의 생성자 안에는 컴파일러가 super()를 만들어준다.
  • 그러므로 상속 관계가 선언이 되어 있다면 부모 클래스의 것을 먼저 생성한 후, 자식 클래스를 생성한다.
class SuperClass {
	public SuperClass() {
		System.out.println("Super Class");
	}
}

class SubClass extends SuperClass {
	public SubClass() {
		System.out.println("Sub Class");
	}
}

public class JavaPractice {

	public static void main(String[] args) {
		new SubClass();

	}

}

2-5. 상속 관계와 컴파일러

  • 컴파일러는 각각 클래스의 생성자와, 자식 클래스 안에 super()를 생성해준다.

이 코드는 컴파일러가 넣어준 것이 아닌, 개발자가 넣어준 코드이다.

class A {
	public A() {
	}
}

class B extends A {
	public B() {
		super();
	}

}

public class JavaPractice {

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

	}

}

3. 예제 : TV를 상속받은 ColorTV

class TV {
	private int size;

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

	protected int getSize() {
		return size;
	}
}

class ColorTV extends TV {
	private int color;

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

	public int getColor() {
		return color;
	}

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

}

public class JavaPractice {

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

}

[Console]
32인치 1024 컬러

profile
velog, GitHub, Notion 등에 작업물을 정리하고 있습니다.

0개의 댓글