靜態(tài)導(dǎo)入
- 靜態(tài)導(dǎo)入可以作用一個類的所有靜態(tài)成員谊囚。
- 靜態(tài)導(dǎo)入的格式 : import static 包名.類名.靜態(tài)的成員镰踏;
import static java.util.Collections.sort;
import static java.util.Collections.binarySearch;
import static java.util.Collections.max; - 靜態(tài)導(dǎo)入要注意的事項:
- 如果靜態(tài)導(dǎo)入的成員與本類的成員存在同名的情況下余境,那么默認(rèn)使用本類的靜態(tài)成員芳来,如果需要指定使用靜態(tài)導(dǎo)入的成員,那么需要在靜態(tài)成員前面加上類名
import static java.util.Collections.sort;
import static java.util.Collections.binarySearch;
import static java.util.Collections.max;
import static java.lang.System.out;
public class Demo{
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(13);
list.add(9);
list.add(10);
list.add(19);
//排序
Collections.sort(list);
out.println("集合的元素:"+ list);
out.println("索引值:"+ binarySearch(list,13));
out.println("最大值:"+ max(list));
}
public static void sort(ArrayList<Integer> list){
System.out.println("本類 的sort方法.....");
}
}
增強(qiáng)for循環(huán)
- 增強(qiáng)for循環(huán)的作用: 簡化迭代器的書寫格式顽聂。(注意:增強(qiáng)for循環(huán)的底層還是使用了迭代器遍歷)
- 增強(qiáng)for循環(huán)的適用范圍: 如果是實(shí)現(xiàn)了Iterable接口的對象或者是數(shù)組對象都可以使用增強(qiáng)for循環(huán)
- 增強(qiáng)for循環(huán)的格式:
for(數(shù)據(jù)類型 變量名 :遍歷的目標(biāo)){} - 增強(qiáng)for循環(huán)要注意的事項:
- 增強(qiáng)for循環(huán)底層也是使用了迭代器獲取的蜜葱,只不過獲取迭代器由jvm完成耀石,不需要我們獲取迭代器而已揭鳞,所以在使用增強(qiáng)for循環(huán)變量元素的過程中不準(zhǔn)使用集合對象對集合的元素個數(shù)進(jìn)行修改
- 迭代器遍歷元素與增強(qiáng)for循環(huán)變量元素的區(qū)別:使用迭代器遍歷集合的元素時可以刪除集合的元素野崇,而增強(qiáng)for循環(huán)變量集合的元素時乓梨,不能調(diào)用迭代器的remove方法刪除元素
- 普通for循環(huán)與增強(qiáng)for循環(huán)的區(qū)別:普通for循環(huán)可以沒有變量的目標(biāo),而增強(qiáng)for循環(huán)一定要有變量的目標(biāo)
public class Demo {
public static void main(String[] args) {
HashSet<String> set = new HashSet<String>();
//添加元素
set.add("狗娃");
set.add("狗剩");
set.add("鐵蛋");
/*
//使用迭代器遍歷Set的集合.
Iterator<String> it = set.iterator();
while(it.hasNext()){
String temp = it.next();
System.out.println("元素:"+ temp);
it.remove();
}
//使用增強(qiáng)for循環(huán)解決
for(String item : set){
System.out.println("元素:"+ item);
}
int[] arr = {12,5,6,1};
普通for循環(huán)的遍歷方式
for(int i = 0 ; i<arr.length ; i++){
System.out.println("元素:"+ arr[i]);
}
//使用增強(qiáng)for循環(huán)實(shí)現(xiàn)
for(int item :arr){
System.out.println("元素:"+ item);
}
//需求: 在控制臺打印5句hello world.
for(int i = 0 ; i < 5; i++){
System.out.println("hello world");
}
*/
//注意: Map集合沒有實(shí)現(xiàn)Iterable接口睛蛛,所以map集合不能直接使用增強(qiáng)for循環(huán),如果需要使用增強(qiáng)for循環(huán)需要借助于Collection
// 的集合。
HashMap<String, String> map = new HashMap<String, String>();
map.put("001","張三");
map.put("002","李四");
map.put("003","王五");
map.put("004","趙六");
Set<Map.Entry<String, String>> entrys = map.entrySet();
for(Map.Entry<String, String> entry :entrys){
System.out.println("鍵:"+ entry.getKey()+" 值:"+ entry.getValue());
}
}
}
自定義增強(qiáng)for循環(huán)
import java.util.Iterator;
//自定一個類使用增強(qiáng)for循環(huán)
class MyList implements Iterable<String>{
Object[] arr = new Object[10];
int index = 0 ; //當(dāng)前的指針
public void add(Object o){
arr[index++] = o; // 1
}
public int size(){
return index;
}
@Override
public Iterator<String> iterator() {
return new Iterator<String>() {
int cursor = 0;
@Override
public boolean hasNext() {
return cursor<index;
}
@Override
public String next() {
return (String) arr[cursor++];
}
@Override
public void remove() {
}
};
}
}
public class Demo {
public static void main(String[] args) {
MyList list = new MyList();
list.add("張三");
list.add("李四");
list.add("王五");
for(String item :list){
System.out.println(item);
}
}
}
jdk1.5可變參數(shù)
- 可變參數(shù)的格式:數(shù)據(jù)類型... 變量名
- 可變參數(shù)要注意的細(xì)節(jié):
- 如果一個函數(shù) 的形參使用上了可變參數(shù)之后旭从,那么調(diào)用該方法的時候可以傳遞參數(shù)也可以不傳遞參數(shù)
- 可變參數(shù)實(shí)際上是一個數(shù)組對象
- 可變參數(shù)必須位于形參中的最后一個參數(shù)
- 一個函數(shù)最多只能有一個可變參數(shù),因?yàn)榭勺儏?shù)要位于形參中最后一個位置上
需求 : 定義一個函數(shù)做加法功能(函數(shù)做幾個數(shù)據(jù)的加法功能是不確定)
public class Demo {
public static void main(String[] args) {
int[] arr = {1,2,45,6,7};
/*System.out.println(arr);
add(arr);*/
add();
}
public static void add(int... arr){ //長度是0
int result = 0;
for(int item : arr){
result+=item;
}
System.out.println("總和:"+ result);
}
}
自動裝箱與自動拆箱
簡介: java是面向?qū)ο蟮恼Z言鸽素,任何事物都可以使用類進(jìn)行描述馍忽,sun就使用了一些類描述java中八種基本數(shù)據(jù)類型數(shù)據(jù)
基本數(shù)據(jù)類型 包裝類型
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
char Character
public class Demo {
public static void main(String[] args) {
String str = "12";
//字符串轉(zhuǎn)換成int類型數(shù)據(jù)坝冕。 可以把字符串轉(zhuǎn)換成對應(yīng)的數(shù)字
int i = Integer.parseInt(str);
System.out.println(i+1);
//把數(shù)字轉(zhuǎn)換成字符串
System.out.println("把整數(shù)轉(zhuǎn)換成對應(yīng) 的字符串:"+Integer.toString(i));
//把整數(shù)轉(zhuǎn)換成對應(yīng)的進(jìn)制形式
System.out.println("10的二進(jìn)制:"+ Integer.toBinaryString(10));
System.out.println("10的二進(jìn)制:"+ Integer.toBinaryString(10));
System.out.println("10的十六進(jìn)制:"+ Integer.toHexString(10));
//可以把字符串當(dāng)成對應(yīng)的進(jìn)行數(shù)據(jù)幫你轉(zhuǎn)換
String data = "10";
int a = Integer.parseInt(data, 2);
System.out.println("a="+a);
//集合: 集合是可以存儲任意對象類型數(shù)據(jù)的容器徽诲。
ArrayList list = new ArrayList();
list.add(1);
list.add(2);
list.add(3);
//自動裝箱: 自動把java的基本數(shù)據(jù)類型數(shù)據(jù)轉(zhuǎn)換成對象類型數(shù)據(jù)谎替。
int temp = 10; //基本數(shù)據(jù)類型
Integer b =temp; //把a(bǔ)存儲的值賦予給b變量钱贯。
//自動拆箱: 把引用類型的數(shù)據(jù)轉(zhuǎn)換成基本類型的數(shù)據(jù)
Integer c = new Integer(13);
int d = c; //
System.out.println(d);
//引用的數(shù)據(jù)類型
Integer e = 128;
Integer f = 128;
System.out.println("同一個對象嗎秩命?"+(e==f)); // Integer類內(nèi)部維護(hù) 了緩沖數(shù)組,該緩沖數(shù)組存儲的-128~127 這些數(shù)據(jù)在一個數(shù)組中霹菊。如果你獲取的數(shù)據(jù)是落入到這個范圍之內(nèi)的,那么就直接從該緩沖區(qū)中獲取對應(yīng)的數(shù)據(jù)支竹。
}
}
枚舉
問題:某些方法所接收的數(shù)據(jù)必須是在固定范圍之內(nèi)的
解決方案: 這時候我們的解決方案就是自定義一個類, 然后是私有化構(gòu)造函數(shù)旋廷,在自定義類中創(chuàng)建本類的對象對外使用
jdk1.5對以上問題提出了新的解決方案: 就是使用枚舉類解決
一些方法在運(yùn)行時,它需要的數(shù)據(jù)不能是任意的礼搁,而必須是一定范圍內(nèi)的值饶碘,Java5以后可以直接使用枚舉予以解決。比如: 方向 , 性別 馒吴、 季節(jié) 扎运、 星期......
//自定義一個性別類
class Gender{
String value;
public static final Gender man = new Gender("男");
public static final Gender woman = new Gender("女");
private Gender(String value) {
this.value = value;
}
}
enum Gender{
man("男"),woman("女");
String value;
private Gender(String value){
this.value = value;
}
}
class Person{
private String name;
private Gender sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Gender getSex() {
return sex;
}
public void setSex(Gender sex) {
this.sex = sex;
}
}
public class Demo {
public static void main(String[] args) {
Person p = new Person();
p.setName("狗娃");
p.setSex(Gender.woman);
System.out.println("名字:"+ p.getName()+" 性別:"+ p.getSex().value);
}
}
- 枚舉:一些方法在運(yùn)行時,它需要的數(shù)據(jù)不能是任意的饮戳,而必須是一定范圍內(nèi)的值鬼吵,可以直接使用枚舉予以解決
- 枚舉類的定義格式:
enum 類名{
//枚舉值
} - 枚舉要注意的細(xì)節(jié):
- 枚舉類也是一個特殊的類。
- 枚舉值默認(rèn)的修飾符是public static final
- 枚舉值就是是枚舉值所屬的類的類型, 然后枚舉值是指向了本類的對象的
- 枚舉類的構(gòu)造方法默認(rèn)的修飾符是private的
- 枚舉類可以定義自己的成員變量與成員函數(shù)
- 枚舉類可以自定義構(gòu)造函數(shù),但是構(gòu)造函數(shù)的修飾符必須是private
- 枚舉類可以存在抽象的方法,但是枚舉值必須要實(shí)現(xiàn)抽象的方法
- 枚舉值必須要位置枚舉類的第一個語句
enum Sex{
man("男"){
@Override
public void run() {
System.out.println("男人在跑...");
}
},woman("女"){
@Override
public void run() {
System.out.println("女人在跑...");
}
}; //枚舉值
String value; //成員 變量
// public static final Sex man = new Sex();
//構(gòu)造函數(shù)
private Sex(String value){
this.value = value;
}
//成員函數(shù)
public void getValue(){
System.out.println("value :"+ value);
}
public abstract void run();
}
public class Demo {
public static void main(String[] args) {
Sex sex = Sex.man; //獲取到了枚舉類的對象
sex.value = "男";
sex.getValue();
sex.run();
}
}
switch與枚舉類的應(yīng)用
- switch適用的數(shù)據(jù)類型: byte \ char \short \ int \ String\枚舉類型
- 注意:case語句后面跟的枚舉值贬派,只需要單寫枚舉值即可请敦,不需要再聲明該 枚舉值是屬于哪個枚舉類的
//季節(jié)枚舉類
enum Season{
spring,summer,autumn,winter;
}
enum Person2{
student,worker;
}
public eclass Demo {
public static void main(String[] args) {
Season season = Season.summer;
switch(season){
case spring:
System.out.println("春天...");
break;
case summer:
System.out.println("夏天...");
break;
case autumn:
System.out.println("秋天...");
break;
case winter:
System.out.println("冬天...");
break;
}
}
}
可變參數(shù)
- 需求 : 定義一個函數(shù)做加法功能(函數(shù)做幾個數(shù)據(jù)的加法功能是不確定)
- 可變參數(shù)的格式 :
數(shù)據(jù)類型... 變量名
- 可變參數(shù)要注意的細(xì)節(jié):
- 如果一個函數(shù)的形參使用上了可變參數(shù)之后桥滨,那么調(diào)用該方法的時候可以傳遞參數(shù)也可以不傳遞參數(shù)
- 可變參數(shù)實(shí)際上是一個數(shù)組對象
- 可變參數(shù)必須位于形參中的最后一個參數(shù)
- 一個函數(shù)最多只能有一個可變參數(shù)喻括,因?yàn)榭勺儏?shù)要位于形參中最后一個位置上
public class Demo {
public static void main(String[] args) {
int[] arr = {1,2,45,6,7};
/*System.out.println(arr);
add(arr);*/
add();
}
public static void add(int... arr){ //長度是0
int result = 0;
for(int item : arr){
result+=item;
}
System.out.println("總和:"+ result);
}
}
自動裝箱與自動拆箱
- 自動裝箱 : 自動把java的基本數(shù)據(jù)類型數(shù)據(jù)轉(zhuǎn)換成對象類型數(shù)據(jù)
- 自動拆箱 : 把引用類型的數(shù)據(jù)轉(zhuǎn)換成基本類型的數(shù)據(jù)
- java是面向?qū)ο蟮恼Z言脖律,任何事物都可以使用類進(jìn)行描述冕杠,sun就使用了一些類描述java中八種基本數(shù)據(jù)類型數(shù)據(jù):
- 基本數(shù)據(jù)類型: byte, short, int, long, float, double, boolean, char
- 包裝類型 : Byte, Short, Integer, Long, Float, Double, Boolean, Character
public class Demo {
public static void main(String[] args) {
String str = "12";
//字符串轉(zhuǎn)換成int類型數(shù)據(jù)噪舀。 可以把字符串轉(zhuǎn)換成對應(yīng)的數(shù)字
int i = Integer.parseInt(str);
System.out.println(i+1);
//把數(shù)字轉(zhuǎn)換成字符串
System.out.println("把整數(shù)轉(zhuǎn)換成對應(yīng) 的字符串:"+Integer.toString(i));
//把整數(shù)轉(zhuǎn)換成對應(yīng)的進(jìn)制形式
System.out.println("10的二進(jìn)制:"+ Integer.toBinaryString(10));
System.out.println("10的二進(jìn)制:"+ Integer.toBinaryString(10));
System.out.println("10的十六進(jìn)制:"+ Integer.toHexString(10));
//可以把字符串當(dāng)成對應(yīng)的進(jìn)行數(shù)據(jù)幫你轉(zhuǎn)換
String data = "10";
int a = Integer.parseInt(data, 2);
System.out.println("a="+a);
//集合: 集合是可以存儲任意對象類型數(shù)據(jù)的容器息拜。
ArrayList list = new ArrayList();
list.add(1);
list.add(2);
list.add(3);
//自動裝箱: 自動把java的基本數(shù)據(jù)類型數(shù)據(jù)轉(zhuǎn)換成對象類型數(shù)據(jù)赞别。
int temp = 10; //基本數(shù)據(jù)類型
Integer b =temp; //把a(bǔ)存儲的值賦予給b變量。
//自動拆箱: 把引用類型的數(shù)據(jù)轉(zhuǎn)換成基本類型的數(shù)據(jù)
Integer c = new Integer(13);
int d = c; //
System.out.println(d);
//引用的數(shù)據(jù)類型
Integer e = 128;
Integer f = 128;
System.out.println("同一個對象嗎屿笼?"+(e==f)); // false (-128~127中就是true)
// Integer類內(nèi)部維護(hù) 了緩沖數(shù)組,該緩沖數(shù)組存儲的-128~127 這些數(shù)據(jù)在一個數(shù)組中。如果你獲取的數(shù)據(jù)是落入到這個范圍之內(nèi)的,那么就直接從該緩沖區(qū)中獲取對應(yīng)的數(shù)據(jù)
}
}
枚舉類
- 枚舉類的引入 :
- 問題 : 某些方法所接收的數(shù)據(jù)必須是在固定范圍之內(nèi)的
- 解決方案 : 這時候我們的解決方案就是自定義一個類,然后是私有化構(gòu)造函數(shù)间唉,在自定義類中創(chuàng)建本類的對象對外使用。jdk1.5對以上問題提出了新的解決方案: 就是使用枚舉類解決一些方法在運(yùn)行時轮蜕,它需要的數(shù)據(jù)不能是任意的闲延,而必須是一定范圍內(nèi)的值,Java5以后可以直接使用枚舉予以解決。比如: 方向 , 性別 档冬、 季節(jié) 、 星期......
/*
//自定義一個性別類
class Gender{
String value;
public static final Gender man = new Gender("男");
public static final Gender woman = new Gender("女");
private Gender(String value) {
this.value = value;
}
}
*/
enum Gender{
man("男"),woman("女");
String value;
private Gender(String value){
this.value = value;
}
}
class Person{
private String name;
private Gender sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Gender getSex() {
return sex;
}
public void setSex(Gender sex) {
this.sex = sex;
}
}
public class Demo{
public static void main(String[] args) {
Person p = new Person();
p.setName("娃");
p.setSex(Gender.woman);
System.out.println("名字:"+ p.getName()+" 性別:"+ p.getSex().value);
}
}
- 枚舉:一些方法在運(yùn)行時,它需要的數(shù)據(jù)不能是任意的掰伸,而必須是一定范圍內(nèi)的值,可以直接使用枚舉予以解決
- 枚舉類的定義格式:
enum 類名{
//枚舉值
}
- 枚舉要注意的細(xì)節(jié):
- 枚舉類也是一個特殊的類
- 枚舉值默認(rèn)的修飾符是public static final
- 枚舉值就是枚舉值所屬的類的類型, 然后枚舉值是指向了本類的對象的
- 枚舉類的構(gòu)造方法默認(rèn)的修飾符是private的
- 枚舉類可以定義自己的成員變量與成員函數(shù)
- 枚舉類可以自定義構(gòu)造函數(shù),但是構(gòu)造函數(shù)的修飾符必須是private
- 枚舉類可以存在抽象的方法妒貌,但是枚舉值必須要實(shí)現(xiàn)抽象的方法
- 枚舉值必須要位置枚舉類的第一個語句
//自定義一個枚舉類
enum Sex{
man("男"){
@Override
public void run() {
System.out.println("男人在跑...");
}
},woman("女"){
@Override
public void run() {
System.out.println("女人在跑...");
}
}; //枚舉值
String value; //成員 變量
// public static final Sex man = new Sex();
//構(gòu)造函數(shù)
private Sex(String value){
this.value = value;
}
//成員函數(shù)
public void getValue(){
System.out.println("value :"+ value);
}
public abstract void run();
}
public class Demo{
public static void main(String[] args) {
Sex sex = Sex.man; //獲取到了枚舉類的對象
sex.value = "男";
sex.getValue();
sex.run();
}
}
switch
- switch適用的數(shù)據(jù)類型: byte \ char \short \ int \ String\枚舉類型
- 注意 : case語句后面跟的枚舉值,只需要單寫枚舉值即可靡馁,不需要再聲明該枚舉值是屬于哪個枚舉類的
//季節(jié)枚舉類
enum Season{
spring,summer,autumn,winter;
}
enum Person2{
student,worker;
}
public eclass Demo {
public static void main(String[] args) {
Season season = Season.summer;
switch(season){
case spring:
System.out.println("春天...");
break;
case summer:
System.out.println("夏天...");
break;
case autumn:
System.out.println("秋天...");
break;
case winter:
System.out.println("冬天...");
break;
}
}
}
反編譯
javap -c -l -private Sex // Sex是你需要查看反編譯的東西