JAVA类--重中之重--好复杂

今天写我们需要精通的一些java类;这好像很复杂,东西太多有的都记不住,只能死记硬背,经常练习了,但又爱玩坐不住啊,所以这张弄
的我很纠结啊!慢慢写吧哎作业还没交呢呼呼,都赶一块了!!
Java系统提供了大量的类和接口供程序开发人员使用,并且按照功能的不同,存放在不同的包中。
这些包的集合就是应用程序的编程接口(Application Programming Interface,API),也称为“类库”。

java类库中常用的包有如下几种:
java.lang包:主要包含与语言、数据类型相关的类。由于该包中由解释程序自动加载,因此不需要显示的说明,即不用import语句导入即可用
该包中的类;例:Object、String、Boolean、Byte、Integer、Math等;
java.io包:主要包含与输入输出操作相关的类;
java.util包:主要包含具有特定功能的类,有日期、向量、哈希表、堆栈等:
java.awt包和javax.swing包:提供了创建图形界面元素的类,通过这些类,可以控制应用程序的外观界面

java.net包:Java网络包,提供与网络操作相关的类;
java.sql包:包含了与数据库编程相关的类/接口

除了java.lang包中的类系统自动引入外,若要使用其他包中的类,应在程序的开始部分引入相应的包。

import java.io.*;

import java.util.Date;

improt java.awt.*;


下面先写java类的共同父类---java.lang.Object
java.lang.Object类是所有Java类的最高层次父类。
属性:没有定义任何属性

方法
: String toString() :返回当前对象的有关信息(对象所属的类型名称以及对象的哈希码)。
class TestObject
{

public static void main(String[] args)
{
Object 0=new Object();
System.out.println(o.toString());
}

}

System.out.println(o.toString()) ;
等价于

System.out.println(o);


可以根据需要在自己定义的Java类中重写toString()方法,以提供更适用的说明信息。


boolean equals()方法:用于比较两个对象是否等价。对于非空引用值来说,只有当这两个引用变量是对同一个对象的引用时,该方法才返回true。
equals()方法在比较一些特定的引用类型(如String、java.io.File、java.util.Data以及封装类等)数据时,只要两个对象等价即返回为true。

String变量赋值内容相同的话,在内存中将只保存一份。

String s1="abc"; String s2="abc"; //s1和s2指向同一对象
System.out.println(s1==s2); //结果为true


hashCode()方法:返回当前对象的哈希码(HashCode)。
哈希码可以理解为系统为每个Java对象自动创建的整型编号,任何两个不同的Java对象的哈希码一定不同,而在Java程序的一次执行期间,在同一对象上多次调用hashCode()方法时,必须一致返回相同的整数。
语法格式:public int hashCode();
public class TestHc
{
public static void main(String[] args)
{
Car c1 = new Car();
c1.color = "green";
c2.name = "banma";
Car c2 = new Car();
c2.color = c1.color;
c2.name = c1.name;
System.out.println(c1.hashCode());
System,out.println(c2.hashCode());
}
//程序中,变量c1和c2所引用的两个Car类型对象虽然内容相同,但其哈希吗不同;
}


字符串相关类型:

java.lang.String:表示的是16位Unicode编码字符组成的字符串,用于记录和操作文本信息 。
构造方法
1、public String() //初始化一个新创建的String对象,他表示一个空的字符序列("")也称空串;
2、public String (byte[] bytes)//使用当前的平台的默认子集,解码指定的数组,并将其解析出来的字符按照原来顺序构造一个新的String对象;
3、public String(char[] value)// 复制字符数组value的内容,构造新的String对象;
4、public String(String original)//用已有的String对象original初始化一个新的String对象,实现String对象的复制功能;
5、public String(StringBuffer stringBuffer)
// 使用对象stringBuffer的内容,构造新的String对象,创建后对stringBuffer对象的修改不会影响新创建的字符串;

例:
public class TestString
{
public static void main(String[] args)
{
byte[] b={67,68,66,100,101};
System.out.println(new String(b));
char[] c={'a','b','c','c','b','a'};
System.out.println(new String(c));
System.out.println(new String("hello,world"));
System.out.println(new StringBuffer("abc"));
}
}

输出结果为:CDBde abccba hello,world abc;

