匿名内部类使用,例:
public class Demo {
public static void main(String[] args) {
RichMan rm = new RichMan();
rm.marry(new IWRB() {
@Override
public void white() {
System.out.println("beauty");
}
@Override
public void rich() {
System.out.println("rich");
}
@Override
public void beauty() {
System.out.println("white");
}
});
}
}
interface IWhite {
void white();
}
interface IRich {
void rich();
}
interface IBeauty {
void beauty();
}
interface IWRB extends IBeauty, IRich, IWhite {
}
class RichMan {
public void marry(IWRB iwrb) {
iwrb.beauty();
iwrb.rich();
iwrb.white();
System.out.println("-------");
}
}自定义异常,例:
public class Triangle {
private int a;
private int b;
private int c;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
public int getC() {
return c;
}
public void setC(int c) {
this.c = c;
}
public Triangle(int a, int b, int c) throws TriangleException{
super();
if (a + b <= c || a + c <= b || b + c <= a) {
throw new TriangleException("两边之和必须大于第三边!");
}
this.a = a;
this.b = b;
this.c = c;
}
}public class TriangleException extends RuntimeException {
private String info;
public TriangleException(String info) {
super(info);
this.info = info;
}
public TriangleException() {
super();
}
}public class Person {
private int age;
private String name;
private Birthday birthday;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Birthday getBirthday() {
return birthday;
}
public void setBirthday(Birthday birthday) {
this.birthday = birthday;
}
}
class Birthday {
private int ad;
private int year;
private int month;
private int day;
public int getAd() {
return ad;
}
public void setAd(int ad) {
this.ad = ad;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public Birthday(int year, int month, int day) throws Exception {
super();
Tool.legalBirthday(year, month, day);
this.year = year;
this.month = month;
this.day = day;
}
}
class Tool {
public static boolean legalBirthday(int year,int month,int day) throws IllegalYearException, IllegalMonthException, IllegalDayException{
return legalDay(year, month, day);
}
public static boolean leapYear(int year) throws IllegalYearException {
if (year < 0) {
throw new IllegalYearException("不合法的年份");
}
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return true;
}
return false;
}
public static boolean legalMonth(int month) throws IllegalMonthException {
if (month < 1 || month > 12) {
throw new IllegalMonthException("不合法的月份");
}
return true;
}
public static boolean legalDay(int year, int month, int day) throws IllegalYearException,
IllegalMonthException, IllegalDayException {
int m = maxDay(year, month);
if (day > 0 && day <= m) {
return true;
}
throw new IllegalDayException("不合法的天数");
}
private static int maxDay(int year, int month) throws IllegalYearException, IllegalMonthException {
boolean f = leapYear(year);
legalMonth(month);
int max = 0;
int[] leapMonth = { 1, 3, 5, 7, 8, 10, 12 };
if (Arrays.binarySearch(leapMonth, month) < 0) {
if (month == 2) {
if (f) {
max = 29;
} else {
max = 28;
}
} else {
max = 30;
}
} else {
max = 31;
}
return max;
}
}public class IllegalYearException extends Exception{
/**
*
*/
private static final long serialVersionUID = 1L;
public IllegalYearException(){
super();
};
public IllegalYearException(String info){
super(info);
}
}
class IllegalMonthException extends Exception{
/**
*
*/
private static final long serialVersionUID = 1L;
public IllegalMonthException(){
super();
};
public IllegalMonthException(String info){
super(info);
}
}
class IllegalDayException extends Exception{
/**
*
*/
private static final long serialVersionUID = 1L;
public IllegalDayException(){
super();
};
public IllegalDayException(String info){
super(info);
}
}原文地址:http://11312010.blog.51cto.com/11302010/1772519