java jdk1.5的新特性——泛型 ForEach 枚举 可变参数 静态导入 线程并发库 内省

泛型
加泛型的目的
1.数据安全
一般在使用集合的时候,我们都会给它加一些泛型,限制添加数据的类型
2.少写几个类
例如在属性类中添加泛型:

public class Person<T>
{
    private int pid;
    private T age;
    private String name;
}

在使用时再添加类型:

Person <Integer> p=new Person<>();

T默认的类型是Object
基本数据类型不能作为泛型
也可以在泛型中继承接口,使用时必须是接口的实现类:

<T extends List>

表示当前的泛型对象一定是List的实现类或者是List本身

如果将一个泛型当作参数的时候,可以给当前的泛型类设置范围,设置参数类型:

public void sayhello(Person<? extends String> p)

表示传递的参数必须是一个泛型对象,String类型或String类型的子类

Person<? super String> p

表示传递的参数必须是一个泛型对象,String类型或String类型的父类
普通类实现泛型接口,必须将泛型也一起继承
泛型作为方法的时候,如果父类中没有泛型,可以使用?来代替泛型

public void say(List<? extends String> list){
}

泛型作为一个返回值类型

public List<? extends String> getList()
{
    return new ArrayList<String>();
}

ForEach
foreach其实就是迭代器,它可以用来遍历数组和集合(除了map之外)
1.先获取到集合或数组长度
2.判断是否有数据
3.如果存在数据,就将数据取出来

int[ ] i={1,2,3,4,5,6,7,8};
for(int k:i)
{
    System.out.println(k);
}

使用一次,指针就加一
主要用于数组或集合的遍历,是迭代器的简写方式

枚举
枚举就是有一定范围的可选值
枚举是特殊的类
枚举的对象一般都是大写
在枚举的对象后面加分号,后面可以写方法,可以写属性

public enum Color
{
	RED,GREEN,YELLOW;
	private int color;
	public void run()
	{
		System.out.println("helloword");
	}
	public static void main(String[] args)
	{
		Color c=Color.RED;
		Color c2=Color.GREEN;
		Color c3=Color.YELLOW;
	}
}

一旦构造器修改,里面定义的对象必须根据我们的构造器来实例化
枚举会给每一个对象分配一个name和(当前名称)ordinal(序号)
枚举一般使用在switch-case语句中

可变参数
数据类型…变量
表示可以传递任意多个该数据类型的数据
在使用的时候可以当做数组来看待
例如:计算几个数的和

public class VarMethod
{

	public void dosomething(int...one)
	{
		int sum=0;
		for(int a:one)
		{
			sum+=a;
		}
		System.out.println(sum);
	}
	public static void main(String[] args)
	{
		new VarMethod().dosomething(1,2,3);
	}
}

可变参数必须放在最后面

public void dosomething2(int a,int...b)
	{	
                       System.out.println(b[0]);
	}

静态导入
直接导入方法的路径,就可以直接使用该方法,不通过方法名、类名去调用
import static 包名.类名.静态方法
优点:
使用静态导入可以使被导入的所有静态变量和静态方法在当前类直接可见,
使这些静态成员无需再给出它们的类名
缺点:
可读性差

import static java.lang.System.out;
public class StaticImport
{
	public static void main(String[] args)
	{
		out.println("helloworld");
	}

}

线程并发库
线程池:
用来存放并且运行线程的管理工具
所在包:
java.util.concurrent类库下的ExecutorService
Executor是线程池最顶级的接口,线程类的祖宗
Executors 工具类 创建线程池
ExecutorService 线程池的服务员 负责加入线程
用法1:
定义了线程池的大小为2,放5个线程进去,就会先执行2个,再执行2个,再执行1个

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadGroup
{
	public static class MyThread implements Runnable
	{
		public void run()
		{
			for(int i=0;i<50;i++)
			{
				System.out.println(Thread.currentThread().getName()+"="+i);
			}
		}
	}
	public static void main(String[] args)
	{
//		定义线程池,自己去确定它的大小
		ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(2);
		newFixedThreadPool.submit(new MyThread());
		newFixedThreadPool.submit(new MyThread());
		newFixedThreadPool.submit(new MyThread());
		newFixedThreadPool.submit(new MyThread());
		newFixedThreadPool.submit(new MyThread());
//      关闭线程池
		newFixedThreadPool.shutdown();

	}

}

用法2:
使用线程池计算1到100的和

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ThreadGroup2
{
	public static class MyCallAble implements Callable
	{
		public Integer call() throws Exception
		{
			int sum=0;
			for(int i=1;i<=100;i++)
			{
				sum+=i;
			}
			return sum;
		}
	}
	public static void main(String[] args)
	{
//		定义线程池,自己去确定它的大小
		ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(2);
		Future submit = newFixedThreadPool.submit(new MyCallAble());
		Future submit2 = newFixedThreadPool.submit(new MyCallAble());
		Future submit3 = newFixedThreadPool.submit(new MyCallAble());
		Future submit4 = newFixedThreadPool.submit(new MyCallAble());
		Future submit5 = newFixedThreadPool.submit(new MyCallAble());
		try
		{
			System.out.println(submit.get());
			System.out.println(submit2.get());
			System.out.println(submit3.get());
			System.out.println(submit4.get());
			System.out.println(submit5.get());
		}
		catch (InterruptedException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (ExecutionException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//      关闭线程池
		newFixedThreadPool.shutdown();

	}

}

内省
java.beans包下
内省最核心的一个类:Introspector
内省可以获取类的所有属性和方法,但是不能改变这些信息

BeanInfo beanInfo = Introspector.getBeanInfo(p.getClass());
			PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
		    for(PropertyDescriptor property:propertyDescriptors)
		    {
		    	Method writeMethod = property.getWriteMethod();
		        System.out.println(writeMethod);
		    }
		    MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors();
		    for(MethodDescriptor md:methodDescriptors)
		    {
		    	System.out.println(md.getName());
		    }

内省一般用于后台的架构,例如xml解析
xml可以存放不规则并且少量的数据
XML的解析技术:
DOM4J 是一个第三方的jar包 使用频率非常高
核心类:SAXReader

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值