常用的成员方法:
public String concat(String str):实现字符串连接功能,相当于字符串'+';
public String replace(char oldChar,char newChar):实现字符串替换功能;
public String substring(int beginIndex)和public String substring(int beginIndex,int endIndex);组成一个是现在字符串中提取子串的功能;begin为开始位置,end为结束位置;
public String toLowerCase():将字符串的内容全部转为小写,组成一个新的字符串;
public String toUpperCase():将字符串的内容全部转为大写,组成一个新的字符串;
public String trim():过滤掉字符串开头和结尾的空格;
例:
public class TestString
{
public static void main(String[] args)
{
String str1="Java String ",str2=" and Apple";
System.out.println(str1.trim(),concat(str2));//先去空格,在字符串连接输出为:Java String and Apple;
String.out.println(str1.replace('S','s'));//将str1的S替换为小写的s;输出;
String.out.println(str2.toLowerCase());//将str2的字符串全部转为小写;
String.out.println(str1.toUpperCase());//将str1的字符串全部转为大写;
String.out.println(str1.substring(5,8));//将str字符串中截取5-8的内容,不包括8;
}
}

检索和查找功能:

public char charAt(int index):返回指定索引位置的字符;
public boolean endWith(String suffix):判断当前字符串是否已指定字符串的子串结尾;
public boolean startsWith(String prefix):判断当前字符串是否一指定字符串的字串开头;
public int indexOf(int ch)和public int indexOf(String str):查找指定的字符或字符串在当前字符串中出现的索引位置;
public int lastIndexOf(int ch)和public int lastIndexOf(String str):查找指定的字符串或字符在当前字符串中最后一次出现的索引位置;(下标);
public int length:返回字符串长度,即取所包含的字符个数;


内容比较功能:

public boolean equals(Object anObject):比较当前字符串与指定对象的等价性;
public boolean equalsIgnoreCase(String anotherString):比较两个字符串的内容是否相同,忽略大小写;、
public int compareTo(String anotherString):按照字典顺序比较来年各个字符串大小,

拆分字符串:

public String[] split(String regex):根据给定表达式的匹配拆分字符串;

Example:
public class TestString
{
public static void main(String[] args)
{
String str="北京:奥运会:2008";
String results= str.split(":");以分号为分割线切割;
for(String each : results)
{
System.out.println(each);
}

}
}
输出:北京
奥运会
2008


java.lang.StringBuffer类
java.lang.StringBuffer类:表示的是内容可以修改的Unicode编码的字符序列,其对象创建之后,所保存的字符串内容和长度均可以修改。
每个StringBuffer对象都拥有一个可变容量的字符串缓冲区

构造方法:1、public StringBuffer() ; 2、 public StringBuffer(int capacity) 3、public StringBuffer(String str)

常用方法:
append():向字符串缓冲区追加信息,可将任何类型的参数的值转换为String类型,然后追加字符串尾部;
insert():在字符串内添加信息;
public StringBuffer reverse():将当前的字符序列进行反转处理;
public void setCharAt(int index,char ch):设置字符序列中指定索引处的字符;

System类:类中所有成员都是静态的,当要引用这些变量和方法的时候,System.属性 System.方法()


exit(int x)方法:终止当前正在运行的;
JVM
currentTimeMillis()方法:返回毫秒数

Runtime类 :该类封装了Java命令本身所启动的实例进程信息---Java虚拟机进程。
import java.io.*;
public class TestRuntime
{
public static void main(String[] args)
{
try{
Runtime r= Runtime.getRuntime();
Process p=r.exec("c:\\windows\\system32\\Notepad.exe");

}
catch(IOException ex){
System.out.println(ex.toString());
}
}

}
可以通过Runtime对象的exec()方法启动子进程,其返回值代表为子进程Process对象;

封装类:
java数据类型可以分为基本数据类型和引用数据类型两大类,两者有各自不同的的特征和用法:
由于基本类型数据不是对象,在有些场合其使用是受到限制的
public void test(Object o){ System.out.println(o.toString()); }
//该方法可处理任何引用类型的数据,不能处理基本类型数据


为弥补基本数据类型在面向对象方面的欠缺,Java语言中引入了封装类(Wrapper Classes)——针对各种基本数据类型均提供了相应的引用数据类型,它们在JDK API的java.lang包中定义的。


intInteger
shortShort
longLong
byteByte 封装类均被定义为final,不能被继承

