可变参数概念
public 【static】【final】返回值类型 方法名称(参数类型 ....变量){//多参数 个数任意
return 返回值
}
System.out.println(add(new int[] {1,2,3}));
System.out.println(add(1,2,3,4,5));
System.out.println(add());//数组不写是0
}
public static int add(int ... data) {
int sum = 0;
for(int x = 0; x< data.length; x++) {
sum += data[x];
}
return sum;
}
foreach输出
增强性for循环
for(数据类型 变量:数组|集合){集合数组的每一个元素 数组 变量是data里的数值
代码
}
实现求和等功能
不考虑下标 不考虑排第几个时使用
静态导入
导入不同包的方法 import导入
直接在后面点静态方法或者
import day_11.math.match.*;
三大新特性
泛型
接收多种类型
1.利用Object
public class Test4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Point point = new Point();
// point.setX(10);
// point.setY(20);
//
// int x = (int) point.getX();
// int y = (int) point.getY();
// point.setX(10.2);
// point.setY(20.2);
// double x = (double) point.getX();
// double y = (double) point.getY();
point.setX("10度");
point.setY("20度");
String x = (String) point.getX();
String y = (String) point.getY();
System.out.println(x+" "+y);
}
}
public class Point {
private Object x;
private Object y;
public Object getX() {
return x;
}
public void setX(Object x) {
this.x = x;
}
public Object getY() {
return y;
}
public void setY(Object y) {
this.y = y;
}
}
2.使用泛型 数据类型统一 避免向下转型
public class Point<T> {
private T x;
private T y;
public T getX() {
return x;
}
public void setX(T x) {
this.x = x;
}
public T getY() {
return y;
}
public void setY(T y) {
this.y = y;
}
}//动态 使用什么类型 T就是什么类型
Point<String> point = new Point<>();
point.setX("10度");
point.setY("20度");
String x = /*(String)*/ point.getX();
String y = /*(String)*/ point.getY();//泛型决定类型 可以不强转
System.out.println(x+" "+y);
}
}
使用泛型解决数据类型统一问题
Point point = new Point<>();
JDK1.5 Point point = new Point();
JDK1.7 Point point = new Point<>(); //可以简化
通配符
泛型<>不是数据类型 不能使用重载 <>前面的是类型
可以接收任何数据类型 又能使用泛型
Object接收
public static void print(Message<?> temp) {//?代表任意类型 向Object转型
public class Test5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Message<String> msg = new Message<String>();
// msg.setM("message");
Message<Integer> msg = new Message<>();
msg.setM(100);
print(msg);
}
// private static void print(Message<Integer> msg) {
// // TODO Auto-generated method stub
// System.out.println("方法重复");
// }
public static void print(Message<?> temp) {//?代表任意类型 向Object转型
System.out.println(temp.getM());
}
}
限制?范围
设置?上限 ? extends 类 public class Message
设置? 下限 super类 public static void print(Message<? super String> temp)
泛型接口部分
public interface Message<T> {
public String echo(T msg);
}
public class MessageImpl<T> implements Message<T> {
@Override
public String echo(T msg) {
// TODO Auto-generated method stub
return "echo"+msg;
}
}
public class Test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Message<String> msg = new MessageImpl<String>();
System.out.println(msg.echo("hello"));
}
}
public class Test6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Integer result[] = get(1,2,3);//方法调用时候根据参数改变类型
for(int temp : result) {
System.out.println(temp);
}
}
public static <T extends Number> T[] get(T ... args) {
return args;
}
}
枚举
简化多例 enum
enum Color{
RED,GREEN,BLUE;
}
for(Color c : Color.values()) {//数组
System.out.println(c.ordinal()+" "+c.name());//ordinal序号 name里面的值名字
}
enum是jdk1.5之后新关键字 都是继承Enum类而已
枚举的其他定义
枚举属于多例模式
枚举中定义的构造方法不能是public 只能是私有化 枚举中定义的对象只能放在第一行
enum Color{
RED("红色"),GREEN("绿色"),BLUE("蓝色");//final static
private String title;
private Color(String title) {
this.title = title;
}
public String toString() {
return this.title;
}
}
接口可以在枚举中使用
抽象方法可以在枚举使用 只不过得实现他的方法
System.out.println(Color.BLUE.toString());
enum Color {
RED("红色") {
@Override
public String getColor() {
// TODO Auto-generated method stub
return this.toString();
}
},GREEN("绿色") {
@Override
public String getColor() {
// TODO Auto-generated method stub
return this.toString();
}
},BLUE("蓝色") {
@Override
public String getColor() {
// TODO Auto-generated method stub
return this.toString();
}
};//final static
private String title;
private Color(String title) {
this.title = title;
}
public String toString() {
return this.title;
}
public abstract String getColor();
}
Annotation 注解 防止拼写方法错误
@Override//检验方法是不是真的覆写
@Override//检验方法是不是真的覆写
public String toString() {
return “aaaa”;
}
@Deprecated //声明过期操作 不会删 不建议使用 方法更新
@Deprecated
public void print() {
}
@SuppressWarnings//压制警告信息
消除黄叹号问题
@SuppressWarnings(“rawtypes”)
Message msg = new Message();
switch 允许的操作类型
1.5之前int char
1.5之后 可以操作enum
1.7之后 可以操作String