Java学习第四周常用类总结

1. Object类中的常用方法

Object类是所有类的超类

1.1 clone()方法说明

cline() 的方法是Object执行特定的克隆操作。

1.2使用方法
Object obj = 对象名.clone
1.3代码体现
//测试类
public class ObjectDemo {	
	public static void main(String[] args) throws CloneNotSupportedException {
		//有参构造赋值
		Student s = new Student("高圆圆",41) ;
		System.out.println(s);
		System.out.println(s.getName()+"---"+s.getAge());
		System.out.println("---------------------------------");
		
		//使用clone()之前
		Student s2 = s ;
		System.out.println(s2.getName()+"---"+s2.getAge());
		
		System.out.println("----------------------------------");
		
		//使用clone方法
		//s对象所在的类必须实现一个接口:cloneable接口
		Object obj = s.clone() ;//进行复制操作(浅克隆)
		
		//将Object--->具体的子类Student
		Student s3 = (Student)obj ;
		System.out.println(s3.getName()+"---"+s3.getAge());
		
	}
}
//类实现了 Cloneable 接口,以指示 Object.clone() 方法可以合法地对该类实例进行按字段(成员变量)复制。
//学生类
public class Student  implements Cloneable {
	
	
	//提供两个属性
	private String name ;//姓名
	private int age ; //年龄
	public Student() {
		super();
	}
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
	
	//重写clone
	@Override
	protected Object clone() throws CloneNotSupportedException {
		return super.clone();
	}
}

1.2equals
1.2.1方法说明

Object中的equals比较的是两个对象的地址值与==类似

1.2.2使用方法
对象.equals(对象);//放回值为boolean类型
1.2.3代码提现
//变量比较
String str1 = "abc";
String str2 = "abc";
System.out.println(str1.equals(str2)); //true   String 类重写了equals方法,比较的是内容
//引用类型比较
Object obj = new Object();
Object obj2 = new Object();
System.out.println(obj.equals(obj2)); //false  Object中的equals方法比较的是地址值

2. Scanner类中的常用方法

文本扫描器:Scanner (java.util.Scanner)

使用之前需要导包,并且要创建键盘录入的对象,例

Scanner 对象名 = new Scanner(System.in);

2.1 nextInt()
2.1.1方法的说明

int 数据类型的输入操作

2.1.2使用方法
//一般情况会给一个提示
System.out.println("输入的内容是什么");
int 变量名 = 对象名.nextInt();
2.1.3代码体现
//导包
import java.util.Scanner;
public class ScannerDemo2 {
	public static void main(String[] args) {
		//创建键盘录入对象
		Scanner sc = new Scanner(System.in) ;
		//提示并录入数据
		System.out.println("请您输入一个int类型数据:");
		int a = sc.nextInt() ;
		System.out.println("值是:"+a);        
    }
}		
2.2 nextLine()
2.2.1方法说明

String 类型的数据的输入操作

2.2.2 方法使用
//一般情况会给一个提示
System.out.println("输入的内容是什么");
int 变量名 = 对象名.nextLine();
2.2.3代码体现
//导包
import java.util.Scanner;
public class ScannerDemo2 {
	public static void main(String[] args) {
		//创建键盘录入对象
		Scanner sc = new Scanner(System.in) ;
		//提示并录入数据
		System.out.println("请您输入String类型数据:");
		String a = sc.nextLine() ;
		System.out.println("输入的内容是"+a);        
    }
}	
2.3注意事项

如果要录入int类型的数据的同时还要录入String类型的数据

系统会默认(Enter)回车键为String类型数据,当先录入的是int类型的数据时在录入String类型的数据时会直接显示录入完毕

解决办法:重新创建一个录入对象

3.String类中的常用方法

3.1.构造方法
3.1.1常用的构造方法
1)String str = "常量值";//创建字符串变量(推荐)
2)String str = new String (byte[] bytes);//将字节数组转为字字符
3)String str = new String (char[] chs); // 将字符数组转为字符串 
4)String str = new String (String str); // 创建字符串变量
3.2常用方法
3.2.1获取方法
1)int length();// 获取字符串长度
2)String concat (String str);//拼接功能
3)String[] split (String str);//拆分
4char charAt(int index);//获取指定索引值处的字符
5)int indexOf(int ch)://查询元素的索引值,查不到返回-1
3.2.2转换方法
1public char toCharArray();//将字符串转换为字符数组
2public byte[] getByte();//平台默认编码值,字符串转为字节数组
3public String toUppCase();//将小写的字符串大写
4public String toLowerCase();//将字符转换为小写
3.2.3截取功能
public String substring (int beginIndex )//子字符串以指定索引处的字符开头,并扩展到该字符串的末尾
public String substring (int beginIndex,int endIndex)://子串开始于指定beginIndex并延伸到字符索引endIndex - 1 
public int indexOf(char ch/String )://查询当前字符或字符串第一次出现的索引值
3.2.4其他功能
//替换功能
public String replace(char oldChar,char newChar);//替换功能,将指定的新字符串替换旧的字符串
//去除两端的空格的功能
public String trim();  //返回一个删除了首位和结尾的空格的字符串
//按照字典顺序比较
public int compareTo(String anotherString);