floatFloat
doubleDouble
charCharacter
booleanBoolean

每个Integer类的对象可以封装一个int型的整数值,该类中还提供了多个用于处理int型数据的功能方法。
构造方法

1、public Integer(int value) 2、
public Integer(String s)throws NumberFormatEcception

例如,Integer i1=new Integer(123);
Integer i2=new Integer(“123”);

其它常用方法

public int intValue();返回当前对象封装的int型值

public boolean equals(Object obj);// 用于比较两个Integer对象中封装的整型值是否相等


public String toString();将当前对象所封装的int型数值转换成字符串返回。

public static String toString(int i);将参数i指定的int型数值以字符串的形式返回

public static String toBinaryString(int i);将i指定的int型数值的二进制无符号整数表示以字符串形式返回。

public static String toOctalString(int i);将i指定的int型数值的八进制无符号整数表示以字符串的形式返回。

public static String toHexString(int i);将i指定的int型数值的十六进制无符号整数表示以字符串的形式返回。

public static int parseInt(String s) throws NumberFormatException;将参数s指定的字符串转换成整型数据返回。

封装类的用法。

Integer int1=new Integer(786);

Integer int2=new Integer(“786”);

System.out.println(int1==int2);

System.out.println(int1.equals(int2));

int i=Integer.parseInt(“123”);//若字符串中包含非法字符或是null、空串则抛数据格式异常

System.out.println(Integer.toHexString(-1));

String.valueOf(d) ===将参数d转化为字符串的形式返回

Integer.toString(i) ===将整型i转换为字符串的形式返回


自动封装/拆封是指在基本数据类型值和其对应的包装类对象之间完成自动转换的过程。

自动封装 自动拆封


Integer obj1; nteger obj2=new Integer(22);


int num1=33;
int num2;

obj1=num1;
num2=obj2;
这种自动封装和自动拆封功能只能发生在基本数据类型和其所对应的包装类型之间。


日期相关类型:
Date类

java.util.Date类用于表示特定的时间点(精确到毫秒),该类是通过记录从基准时间(格林威治标准时间公元1970年1月1日00:00:00)开始到当前时刻的时间差,即所经历的毫秒数,来实现计时。

构造方法
1、 public Date() 创建Date对象(表示/记录当前的系统时间)

