Java 5 的新标准语法和用法详解集锦

时间:2022-03-23 04:41:28

Java 5 的新标准语法和用法详解集锦

Java 5 的新标准语法和用法详解集锦

(需要在首选项-java-complier-compiler compliance level中设置为java5.0以上版本)


1,静态导入

1,静态导入
//如import static java.lang.Math.*;
//导入包或类里面的静态方法. import static java.lang.Math.max; /**
* java5的静态导入
* @author qsuron - 博客:http://blog.csdn.net/qsuron
*/
public class Java5 {
public static void main(String[] args) {
System.out.println(max(1,3));
}
}

2,可变参数的方法:

/**
* java5的可变参数方法
* @author qsuron - 博客:http://blog.csdn.net/qsuron
*/
public class Java5 {
public static void main(String[] args) {
//实现 x*(x1+x2+x3+...+xn)
System.out.println(add(2,2,3));//2*(2+3)=10
System.out.println(add(2,2,3,4,1));//2*(2+3+4+1)=20
}
public static int add(int x,int... args){
int sum = 0;
for(int i=0;i<args.length;i++){
sum += args[i];
}
return sum*x;
}
}

3.新增FOR循环:

/**
* java5的新FOR循环
* @author qsuron - 博客:http://blog.csdn.net/qsuron
*/
public class Java5 {
public static void main(String[] args) {
//实现 x*(x1+x2+x3+...+xn)
System.out.println(add(2,2,3));//2*(2+3)=10
System.out.println(add(2,2,3,4,1));//2*(2+3+4+1)=20
}
public static int add(int x,int... args){
int sum = 0;
for(int i:args){
sum += i;
}
return sum*x;
}
}

4.用普通类模拟实现枚举类型 & 用java 5的枚举类型用法和设计详解

public class Java5 {
public static void main(String[] args) {
Week weekday = Week.MON;
System.out.println(weekday);
System.out.println(weekday.nextDay());
}
} /**
* 用普通类模拟实现枚举类型-星期
* @author qsuron - 博客:http://blog.csdn.net/qsuron
*/
abstract class Week{
private String conf;
private Week(String conf){
this.conf = conf;
} /*建立NextDay()抽象方法,将NextDay方法的一堆if...else...转换成为 由子类去覆盖此方法*/
public abstract Week nextDay(); //建立公开的静态常量子类
public final static Week MON = new Week("星期一"){
public Week nextDay() {
return TUE;
}
};
public final static Week TUE = new Week("星期二"){
public Week nextDay() {
return WED;
}
};
public final static Week WED = new Week("星期三"){
public Week nextDay() {
return THI;
}
};
public final static Week THI = new Week("星期四"){
public Week nextDay() {
return FRI;
}
};
public final static Week FRI = new Week("星期五"){
public Week nextDay() {
return STA;
}
};
public final static Week STA = new Week("星期六"){
public Week nextDay() {
return SUM;
}
};
public final static Week SUM = new Week("星期日"){
public Week nextDay() {
return MON;
}
}; /*重写toString()方法*/
public String toString() {
return this.conf;
} } /**
* 使用JAVA5提供的枚举类型 - 基础用法和基本方法
* @author qsuron - 博客:http://blog.csdn.net/qsuron
*/
public class Java5 {
public static void main(String[] args) {
Week w = Week.MON;
System.out.println("toString - "+w);
System.out.println("与MON比较 - "+w.compareTo(Week.MON));
System.out.println("Nama - "+w.name());
System.out.println("所在顺序 - "+w.ordinal());
System.out.println("枚举数量 - "+Week.values().length);
}
public enum Week{
MON,TUE,WED,THE,FRI,STA,SUM;
}
} /**
* 使用JAVA5提供的枚举类型 - 构造方法
* 加上private String name 和 重新toString方法可以实现中文信息输出
* @author qsuron - 博客:http://blog.csdn.net/qsuron
*/
public class Java5 {
public static void main(String[] args) {
System.out.println("MON(1),TUE(),WED,THE(1),FRI,STA,SUM();");
Week w = Week.MON;
System.out.println(w);
}
public enum Week{
MON(1),TUE(),WED,THE(1),FRI,STA,SUM();
private Week(){
System.out.println("空构造函数");
}
private Week(int i){
System.out.println("带参数构造函数");
}
}
} /**
* 使用JAVA5提供的枚举类型 - 抽象函数
* 让子类去覆盖抽象方法 - 实现Next()的功能
* @author qsuron - 博客:http://blog.csdn.net/qsuron
*/
public class Java5 {
public static void main(String[] args) {
Week w = Week.MON;
System.out.println(w.next());
}
public enum Week{
//由于麻烦,只写2个星期数
MON{
public Week next(){
return TUE;
}
},
TUE{
public Week next(){
return MON;
}
};
public abstract Week next();
}
}


转载请注明出处:blog.csdn.net/qsuron 博主qsuron 小树