内部类
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");//发送异常,转向catch
t=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();
}
}