2、
public Date(long date)创建Date对象(表示/记录的是自基准时间点以来经历了date指定的毫秒数后的时刻


例如:Date d1 = new Date(20000);
Date d2 = new Date();


public int compareTo(Date anotherDate)
比较两个日期对象所记录的日期的前后顺序

public boolean equals(Object obj)
比较两个日期的等价性

public long getTime()
返回自基准时间点到当前时刻(当前对象表示的时间点)所经历的毫秒数

public String toString()
将当前时刻转换为字符串形式返回,其格式为:“星期 月份 日期 小时:分钟:秒 时区 年份”
java.util.Calendar类是Date类的一个增强版,该类提供了常规的日期修改功能和国际化支持,以及对日历操作提供方便。

Calendar类是一个抽象类,可以调用其静态方法getInstance()来获得该类的实例(实际上是其子类的实例):Calendar c=Calendar.getInstance();
基于当前时刻,使用默认(当前操作系统的)时区和语言环境获得的一个Calendar对象。

public void set(int field,int value)
将参数field指定的时间域(年、月、日、时、分、秒等)设置为参数value指定的值。field可为:Calendar.YEAR、Calendar.MONTH、Calendar.HOUR等

public final void set(int year,int month,int date)
设置当前日历的“年、月、日”三个时间域的值。

public final void set(int year,int month,int date,int hourOfDay,int minute,int second)
设置日历的“年、月、日、时、分、秒”等时间域的值。

public abcstract void add(int field,int amount)
为当前日历的指定时间域添加或减去指定的时间量。
Calendar c = Calendar.getInstance();
display(c);

c.set(Calendar.YEAR,2009);
c.set(Calendar.MONTH,4);
c.set(Calendar.DATE,30);
display(c);

c.set(2009,8,12);
display(c);

c.set(2009, 8, 12, 10, 23, 15);
display(c);

c.add(Calendar.MONTH,2);
display(c);

Date d = c.getTime();
System.out.println(d);


属性Calender.HOUR标记的时间域“小时”是12小时制的,要想获得24小时制的数值,则必须使用Calender.HOUR_OF_DAY来标记。
从Calender获取的月份信息值是从0开始的,即一月份的时间域获取值为0,因此display方法中将该字段的值加1已以得到实际的月份数。


java.text.DateFormat类提供了将日期/时间信息进行格式化处理的功能,主要是将日期/时间信息(Date类型数据)转换成人们所习惯的格式字符串以及反向转换的功能。
public static final DateFormat getDateInstance()
获得一个具有默认语言环境、默认格式化风格的DateFormat对象。

public static final DateFormat getDateInstance(int style,Locale aLocale)
获得具有给定语言环境、给定格式化风格的DateFormat对象

public static final DateFormat getTimeInstance()
获取具有默认语言环境、默认格式化风格的日期/时间DateFormat对象。

public final String format(Date date)
将一个Date对象格式化为日期/时间字符串。
DateFormat类中定义了相应的整型常量来标识各种转换风格,包括:DateFormat.SHORT、DateFormat.MEDIUM、DateFormat.LONG、DateFormat.FULL。

Locale locale1=new Locale(“zh”,”CN”);
//根据指定的语言和国家/地区构造一个语言环境Locale对象
国家/地区代码
ChinaCN
TaiwanTW
GermanyDE
Great BritainGB
ItalyIT
JapanJP
SpanSP
United StatesUS


语言代码
Chinesezh
Englishen
French fr
Germande
Italianit
Japanese ja
Spanishsp

java.text.SimpleDateFormat类是DateFormat类的子类,它方便用户自己定义日期、时间表示格式,并提供更灵活的日期和字符串信息转换和解析的功能。
SimpleDateFormat类的使用。

Locale locale1;
SimpleDateFormat sdf1,sdf2;

Date d=new Date();
locale1 = new Locale("zh","CN");

sdf1 = new SimpleDateFormat();

sdf2 = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");//模式串


System.out.println(sdf2.format(d));


模式串中可以包含模式符以外的字符或字符串,如“年”、“月”、“日”等字符,这些字符或字符串在转换过程中将被原样保留;

如果要在模式串中包含字母(a~z,A~Z)且不希望其被当做模式符转换掉,则可以将其用半角单引号“‘”括起来;

由于单引号已被用作分隔标记,如果在模式串中要作为普通字符来包含它,就需要使用两个连续的半角单引号(“''”,注意这不同于一个双引号)来代替。例如,"'On' yyyy-MM-dd HH 'o''clock,We tesed it.'"


基本数学功能类Math
:
java.lang.Math类提供了常用的数学运算功能和数学常量,其中的属性和方法均被定义为public和static的,因此不需要创建Math的实例即可直接访问或调用。

public static double random() 生成double型随机数,其取值区间为[0.0,1.0)

例如,int i=(int)(Math.random()*100);
生成0~99之间的随机整数


java.text.NumberFormat:所有数据格式的抽象基类 功能:提供了将数字(包括整型和浮点型)格式化为符合特定语言环境(Locale)表示习惯的字符串以及逆向解析字符串为数字的功能
public static final NumberFormat getInstance()
返回当前默认语言环境的通用数字格式对象。

public static NumberFormat getInstance(Locale intLocale)
返回指定语言环境的通用数字格式对象。
public final String format(double number)
格式化指定的数据,返回格式化字符串。

public static final NumberFormat getCurrencyInstance()
返回默认语言环境的货币格式对象。

public static final NumberFormat getCurrencyInstance(Locale inLocale)
返回默认语言环境的货币格式对象。

public static final NumberFormat getPercentInstance()
返回当前默认语音那环境的百分比格式对象。

public static NumberFormat getPercentInstance(Locale inLocale)返回指定语言环境的百分比格式对象。
实际上货币数值输出时不大可能随便改变其货币符号,除非同时进行相应的汇率转换,否则后果会很严重,而小数表示为百分比形式各语言环境中没什么分别。

java.text.DecimalFormat:是NumberFormat的子类 功能:用于格式化各种形式的十进制(整数、浮点数、科学记数法表示的数据、百分数和金额(如$20)等
)数据为当前语言环境相关的字符串表示形式以及反向的解析操作。
public DecimalFormat()
使用默认模式语言环境创建DecimalFormat对象

public DecimalFormat(String pattern//模式字符串
)
给定模式和默认语言环境创建DecimalFormat对象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值