【部分】Java速成学习笔记

StringBuilder类 可变,
方法:
sb.append(“hello”); //可追加任意数据类型
sb.reverse(“hello”); //实现字符串转换,变为"olleh"

转换
sb.toString(); //StringBuilder转化为String
StringBuilder sb = new StringBuilder(s);//String转化为StringBuilder

ArrayList类似C++里的vector
定义:ArrayList array=new ArrayList<>(); //其中E为ArrayList里存储的数据类型
array.add(“hello”); //可以追加元素到末尾
array.add(index:1,element:“hello”); //在索引1处添加字符串"hello"
array.remove(“hello”); //删除指定元素,返回删除是否成功
array.remove(index: 1); //删除指定索引处的元素,返回被删除的元素
array.set(index: 1,“javese”); //修改指定索引处的元素,返回被修改的值
array.get(0);//返回指定索引的元素
array.size(); //返回集合的元素个数

int输入后不能输入String的解决方法:
int a=cin.nextInt();
cin.nextLine(); //在int后加上这一行
String s=cin.nextLine();

继承
public class 子类 extends 父类 {}
super关键字用于访问父类中的成员变量

关于方法重写
私有方法不能被重写
子类中重写方法的访问权限不能比父类的低(public>默认>私有)

方法被final修饰后不能重写
变量别final修饰,不能再次被赋值
类被final修饰后不能作为父类被继承

静态成员方法只能访问静态成员

抽象类中不一定有抽象方法
类中如果有抽象方法,该类必须定义为抽象类
public abstract class Animal{
public abstract void eat();
}

抽象类的子类重写父类中所有抽象方法时,抽象方法不含abstract关键字

字符串比较==和equals()的区别
==比的是字符串的地址
equals()比的是字符串的内容

接口里只存在成员常量,默认修饰为public static final,可以被接口名直接访问
接口没有构造方法,接口里的成员方法不能有方法体(只能是抽象方法),默认修饰为public abstract
一个类如果没有父类,默认继承为Object类

抽象类是对事物的抽象,而接口是对行为的抽象

内部类可以直接访问外部类的成员,包括私有
外部类要访问内部类成员,必须创建对象

只要一个类是抽象的或是一个接口,那么其子类中的方法都可以使用匿名内部类来实现

常见API
System.currentTimeMillis(); //当前时间和1970年之间的毫秒值

int—>String
int num=100;
String s1=String.valueOf(num); //int转String


String—>int
String s=“100”;
int x=Integer.parseInt(s);// String转int


public String[] split(String regex) //以regex为分隔符,将分割后的字符串存储数组
eg.
String s = “91 27 47 38 50”;
String[] strArray = s.split(" ");

Integer i = Integer.valueOf(100); //装箱
Integer ii=100; //自动装箱
int k=ii.intValue(); //拆箱
int k=ii+200; //自动拆箱

日期类
Date d=new Date();
d.getTime(); //获取的是日期对象从1970年1月1日00:00:00到现在的毫秒值
System.out.println(d); //直接获取当前时间

格式化输出日期+ Date—>String
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy年MM月dd日 HH:mm:ss”);
String s = sdf.format(d);
System.out.println(s);// 2020年10月03日 13:03:00


String—>Date //函数需要抛出异常 throws ParseException
String s=“2020-10-03 14:06:15”;
SimpleDateFormat sdf=new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
Date d=sdf.parse(s);
System.out.println(d);


Calendar类
import java.util.Calendar;//导包
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month=c.get(Calendar.MONTH)+1;
int date=c.get(Calendar.DATE);
System.out.println(year+“年”+month+“月”+date+“日”); //2020年10月3日


try{
int[] arr={1,2,3};
System.out.println(arr[3]);
}catch(ArrayIndexOutOfBoundsException e)
{
e.printStackTrace(); //输出异常的信息
}


自定义异常类 继承Exception
格式:
public class 异常类 extends Exception{
无参构造
带参构造
}
带throws的方法
public void 方法名(参数) throws 自定义的异常类名{
if(…){ //如果发生…,则抛出异常
throw new 自定义的异常名();
}
}
在调用带throws的方法时,需要try{}catch(异常类名 变量){} 来捕获异常


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值