内部类与异常类例题
內部類
public class RedCowForm{static String formName;RedCow cow;//內部類聲明對象public RedCowForm() {}RedCowForm(String s){cow=new RedCow(150,112,5000);formName=s;}public void showCowMess() {cow.speak();}class RedCow{ //內部類的聲明String cowName="紅牛";int height,weight,price;public RedCow(int h,int w,int p) {height=h;weight=w;price=p;}void speak(){System.out.println("偶是"+cowName+"身高:"+height+"cm 體重:"+weight+"kg,生活在"+formName);}}//內部類結束 }// 外嵌類結束 public class exma {public static void main(String[] args) {RedCowForm form=new RedCowForm("紅牛農場");form.showCowMess();form.cow.speak();}}匿名類
abstract class OutputAlphabet {public abstract void output(); } public class OutputEnglish extends OutputAlphabet { //輸出英文字母的子類public void output(){for(char c='a';c<='z';c++){System.out.printf("%3c",c);}} } public class ShowBoard {void showMess(OutputAlphabet show){ //參數show是OutputAlphabet類型的對象show.output();} } public class Exam {public static void main(String[] args) {ShowBoard borad=new ShowBoard();borad.showMess(new OutputEnglish());//向參數傳遞OutputAlphabet的子類OutputEnglish對象borad.showMess(new OutputAlphabet() {public void output() { //參數傳遞OutputAlphabet的匿名子類對象for(char c='a';c<='z';c++){System.out.printf("%3c",c);}}});} }和接口有關的匿名類
interface SpeakHello{void speak(); } class HelloMachine{public void turnOn(SpeakHello hello) {hello.speak();} } public class Exam {public static void main(String[] args) {HelloMachine machine=new HelloMachine();machine.turnOn(new SpeakHello() { //和接口SpeakHello有關的匿名類public void speak() {System.out.println("hello,you are welcome!");}});machine.turnOn(new SpeakHello() { //和接口SpeakHello有關的匿名類public void speak() {System.out.println("你好,歡迎光臨");}});} }try-catch語句
import java.util.jar.JarException; public class exam {public static void main(String[] args) {int n=0,m=0,t=1000;try {m=Integer.parseInt("8888");n=Integer.parseInt("ab89");//發送異常,轉向catcht=7777;//t沒有機會被賦值} catch (NumberFormatException e) {System.out.println("發生異常:"+e.getMessage());}System.out.println("n="+n+",m="+m+",t="+t);try {System.out.println("故意拋出I/O異常!");throw new java.io.IOException("我是故意的"); //故意拋出異常//System.out.println("這個輸出語句肯定沒有機會執行,必須注釋,否則編譯出錯");} catch(java.io.IOException e) {System.out.println("發生異常:"+e.getMessage());}} }public class BankException extends Exception {String message;public BankException(int m,int n) {message="入賬資金"+m+"是負數或支出"+n+"是正數,不符合系統要求";}public String warnMess() {return message; } } public 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.printf("本次計算出得純收入是:%d\n",netIncome);money=money+netIncome;}public int getMoney() {return money;} } public class Exam {public static void main(String[] args) {Bank bank=new Bank();try {bank.income(200, -100);bank.income(300, -100);bank.income(400, -100);System.out.printf("銀行目前有%d元\n",bank.getMoney());bank.income(200,100);bank.income(99999,-100);} catch (BankException e) {System.out.println("計算收益得過程出現如下問題");System.out.println(e.warnMess());}System.out.printf("銀行目前有%d元\n",bank.getMoney());}}斷言
import java.util.*; public class exam {public static void main(String[] args) {int [] score={-120,98,89,120,99};int sum=0;for(int number:score){assert number>=0:"負數不能是成績";sum=sum+number;}System.out.println("總成績:"+sum);} }
應用舉例
public class DangerException extends Exception {final String message="超重";public String warnMess() {return message; } } public class CargoBoat {int realContent; //裝載的重量int maxContent; //最大裝載量public void setMaxContent(int c) {maxContent=c;}public void loading(int m)throws DangerException {realContent+=m;if(realContent>maxContent){realContent-=m;throw new DangerException();}System.out.println("目前裝載了"+realContent+"噸貨物");} } public class Exam {public static void main(String[] args) {CargoBoat ship=new CargoBoat();ship.setMaxContent(1000);int m=600;try {ship.loading(m);m=400;ship.loading(m);m=347;ship.loading(m);m=555;ship.loading(m);} catch (DangerException e) {System.out.println(e.warnMess());System.out.println("無法再裝載重量是"+m+"噸的集裝箱");}finally{System.out.println("貨物將正點啟航");}}}習題
class Cry{public void cry() {System.out.println("大家好");} } public class E {public static void main(String[] args) {Cry hello=new Cry(){public void cry(){System.out.println("大家好,祝工作順利!");}};hello.cry();}} interface Com{public void speak(); } public class E {public static void main(String[] args) {Com p=new Com(){public void speak(){System.out.println("p是接口變量");}};p.speak();}} import java.io.IOException;import org.omg.CORBA.PUBLIC_MEMBER; public class E {public static void main(String[] args) {try{methodA();}catch(IOException e){System.out.println("你好");return ;}finally{System.out.println(" fine thanks");}}public static Void methodA() throws IOException{throw new IOException();} } class RedCowForm{static class RedCow{ //靜態內部類是外嵌類中的一種靜態數據類型void speak(){System.out.println("我是紅牛");}} } public class E {public static void main(String[] args) {RedCowForm.RedCow redCow= new RedCowForm.RedCow();//如果RedCow不是靜態類此代碼非法redCow.speak();}}總結
- 上一篇: Netty第二章 2020 3-9 Ne
- 下一篇: 温暖的奢侈,当手机遇到机器人