一、内部类
public static void main(String[] args) {
//创建接口类型的变量
// Usb usb=new Mouse();
// usb.show();
//创建局部内部类
// class Fan implements Usb{
// @Override
// public void show() {
// System.out.println("Usb连接成功,键盘开始工作");
// }
// }
//使用局部内部类创建对象
// Usb usb=new Fan();
// usb.show();
//创建匿名内部类
Usb usb=new Usb() {
@Override
public void show() {
System.out.println("USB连接成功,风扇开始工作");
}
};
usb.show();
}
}
//匿名内部类特点:
/*
1.没有类名的局部内部类(一切特征都与局部内部类相同)
2.必须继承一个父类或者实现一个接口
3。定义类、实现类、创建对象的语法合并,只能创建一个该类的对象
*/
二、Object类
1.equals()方法
public class Test {
public static void main(String[] args) {
A t=new A("李四",25);
A t1=new A("张三",25);
System.out.println(t.equals(t1));
}
}
/*笔记
1.public boolean equals(Object obj){}
2.默认实现为(this==obj ),比较两个对象地址是否相同
*/
2.Getclass()方法
public class Test {
public static void main(String[] args) {
GetClass s1=new GetClass("张三",20);
GetClass s2=new GetClass("李四",25);
Class aClass=s1.getClass();
Class aClass1=s2.getClass();
if (aClass==aClass1){
System.out.println("类型相同");
}else {
System.out.println("类型不同");
}
}
}
/*
public final Class<?> getClass(){}
返回引用中存储的实际对象类型
应用:通常用于判断两个引用中实际存储对象类型是否一致
*/
3.Hashcode()方法
public class TestStudent {
public static void main(String[] args) {
Student s1=new Student("李四",21);
Student s2=new Student("张三",25);
System.out.println(s2.hashCode());;
System.out.println(s1.hashCode());
Student s3=s1;//相同对象返回相同哈希码
System.out.println(s3.hashCode());
}
}
/*笔记
1.public int hashCode(){}
2.返回该对象的哈希码值
3.哈希值根据对象的地址或字符串或数字使用hash算法计算出来的int类型的数值
4.一般情况相同对象返回相同的哈希码
*/
4.toString()方法
public class T {
public static void main(String[] args) {
A A1=new A("K",22);
A A2=new A("X",40);
System.out.println(A1.toString());
System.out.println(A2.toString());
}
}
/*笔记
public String toString(){}
返回该对象的字符串表示(表现形式)
可能根据程序需求覆盖该方法,如:展示对象的各个属性值。
*/
三、包装类
public class Demo {
public static void main(String[] args) {
//类型转换 装箱 基本类型转引用类型
int num=18;//基本类型
//使用Integer类创建对象
Integer integer1=new Integer(num);
Integer integer2=Integer.valueOf(num);
//类型转型 拆箱 引用类型转基本类型
Integer integer3=new Integer(100);
int num1=integer3.intValue();
//JDK1.5之后,提供了自动装箱和拆箱
int age=30;
//自动装箱
Integer integer5=age;
//自动拆箱
int num3=integer5;
}
}
//基本类型和字符串之间的转化
public class Dome1 {
public static void main(String[] args) {
int num=15;
//1.使用"+"
String str=num+"";
//2.使用Integer中的toString方法
String str1=Integer.toString(num);
//3.使用Integer中的toString方法重载方法也可以转换进制
String str2=Integer.toString(num,16);
System.out.println(str2);
//字符串类型转基本类型
String str6="15";
//使用Integer中的parsexxxx方法
int num1=Integer.parseInt(str6);
//boolean字符串形式转基本类型 ”true“------>true 非true------->false
String s="true";
boolean b=Boolean.parseBoolean(s);
System.out.println(b);
}
}
//Integer缓冲区 按住CTRL鼠标点击Integer方法,可进入查看
public class Demo2 {
public static void main(String[] args) {
Integer integer=new Integer(100);
Integer integer1=new Integer(200);
System.out.println(integer==integer1);
Integer integer2=Integer.valueOf(100);//自动装箱 Integer.valueof()
Integer integer3=Integer.valueOf(100);
System.out.println(integer2==integer3);
Integer integer4=Integer.valueOf(200);
Integer integer5=Integer.valueOf(200);
System.out.println(integer4==integer5);
}
}
/*
JAVA预先创建了256个常用的整数包装类型对象
在实际应用当中,对已创建的对象进行复用
*/
四、String 类
1.
public class Dome1 {
public static void main(String[] args) {
String name="hello";//"hello "常量存储在常量池中
name="hei";//"hei"赋值给name变量,给字符串重新赋值时,并没有直接去修改原来的数据,而是重新开辟了一个空间
//这就是字符串常量的不可变性
String name1="hello";//如果给新的字符串赋的值可以在字符串池(常量池)中找到,就会直接将它引用过去(字符串常量的共享)
//是同一个地址
//另外一种创建字符串的方法
String str=new String("java");//产生两个对象,堆、池各存储一个,而真正运行时,会合并成一个(实际上调用的池中的对象)
}
}
2.
字符串方法的使用
1.length()返回自字符串的长度
2.charAt(int index)返回某个位置的字符
3.contains(String str)判断是否包含某个字符串
4.TocharArray()返回字符串对应的数组
5.indexOf();返回字符串首出现的位置
6.lastIndexOf();返回字符串最后出现的位置
7.trim();去掉字符串前后的空格
8.toUpperCase();把小写转成大写。toLowerCase();把大写转成小写
9.endsWith(str)判断是否以str结尾,startsWith(str)判断是否以str开头
10.replace(char old,char new );用新的字符或字符串替换旧的字符或字符串
11.split()对字符串进行拆分
//StringBuffer和StringBuilder用法
/*
StringBuffer:可变长字符串,JDK1.0提供,运行效率慢、线程安全。
StringBuilder:可变长字符串,JDK5.0提供,运行效率快,线程不安全
*/
public class Dmoe3 {
public static void main(String[] args) {
StringBuffer str=new StringBuffer();
//append() 追加
str.append("java是最好的语言");
System.out.println(str.toString());
str.append("java很香");
System.out.println(str);
//insert()添加
str.insert(0,"你知道吗");
System.out.println(str);
//replace()替换
str.replace(0 ,5,"python");
System.out.println(str);
//delete()删除
str.delete(0,str.length());
System.out.println(str.length());
}
}
五、BigDecimal使用
//BigDecimal使用 精确运算
public class Test {
public static void main(String[] args) {
double db1=1.0;
double db2=0.9;
System.out.println(db1-db2);
//使用BigDecimal进行精确运算
BigDecimal str2=new BigDecimal("1.0");
BigDecimal str1=new BigDecimal("0.9");
BigDecimal str3=str2.subtract(str1);//subtract()减法
System.out.println(str3);
System.out.println(str2.add(str1)); //add 加法
System.out.println(str2.multiply(str1));//multiply()乘法
//divide()除法
BigDecimal r4=new BigDecimal("1.4").subtract(new BigDecimal("0.5")).divide(new BigDecimal("0.9"));
System.out.println(r4);
}
六、Date类
public static void main(String[] args) {
//创建一个Date对象
Date date1=new Date();
//打印今天
System.out.println(date1.toString());
System.out.println(date1.toLocaleString());
//昨天 getTime()
Date date2= new Date(date1.getTime()-(60*60*24*1000));
System.out.println(date2.toLocaleString());
//after()方法 表示date1在date2后
boolean b1=date1.after(date2);
System.out.println(b1);
//before()方法 表示date2在date1前
boolean b2=date2.before(date1);
System.out.println(b2);
//compareTo()方法 用来比较两个时间
int s1=date2.compareTo(date1);
System.out.println(s1);
}
}
七、Calendar类
Calender提供了获取或设置各种日历字段的方法
构造方法: protected Claender() :由于修饰符是protected,所以无法直接创建对象
public static void main(String[] args) {
//创建一个Calender对象
Calendar calendar1=Calendar.getInstance();
System.out.println(calendar1.getTime().toLocaleString());
System.out.println(calendar1.getTimeInMillis());
//获取时间信息
//获取年
int year=calendar1.get(calendar1.YEAR);
//获取月
int month=calendar1.get(calendar1.MONTH);
//获取日
int day=calendar1.get(calendar1.DAY_OF_MONTH);
//小时
int hour=calendar1.get(calendar1.HOUR_OF_DAY);
//分钟
int minute=calendar1.get(calendar1.MINUTE);
//秒
int second=calendar1.get(calendar1.SECOND);
System.out.println(year+"年"+(month+1)+"月"+day+"日"+hour+"时"+minute+"分"+second+"秒");
//修改时间
Calendar calendar2=Calendar.getInstance();
calendar2.set(calendar1.DAY_OF_MONTH,23);
System.out.println(calendar2.getTime().toLocaleString());
//add()方法修改时间
Calendar calendar3=Calendar.getInstance();
calendar3.add(calendar2.HOUR_OF_DAY,1);
System.out.println(calendar3.getTime().toLocaleString());
//补充方法 获取当前时间的最大最小值
int s1=calendar2.getMaximum(calendar2.DAY_OF_MONTH);//最大值
System.out.println(s1);
int s2=calendar2.getMinimum(calendar2.DAY_OF_MONTH);//最小值
System.out.println(s2);
}
}
八、SimpleDateFromat类
//SimpleDateFormat是一个以与语言环境有关的方式来格式化和解析日期的具体类
public class Test {
public static void main(String[] args) throws Exception{
//创建SimpleDateFormat对象
SimpleDateFormat s1=new SimpleDateFormat("yyyy-MM-dd");
//创建Date
Date date1=new Date();
//格式化Date(把日期转成字符串)
String string=s1.format(date1);
System.out.println(string);
//解析(把字符串转成日期)
Date date2=s1.parse("1999-06-20");
System.out.println(date2);
}
}
九、System系统类
//Systme方法
public class Test {
public static void main(String[] args) {
int[] arr={55,16,42,46,45};
int[] arr1=new int[5];
//arraycopy()方法复制数组
/*
src:原数组
srcPos:从哪个位置开始复制
dest:目标数组
destPos:目标数组的位置
length:复制的长度
*/
System.arraycopy(arr,0,arr1,0,5);
for (int i=0;i<arr1.length;i++){
System.out.println(arr1[i]);
}
}
}