Javascript의 Object 생성 방법은 세 가지가 있다

  1. new Object()이용
  2. 리터럴 표기법
  3. 프로토타입
  1. new Object()이용
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    <html>
        <head>
            <title></title>
            <meta charset="utf-8">
            <script>
                function deposit(a) {return this.balance += a;}
                function withdraw(a) {return this.balance -= a;}
     
            </script>
        </head>
        <body>
            <h3>new Object()</h3>
            <hr>
            <script>
                var account = new Object();
                account.owner = 'KSD';
                account.code = '1111';
                account.balance = 400000;
                account.deposit = deposit;
                account.withdraw = withdraw;
     
                account.deposit(10000);
                document.write('after deposit balance = ' + account.balance + '<br>');
                account.withdraw(20000);
                document.write('after deposit withdraw = ' + account.balance + '<br>');
            </script>
        </body>
    </html>
    cs
  2. 리터럴 표기법
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    <html>
        <head>
            <title></title>
            <meta charset="utf-8">
        </head>
        <body>
            <h3>new Object()_Literal</h3>
            <hr>
            <script>
                var account = {
                owner : 'KSD',
                code : '1111',
                balance : 400000,
                deposit : function deposit(a) {return this.balance += a;},
                withdraw : function withdraw(a) {return this.balance -= a;}
                }
     
                account.deposit(10000);
                document.write('after deposit balance = ' + account.balance + '<br>');
                account.withdraw(20000);
                document.write('after deposit withdraw = ' + account.balance + '<br>');
            </script>
        </body>
    </html>
    cs
  3. 프로토타입
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    <html>
        <head>
            <title></title>
            <meta charset="utf-8">
        </head>
        <script>
            function Account(owner, code, balance){
     
                this.owner = owner;
                this.code = code;
                this.balance = balance;
                this.deposit = function deposit(a) {return this.balance += a;}
                this.withdraw = function withdraw(a) {return this.balance -= a;}
     
            }
        </script>
        <body>
            <h3>new Object()_Prototype</h3>
            <hr>
            <script>
                var account = new Account('KSD''1111'40000);
            account.deposit(10000);
            document.write('after deposit balance = ' + account.balance + '<br>');
            account.withdraw(20000);
            document.write('after deposit withdraw = ' + account.balance + '<br>');
            </script>
        </body>
    </html>
    cs

     

실행결과 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package exception;
 
public class exception_Num {
    public static void main(String[] args) {
        String[] strNum = {"23""12""3.14""998"};
        
        int i = 0;
        try {
            for(i = 0; i < strNum.length; i++) {
                int j = Integer.parseInt(strNum[i]);
                System.out.println(j);
            }
        }
        catch(NumberFormatException e) {
            System.out.println(strNum[i] + "는 정수로 변환할 수 없습니다");
        }
    }
}
 
cs

NumberFormatException 예외처리를 하는 코드이다. 

Integer.parseInt()라는 함수도 볼 수 있다. String 을 int로 변환하는 코드이다.

 

'Java' 카테고리의 다른 글

Java, String(2)  (0) 2019.10.04
Java, String(1)  (0) 2019.10.04
Java, Array  (0) 2019.09.25
Java, for-while, continue, Scanner  (0) 2019.09.25
Java, switch문  (0) 2019.09.25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
public class Review_Array {
 
    static int[][] makeArray(){
        int intArr[][] = new int[4][];
        for(int x = 0; x < intArr.length; x++) {
            intArr[x] = new int[6-x];
        }
 
        for(int i = 0; i < intArr.length; i++) {
            for(int j = 0; j < intArr[i].length; j++) {
                intArr[i][j] = (i + 1)*10 + j;
            }
        }
        return intArr;
    }
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int intArr[][];
        intArr = makeArray();
        for(int i = 0; i < intArr.length; i++) {
            for(int j = 0; j < intArr[i].length; j++) {
                System.out.print(intArr[i][j] + " ");
            }
            System.out.println();
        }
    }
 
}
cs

method makeArray에서 비정방형 2차원 배열을 생성한뒤 main함수에서 호출하는 코드이다.

 

실행결과 : 

10 11 12 13 14 15 
20 21 22 23 24 
30 31 32 33 
40 41 42 

'Java' 카테고리의 다른 글

Java, String(2)  (0) 2019.10.04
Java, String(1)  (0) 2019.10.04
Java, Interger.parseInt(), 예외처리  (0) 2019.09.25
Java, for-while, continue, Scanner  (0) 2019.09.25
Java, switch문  (0) 2019.09.25

+ Recent posts