By GokiSoft.com| 18:32 06/05/2024|
Java Basic

[Share Code] Tìm hiểu Exception & Lambda trong Java - C2307L

#Test01.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package lesson09;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author diepvan
 */
public class Test01 {
    public static void main(String[] args) {
        //Cach tao object tu interface -> vua hoc
        IRunning r1 = new IRunning() {
            @Override
            public void onRunning() {
                System.out.println("Toi dang chay ...");
            }
        };
        r1.onRunning();
        
        //Hoc cach toi uu code
        IRunning r2 = () -> {
            System.out.println("Toi dang chay ...");
            System.out.println("Toi dang chay ...");
            System.out.println("Toi dang chay ...");
            System.out.println("Toi dang chay ...");
            System.out.println("Toi dang chay ...");
        };
        r2.onRunning();
        
        
        IRunning r3 = () -> System.out.println("Toi dang chay ...");
        r3.onRunning();
        
        //Cach 1
        IMessage m1 = new IMessage() {
            @Override
            public void showMsg(String msg) {
                System.out.println("Hello > " + msg);
            }
        };
        m1.showMsg("sfsfdf");
        
        //Cach 2
        IMessage m2 = (String msg) -> {
            System.out.println("Hello > " + msg);
            System.out.println("Hello > " + msg);
            System.out.println("Hello > " + msg);
            System.out.println("Hello > " + msg);
            System.out.println("Hello > " + msg);
        };
        m2.showMsg("sfsfdf");
        
        //Cach 3 -> Chi 1 dong code trong showMsg
        IMessage m3 = (String msg) -> System.out.println("Hello > " + msg);
        m2.showMsg("sfsfdf");
        
        //Cach 4
        IMessage m4 = (msg) -> {
            System.out.println("Hello > " + msg);
            System.out.println("Hello > " + msg);
            System.out.println("Hello > " + msg);
            System.out.println("Hello > " + msg);
            System.out.println("Hello > " + msg);
        };
        m4.showMsg("sfsfdf");
        
        IMessage m44 = msg -> {
            System.out.println("Hello > " + msg);
            System.out.println("Hello > " + msg);
            System.out.println("Hello > " + msg);
            System.out.println("Hello > " + msg);
            System.out.println("Hello > " + msg);
        };
        m44.showMsg("sfsfdf");
        
        IMessage2 m5 = (msg, msg2) -> {
            System.out.println("OKOK");
        };
        m5.showMsg("123", "456");
        
        //Loop
        List<String> list = new ArrayList<>();
        list.add("12312");
        list.add("12312");
        list.add("12312");
        list.add("12312");
        list.add("12312");
        
        for (String v : list) {
            System.out.println(v);
        }
        
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        
        list.forEach(v -> {
            System.out.println(v);
        });
        
        //swift -> iOS, Kotlin (Android) -> Su dung cu phap lambda
        //.NET core (SEM3)
        //Java -> plugin (lambda)
    }
    
    static interface IRunning {
        void onRunning();
    }
    
    static interface IMessage {
        void showMsg(String msg);
    }
    
    static interface IMessage2 {
        void showMsg(String msg, String msg2);
    }
}

#Test.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package lesson09;

/**
 *
 * @author diepvan
 */
public class Test {
    public static void main(String[] args) {
        //Khong the tao object tu interface & abstract class
        //Animal a1 = new Animal();
        Tiger t = new Tiger();
        t.showSound();
        
        //Chi dc su dung 1 lan trong khoi code nay thoi -> anonymous class
        Animal a = new Animal() {
            @Override
            public void showSound() {
                System.out.println("Go .. go ..");
            }
        };
        a.showSound();
        
        //Anonymous class
        IRunning r = new IRunning() {
            @Override
            public void running() {
                System.out.println("Running ...");
            }

            @Override
            public void test() {
                System.out.println("Testing ...");
            }
        };
        
        r.running();
        r.test();
    }
    
    static interface IRunning {
        void running();
        void test();
    }
    
    static abstract class Animal {
        public abstract void showSound();
    }
    
    static class Tiger extends Animal implements IRunning{

        @Override
        public void showSound() {
            System.out.println("Ho ... ho...");
        }

        @Override
        public void running() {
        }

        @Override
        public void test() {
        }
        
    }
}

#Main.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package lesson09;

import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author diepvan
 */
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        
        int x, y;
        System.out.println("Nhap x = ");
        x = scan.nextInt();
        System.out.println("Nhap y = ");
        y = scan.nextInt();
        
        //Nen code theo cach nay
        if(y == 0) {
            System.out.println("Loi chia cho 0"); 
        } else {
            float z = x/y; //Crash -> thuc hien phep chia cho 0 -> Exception
            System.out.println("z = " + z);
        }
        
        int[] t = {1, 2, 7, 2, 10};
        System.out.println("t[1] = " + t[1]);
        //Nen code theo cach nay
        if(t.length > 10) {
            System.out.println("t[10] = " + t[10]);//Crash -> Exception -> index
        }
        
        //Exception: Logic
        //Exception -> Tu code (Kiem soat dc), su dung lib (Co cai kiem soat dc, co cai ko), System
        //Xuat hien exception -> crash -> dung lai.
        //TH khi exception xuat hien -> Van muon chuong trinh chay binh thuong
        //Lam nhu the nao???
        //Exception -> lib, system -> rat kho
        try {
            int zz = x/y; //Neu xuat hien exception tai day -> chia cho 0
            //Nhay sang step 2 -> catch
            System.out.println("Ket qua z = " + zz);
            
            System.out.println("t[10] = " + t[10]);
        } catch(ArithmeticException e) {
            //step 2
            System.out.println("ArithmeticException > Loi chia cho 0");
            //Goi toi day -> Biet ma xu ly
        } catch(ArrayIndexOutOfBoundsException e) {
            System.out.println("ArrayIndexOutOfBoundsException > error");
        } catch(Exception e) {
            e.printStackTrace();
            System.out.println("Exception > crash");
        } finally {
            System.out.println("Deu duoc goi");
            //Giai phong du lieu -> giai phong bien (reset du lieu)
        }
        
        
        try {
            Calculator cal = new Calculator();
            int zzz = cal.chia(x, y);
            System.out.println("zzz = " + zzz);
        } catch(CalculatorException e) {
            e.printStackTrace();
        }
        
        Calculator cal2 = new Calculator();
        try {
            cal2.chia(x, y);
        } catch (CalculatorException ex) {
            System.out.println("CalculatorException > error");
        }
    }
    
    static class CalculatorException extends Exception {
        //Phan loai error > exception
    }
    
    static class Calculator {
        public int chia(int x, int y) throws CalculatorException {
            if(y == 0) {
                throw new CalculatorException();
            }
            return x/y;
        }
        
        public int cong(int x, int y) {
            return x + y;
        }
    }
}
Tags:



Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó