<4>第四期

五、**java常用类
1.java日期处理类**

1.Date类:Date类是jdk给我们提高的标准日期类,在java.util包下;

2.Calendar类:Calendar是日历类,也是java.util包下的,功能比较强大,能获取到年月日时分秒的具体值;

import java.util.Calendar;
import java.util.Date;

public class Test {
	public static void main(String[] args) {
		// date时间类
		Date date=new Date();
		System.out.println("现在时间"+date);
		// Calendar 类
		Calendar  calendar=Calendar.getInstance();
		
		System.out.println(calendar.get(Calendar.YEAR));
        System.out.println(calendar.get(Calendar.MONTH)+1); // 月份从0开始 要+1
     
        System.out.println("现在是:"+calendar.get(Calendar.YEAR)+"年"
                +(calendar.get(Calendar.MONTH)+1)+"月"
                +calendar.get(Calendar.DAY_OF_MONTH)+"日"
                +calendar.get(Calendar.HOUR_OF_DAY)+"时"
                +calendar.get(Calendar.MINUTE)+"分"
                +calendar.get(Calendar.SECOND)+"秒");
	}
}

3.SimpleDateFormat类:SimpleDateFormat类主要是用作日期类型转换用的,在java.text包

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDateFormat {
	/**
	 * 
	 * @param date 传入的日期对象
	 * @param format    转化的格式
	 * @return
	 */
	//1.封装方法  ,把Date类型转化为指定类型的字符串(对象  格式)
	public static String formatDate(Date date ,String format){
		String result="";//定义一个返回的字符串 
		// 实列化需要返转化的对象
		SimpleDateFormat sdf=new SimpleDateFormat(format);
		if(date!=null){
			result=sdf.format(date);
		}
		return result;
	}
	 /**
     * 将日期字符串转换成一个日期对象 
     * @param dateStr 日期字符串
     * @param format 格式
     * @return
     * @throws ParseException 
     */
    public static Date formatDate(String dateStr,String format) throws ParseException{
        SimpleDateFormat sdf=new SimpleDateFormat(format);
        return sdf.parse(dateStr);
    }
	
	public static void main(String[] args) throws ParseException {
		// 把日期对象转换成日期字符串
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat();
		String cs=sdf.format(date);
	    System.out.println(cs);
	    
	    //测试:把Date类型转化为指定类型的字符串(对象  格式)
		System.out.print(formatDate(date,"yyyy-mm-dd HH:MM:ss   "));
		
		//测试:将日期字符串转换成一个日期对象  注意:在这里转换的时候,需要前后的类型一样
		String dataStr="2022年3月20日 19:06:27";
		Date date2=formatDate(dataStr,"yyyy年mm月dd日 HH:MM:ss");
		System.out.println(formatDate(date2, dataStr));
		}
}

2.java常用类-String VS StringBuffer

String:对String类型的对象操作,等同于重新生成一个新对象,然后讲引用指向它;

public class TestString {
 
    public static void main(String[] args) {
        		//前者str与后者str在堆区的指向不同,后者者执行后前者会被回收
        String str="123";
        str+="abc";
        System.out.println(str);
    }
}

StringBuffer:对StringBuffer类型的对象操作,操作的始终是同一个对象;

public class TestStringBuffer {
	public static void main(String[] args) {
		// StringBuffer里始终是一个对象;进行实列化
		StringBuffer sb=new StringBuffer("sss");
		sb.append("2223");
		System.out.println(sb);
	}

}

总结下:假如定义的字符串内容基本不变或者很少变化,用String效率高;假如定义的字符串内容经常变动,要用StringBuffer;

3.java常用类-Math类

Math类是一个数学工具类方法,里面有很多静态工具方法;方便开发者直接调用;

4.java常用类-Arrays类

工具类,在使用时候需要查看他的api文档进行使用

import java.util.Arrays;
 
public class TestArrays {
 
    public static void main(String[] args) {
        int arr[]={1,7,3,8,2};
        System.out.println(arr);
        System.out.println("以字符串形式输出数组:"+Arrays.toString(arr));
        Arrays.sort(arr); // 给数组排序
        System.out.println("排序后的数组:"+Arrays.toString(arr));
        System.out.println(Arrays.binarySearch(arr, 1));
        Arrays.fill(arr, 0); // 将指定内容填充到数组中
        System.out.println("填充数组后的字符串:"+Arrays.toString(arr));
    }
}
六、泛型
1,泛型引入

定义:使用泛型可以指代任意对象类型;

为什么引入泛型?

原因是以内在实际开发时候,可能胡想到用多态,父类直接指向子类的引用,会在开发过程中,出现数据的转型,而且还麻烦,对此,有了泛型,多的不说直接上实列:

//传统

public class A {
	private Integer a;

	public Integer getA() {
		return a;
	}

	public void setA(Integer a) {
		this.a = a;
	}

	public A(Integer a) {
		super();
		this.a = a;
	}
	public void print(){
		System.out.println("输出类型"+a.getClass().getName());
	}

}

public class B {
	private String b;

	public String getB() {
		return b;
	}

	public void setB(String b) {
		this.b = b;
	}

	public B(String b) {
		super();
		this.b = b;
	}
	public void print(){
		System.out.println("输出类型"+b.getClass().getName());
	}
	

}
// 测试
public class A_Btest {
	public static void main(String[] args) {
		A x=new A(3);
		x.print();
		int q=x.getA();
		System.out.println(q);
		
		B y=new B("我爱你;宝贝");
		y.print();
		String s=y.getB();
		System.out.println(s);
	}
}
// 多态
public class AB {
	// 多态
	private Object object;

	public Object getObject() {
		return object;
	}

	public void setObject(Object object) {
		this.object = object;
	}

	public AB(Object object) {
		super();
		this.object = object;
	}
	public void print(){
		System.out.println("输出的OBJECT类型:"+object.getClass().getName());
	}
// 测试
    public class Test_AB {
	public static void main(String[] args) {
		// Object 是所有类型的父类,可以自动识别类型
		AB z=new AB("6652");
		z.print();
		Object ab_z=z.getObject();
		System.out.println(ab_z);
		
	}

}
    //泛型
    package com.java1234_6;

public class AA <B>{
	private B b;

	public B getB() {
		return b;
	}

	public void setB(B b) {
		this.b = b;
	}

	public AA(B b) {
		super();
		this.b = b;
	}
	public void print(){
		System.out.println("这是B泛型的数据类型:"+b.getClass().getName());
	}

}
// 测试
    public class TestAA {
	public static void main(String[] args) {
		AA<Integer> k=new AA<Integer>(1);
		k.print();
		int kk=k.getB();
		System.out.println(kk);
		  
		AA<String> v=new AA<String>("sdf123");
		v.print();
		String vv=v.getB();
		System.out.println(vv);

	}

}

2.java泛型-限制泛型类型

多的不说直接上代码:前面我们讲的泛型,可以是任意类型,但是我们有时候,需要限制类型,这样更加安全。

public class Dog extends Animal {
	public void dog() {
		System.out.println("dog");
	
	}

}

public class Cat extends Animal {
	public void cat() {
		System.out.println("cat");
	}

}
// 然后我们定义一个泛型类:
public class TT <B extends Animal >{
	private B b;
	public B getB() {
		return b;
	}
	public void setB(B b) {
		this.b = b;
	}
	public TT(B b) {
		super();
		this.b = b;
	}
	private void print() {
		System.out.println("泛型B的输出类型"+b.getClass().getName());
	}
}

// 测试类:
public class TestAnimal {
	public static void main(String[] args) {
		TT<Dog> dog =new TT<Dog>(new Dog());
		Dog dog1=dog.getB();
		dog1.dog();
		
		TT<Cat> c=new TT<Cat> (new Cat());
		Cat cat1=c.getB();
		cat1.cat();
	}

}



这里假如我们放其他类型:直接编译报错,说类型不对;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SgOcFO47-1655907022895)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220622212003108.png)]

3.java泛型-通配符泛型:

通配符泛型在使用泛型 特殊的场景下用到,比如把泛型对象作为方法参数传入方法的时候,就用到通配符泛

public abstract class TongPeiFuTest {
	
	
	// ?  通配符,在泛型中调用的时候,传入不同的对象,需要不同的泛型对象时候,可以采取使用通配符来使用
	//比如下面需要调用猫和狗但是在这上面方法传入时候,
//	public static void take(TT<Dog> b){
//        b.print();
//}这种情况只能是传dog类型的,不可以获得cat
	
	public static void take(TT<?> b){
	         b.print();
	}
	
	public static void main(String[] args) {
		TT<Animal> animal=new TT<Animal>(new Dog());
		take(animal);
		
		TT<Animal> animal1=new TT<Animal>(new Cat());
		take(animal1);
		
	}

}
4.java泛型-泛型方法:泛型方法指返回值和参数都用泛型表示的方法;
// 
public class FanXingMouth {
	/**
	 * 泛型方法
	 * @param t
	 */
	public static <T> void f(T t){
		System.out.println("T现在的类型是"+t.getClass().getName());
	}
	public static void main(String[] args) {
		f(1);
		f(1.0);
		
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沁颖呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值