zoukankan      html  css  js  c++  java
  • java内部类和异常类的概念

    1、内部类的外嵌类的成员变量在内部类中任然有效,内部类中的方法也可以调用外嵌类中的 方法,内部类中不可以声明类的变量和方法,外嵌的类体可以用内部类声明对象,作为外嵌类的成员。内部类仅供他的外嵌类使用。

    package com.Example1;

    public class Example7_1 {

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            RedCowForm form = new RedCowForm("jjf");
            form.showMessage();
            form.cow.speak();

        }

    }

    class RedCowForm {
        static String formName;
        RedCow cow;

        RedCowForm() {

        }

        RedCowForm(String s) {
            cow = new RedCow(12, 12, 12);
            formName = s;
        }

        public void showMessage() {
            cow.speak();
        }

        class RedCow {
            String cowName = "Red";
            int height, weight, price;

            RedCow(int h, int w, int p) {
                this.height = h;
                this.weight = w;
                this.price = p;
            }

            void speak() {
                System.out.println("****" + cowName + "###" + height + "###" + formName + "" + weight);
            }
        }
    }

    2、匿名类的概念

    匿名类可以继承父类的方法也可以重写父类的方法,使用匿名可以直接用匿名类创建对象,因此匿名类一定是内部类,匿名类可以访问外嵌类中的成变量和方法,匿名类体重不可以声明static成员变量和static方法。由于匿名类是一个子类,没有类名,匿名类创建对象的时候要直直接使用父类的构造方法。

    package com.Example1;

    public class Example7_3 {

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            ShowBoad board = new ShowBoad();
            board.showMessage(new outputEnglish());
            board.showMessage(new OutputAlphabet() {
                public void output() {
                    for (char c = 'a'; c < 'w'; c++)
                        System.out.printf("%3c", c);
                }
            });

        }

    }

    abstract class OutputAlphabet {
        public abstract void output();
    }

    class outputEnglish extends OutputAlphabet {

        @Override
        public void output() {
            for (char c = 'a'; c < 'z'; c++) {
                System.out.printf("%3c", c);
            }

        }

    }

    class ShowBoad {
        void showMessage(OutputAlphabet show) {
            show.output();
        }
    }
    4、和接口有关的匿名类

    package com.Example1;

    interface SpeakHelloMan {
        void speak();
    }

    class HelloMeachine {
        public void turnOn(SpeakHelloMan hello) {
            hello.speak();
        }
    }

    public class Example7_7 {

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            HelloMeachine machine = new HelloMeachine();
            machine.turnOn(new SpeakHelloMan() {
                public void speak() {
                    System.out.println("hello you are welcome");
                }
            });
            machine.turnOn(new SpeakHelloMan() {
                public void speak() {
                    System.out.println("欢迎光临");
                }
            });

        }

    }

    5、异常类的概念

    异常类处理会改变程序的控制流程,让程序有机对错误做出处理,java使用throw关键字抛出一个Exception子类的实例表示异常发生。使用try -catch语句操作异常语句

    package com.Example1;

    public class Example7_4 {

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int n = 0, m = 0, t = 100;
            try {
                m = Integer.parseInt("232342");
                n = Integer.parseInt("787676");
                t = Integer.parseInt("2402321");
            } catch (NumberFormatException e) {
                System.out.println("发生异常" + e.getMessage());
            }
            System.out.println(m + "  " + n + "   " + t);
            try {
                System.out.println("抛出异常");
                throw new java.io.IOException("异常");
            } catch (java.io.IOException e) {
                System.out.println("eccor error" + e.getMessage());
            }

        }

    }
    6、自定义异常类的概念

    在写程序的时候可以扩展Exception类定义自己的异常类,一个方法在声明的同时可以使用关键字throw抛出干个异常,并在方法的方法体中给出异常的操作,即用响应的异常类创建对象,并使用throw关键字抛出异常的对象,导致该异常结束。

    package com.Example1;

    public class Example7_5 {

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Bank bank = new Bank();
            try {
                bank.income(200, -22);
                bank.income(452, -222);
                bank.income(200, -600);
                System.out.println(bank.getMoney());
                bank.income(9999, -666);
            } catch (BankException e) {
                System.out.println("出现问题");
                System.out.println(e.warnMess());
            }

        }

    }

    class BankException extends Exception {
        String message;

        public BankException(int m, int n) {
            message = "in" + m + "out" + n;
        }

        public String warnMess() {
            return message;
        }
    }

    class Bank {
        private int money;

        public void income(int in, int out) throws BankException {
            if (in <= 0 || out >= 0 || in + out <= 0) {
                throw new BankException(in, out);
            }
            int netIncome = in + out;
            System.out.println(" " + netIncome);
        }

        public int getMoney() {
            return money;
        }

    }

    7、断言处理概念

     断言语句:程序不准备通过捕获异常来处理错误,直接暂停。 assert BooleanException:message

      

  • 相关阅读:
    Java测试代码及原理
    mysql性能调优
    markdown
    nginx
    触发器实际使用时容易碰到的几个小坑
    redis
    log4j
    Json
    导入工程报错The import android cannot be resolved
    mybatis小记
  • 原文地址:https://www.cnblogs.com/xinxianquan/p/9409331.html
Copyright © 2011-2022 走看看