int[][] arr = new int[3][4];
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(); } } }
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();
}
}
}
class 자식 클래스이름 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; }
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();
}
}
✔디폴트 생성자
생성자가 하나도 없으면 컴파일러가 자동으로 (디폴트)생성자를 만들어준다.
✔super( )
자식 클래스 생성자에서 부모 클래스의 생성자를 호출하기 위해서 사용된다.
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( )
상속한 부모 클래스의 생성자를 사용하기 위해 컴파일러가 자동으로 만들어줌.