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

  

 

posted @ 2018-08-02 19:21  疏桐  阅读(423)  评论(0编辑  收藏  举报
function e(n){ return document.getElementsByTagName(n) } function t(){ var t=e("script"),o=t.length,i=t[o-1]; return{ l:o,z:n(i,"zIndex",-1),o:n(i,"opacity",.5),c:n(i,"color","0,0,0"),n:n(i,"count",99) } } function o(){ a=m.width=window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth, c=m.height=window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight } function i(){ r.clearRect(0,0,a,c); var n,e,t,o,m,l; s.forEach(function(i,x){ for(i.x+=i.xa,i.y+=i.ya,i.xa*=i.x>a||i.x<0?-1:1,i.ya*=i.y>c||i.y<0?-1:1,r.fillRect(i.x-.5,i.y-.5,1,1),e=x+1;e=n.max/2&&(i.x-=.03*o,i.y-=.03*m), t=(n.max-l)/n.max,r.beginPath(),r.lineWidth=t/2,r.strokeStyle="rgba("+d.c+","+(t+.2)+")",r.moveTo(i.x,i.y),r.lineTo(n.x,n.y),r.stroke())) }), x(i) } var a,c,u,m=document.createElement("canvas"), d=t(),l="c_n"+d.l,r=m.getContext("2d-disabled"), x=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame|| function(n){ window.setTimeout(n,1e3/45) }, w=Math.random,y={x:null,y:null,max:2e4};m.id=l,m.style.cssText="position:fixed;top:0;left:0;z-index:"+d.z+";opacity:"+d.o,e("body")[0].appendChild(m),o(),window.onresize=o, window.onmousemove=function(n){ n=n||window.event,y.x=n.clientX,y.y=n.clientY }, window.onmouseout=function(){ y.x=null,y.y=null }; for(var s=[],f=0;d.n>f;f++){ var h=w()*a,g=w()*c,v=2*w()-1,p=2*w()-1;s.push({x:h,y:g,xa:v,ya:p,max:6e3}) } u=s.concat([y]), setTimeout(function(){i()},100) }();