4.StringBuffer类中的常用方法

4.1构造方法
public StringBuffer();//构造一个没有字符的字符串缓冲区,初始容量为16个字符
public StringBuffer(int capacity);//构造一个没有字符的字符串缓冲区和指定的初始容量。
public StringBuffer(String str);//构造一个初始化为指定字符串内容的字符串缓冲区。 字符串缓冲区的初始容量为16加上字符串参数的长度。 
4.2常用方法
4.2.1获取功能
int length() 获取长度(缓冲区中的字符数的长度)
4.2.2添加功能
public StringBuffer append(任何类型的数据)://将任何数据类型追加到缓冲区中
public StringBuffer insert(int 插入位置 任何数据类型)//插入
4.2.3删除功能
public StringBuffer deleteCharAt(int index):删除指定位置的字符,返回缓冲区本身
public StringBuffer delete(int start,int end):删除从指定位置到指定位置结束(不包括end位置)
4.2.4截取功能
public String substring(int start)://从指定位置开始到末尾
public String substring (int start ,int end)://从指定位置开始到指定位置结束
4.2.4替换功能
 public StringBuffer replace (int start ,int end ,String str) ://从指定位置开始到指定位置结束,使用str 子串进行替换;
4.2.5反转功能
public StringBuffer reverse() ;//将字符进行反转
4.3 String 和StringBuffer的相互转换
示例
//String-----转为----->StringBuffer
	 //方式1
     //通过StringBuffer构造方法:StringBuffer(String str)
 	 StringBuffer sb = new StringBuffer();
	 //方式2
     StringBuffer() 无参构造+append(String str):追加
//StringBuffer -----转为------>String
     //方式一:
     //String 类的构造方法
     String str = new String(buffer);
	 //方式二:
     String str = buffer 

5.Integer类中的常用方法

5.1进制转换
public static String toBinaryString(int i);//转换位二进制数
public static String toHexString(int i);//转换为十六进制数
public static String toOctalString(int i)//转换为八进制数
5.2 Integer和String之间的转换
//如何将String--------------->int 
	// 使用方法功能:
        Integer/Long/Double.parseInt(String str)
    //方式1:
            //使用空字符串拼接int类型的数据
    //方式2:
        int---封装为----Integer ----使用构造方法Integer(int value)
        Integer---使用 toString() -->String
    //方式3:静态功能:
         public static String toString (int i)
//String ---------> Integer 
    // 方式1:
        public static int parseInt(str)
        int number = Integer.parseInt(str);
        System.out.println(str);
    // 方式2:
        public static Integer valueOf(String s)
        Integer number = Integer valueOf(str);
        int number2 - number.intValue();

6.Math类

6.1常用方法
public static double abs()  
    //求绝对值
public static double ceil() 
    //向上取整
public static double floor(double a)
    //向下取整
public static double random() 
    //取一个0~1之间的随机数---Java提供的Random类 是随机数生成器
public static double round()  
    //四舍五入
public static double sqrt()   
    //求某数的平方根
public static double min/max(int a ,int b)
    //求两个数的最小值/最大值
public static double pow (double a double b):
	//返回:a的b次幂

7.Calendar类

7.1构造方法
protected Calendar();
7.2常用方法
//创建该日历类对象
public static Calendar getInstance();
//方法使用    Calendar c = Calendar.getInstance() ;
// 获取年份 
public static final int YEAR;
//获取年中的月份(0-11) +1
public static final int MONTH;
//获取年中的日
public static final int DATE;
//获取月中的日
public static final int DAY_OF_MONTH 
//给日历字段添加或者减去时间偏移量   
public abstract void add(int field,int amount)
//设置当前年月日,日历时间
public final void set(int year,int month, int date)

8.date类

8.1构造方法
public Date() //无参构造
8.2成员方法
//返回1970 年 1 月 1 日所表示的时间毫秒
public long getTime() ;
8.3Data日期格式和String文本格式转换
8.3.1Data---->String
//
//创建一个日期对象
	Date date = new Date() ;
	System.out.println(date);//Wed Oct 21 14:10:48 CST 2020 Date格式
		
//2)创建SimpleDateFormat:格式化的桥梁
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd") ;//年-月-日
//3)格式化操作
	String dateStr = sdf.format(date) ;
	System.out.println(dateStr);
8.3.2String ---->Data
//日期文本
	String source = "2022-6-30 20:00:30" ;
//String---->Date
//创建SimpleDateFormat对象,格式必须和String 日期文本格式一致
//SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日") ;
	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
//使用方法parse
	Date d = sdf2.parse(source) ;
	System.out.println(d);

9.Random类

9.1构造方法
public Random(); //推荐,产生的随机数是变化的
public Random(long seed);//每次产生的随机数是固定的
9.2成员方法
public int nextInt()
    //获取的是int范围内的随机数
public int nextInt(int n)
    //随机生成0~n之间的数
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值