java(doc)基礎知識
Object裡面的所有方法:
clone:
複製物件,副本
wait:讓當前執行緒等待。
notify:喚醒當前執行緒
equals:判斷兩個值是否相等
toString:顯示當前物件的地址
notifyAll:喚醒所有等待執行緒。
finalize:垃圾回收最後一次判斷該堆是否被引用。
hashCode:返回本物件的hash碼。
getClass:返回其物件的 類 執行時類 物件
人 魂 你
java共有48個關鍵字(關鍵字都是小寫的,但小寫的不一定是關鍵字)
java基本型別:primitive type(只有棧)
整數型別:byte位元組(8位),short短的(16位-2位元組),int(32位-4位元組),long長(64位-8位元組)
浮點型別:float單精度浮點(32-4位元組),double雙精度浮點(64 - 8位元組)
字元型別:char字元(16位-2位元組)
布林型別:boolean布林(1位)
java的引用型別:reference type(棧和堆都有)
指標型別,null,array,class,interface,enum(列舉)......
棧:stack
堆:heap
全域性變數:global variable
區域性變數:local variable
區域性變數的優先順序大於全域性變數
全域性變數的初始預設值:int:0,char字元:'\ u0000's',boolean布林:false
區域性變數使用前必須初始化
1 javac HelloWorld.java(doc下編譯)
2 java HelloWorld(doc下解釋)
3 javadoc -d api -windowtitle title -header page brow -footer頁尾HelloWorld.java(生成文件註釋)
final:
變數:最終變數,不能被修改
方法:不能被重寫
類:不能被繼承 (String,Math)
抽象類(abstract):沒有方法體,不能被例項化,非私有,和類的關係是繼承,可以有變數也可以有常量。
1.如果方法沒有方法體,body,我們把這個方法宣告為abstract.
2.抽象類不能被例項化。
3.抽象類的抽象成員必須是非私有的,否則不能被繼承
4.一般方法可以有4個作用域修飾符。private default protected public
5.類和抽象類關係是繼承
6.沒有抽象方法的抽象類可以存在,但是沒有必要,因為他不能例項化。
7.抽象類既可以有變數,又可以有常量
介面(interface):都是抽象方法,所有方法必須是公共的,類可以實現多個介面,先繼承後實現,介面與介面是繼承, 介面可以繼承多個介面,介面中的變數都是常量。
沒有一般方法,都是抽象方法,
所有方法必須是公共的。
類和介面關係是實現implements,且可以實現多個介面,完成了靈活的特性
類和類,介面關係如果放在一起,必須是先繼承在實現。
介面與介面關係是繼承
介面可以繼承多個介面
介面和類沒關係
介面中的變數都是常量。
多型:所謂多型,就是指一個引用(型別)在不同的情況下的多種狀態。也可以理解為,多型是指通過指向父類的指標, 來呼叫在不同子類中實現的方法。
多型:子類繼承父類,一個類物件既可以指自己,也可以指其子類物件。
三種練習:解決了大量具有繼承關係的方法的個數問題
a.向上轉型,虛方法呼叫【編譯型別和執行型別不同】。如果你想使用兒子特殊的方法,必須強轉。
b.形參多型。
c.返回值多型
場景假設:
一個主人養了貓和狗,貓和狗都有自己愛吃的東西,主人在餵它們的時候,如果既要判斷是貓還是狗,再判斷他們分別愛吃什麼,就顯得很麻煩。如果主人養了很多種動物,這樣的重複判斷,就會浪費很多時間。有什麼辦法,能讓主人拿到一種食物就知道這是哪種動物的,就好了。
1.首先,創造動物類:
// 動物類
class Animal {
int age;
String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// 動物類裡面有叫和吃兩個方法
public void cry() {
System.out.println("我不知道叫什麼");
}
public void eat() {
System.out.println("我不知道吃什麼");
}
}
2.其次,分別創造貓類和狗類(他們繼承於動物類):
// 狗類繼承於動物類
class Dog extends Animal {
// 覆蓋(重寫)方法
public void cry() {
System.out.println("旺旺");
}
public void eat() {
System.out.println("我是狗,我愛吃骨頭");
}
}
// 貓類繼承於動物類
class Cat extends Animal {
// 覆蓋(重寫)方法
public void cry() {
System.out.println("喵喵");
}
public void eat() {
System.out.println("我是貓,我愛吃魚");
}
}
3.再者,建立食物類:
// 食物類
class Food {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// 食物類裡面讓它有一個方法
public void showName() {
}
}
4.再者,貓和狗都有自己不同的愛吃的食物(他們繼承於食物類):
// 魚(食物的一種)繼承於食物
class Fish extends Food {
public void showName() {
System.out.println("食物:魚");
}
}
// 骨頭(食物的一種)繼承於食物
class Bone extends Food {
public void showName() {
System.out.println("食物:骨頭");
}
}
5.主人類(就可以將動物和對應的食物統一起來):
// 主人類 存在一種餵食方法
class Master {
// 給動物餵食物,如果沒有多型,他要寫給貓餵食和給狗餵食兩個方法
// 有了多型,以後即使再來好多動物,用這一個函式就可以了
public void feed(Animal an, Food f) {
an.eat();
f.showName();
}
}
6.最後,方法的呼叫(測試):
public class DuoTaiDemo {
public static void main(String args[]) {
Master master = new Master();
master.feed(new Dog(), new Bone());
// hin方便,可以再試試
master.feed(new Cat(), new Fish());
}
}
7.執行結果:
匿名內部類:(立刻實現介面)
重寫equals方法:
package com.lijiaxing.A;
public class D {
private int month;
private int year;
private int day;
public D(int year, int month, int day) {
this.month = month;
this.year = year;
this.day = day;
}
//重寫equals方法
@Override
public boolean equals(Object obj) {
if (this==obj){
return true;
}
if (this.getClass()!=obj.getClass()){//執行時類(eg:人類!=動物類)
return false;
}
if (obj==null){//沒有堆
return false;
}
D d =(D)obj;
if ((this.year!=d.year)||(this.month!=d.month)||(this.day!=d.day)){
return false;
}
return true;
}
public static void main(String[] args) {
D d = new D(2018, 11, 6);
D d1 = new D(2018, 11, 6);
System.out.println(d.equals(d1));
}
}
concat:連線字串
流:
FileInputStream ---FileOutputStream //檔案輸入輸出流
BufferedInputStream ---BufferedOutputStream //快取流
BufferedReader ---BufferedWriter
InputStreamReader ---OutputStreamWriter
System.in ---Scannner
ByteArrayInputStream ---ByteArrayOutputStream
RandomAccessFile ---seek ---Merge
Serializable:序列化
用流讀/寫(writeFile)檔案並在控制檯輸出檔案(readFile)並拷貝檔案(copyFile):
package com.lijiaxing.F;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
public class B {
//寫主方法太麻煩,採用註解方式進行測試
@Test
public void copyFile() {//拷貝檔案
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream("c://mrbzms.mp4");//拷貝路徑
fileOutputStream = new FileOutputStream("d://1.mp4");//拷貝到路徑
int n = -1;
byte[] buffer = new byte[1024 * 1024 * 10];//10M
long start = System.currentTimeMillis();//獲取開始時間
while ((n = fileInputStream.read(buffer)) != -1) {//如果等於-1證明檔案已經讀完
fileOutputStream.write(buffer, 0, n);//寫入內容
}
long end = System.currentTimeMillis();//獲取結束時間
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss");//用SimpleDateFormat輸出mm:ss的時間格式
System.out.println(simpleDateFormat.format(end - start));//輸出結束時間-開始時間(用時)
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void writeFile() {//寫檔案
FileOutputStream fileOutputStream = null;//需要處理FileOutputStream的異常
try {
fileOutputStream = new FileOutputStream("d://33.txt");//要寫入(儲存)的檔案位置
try {
fileOutputStream.write("你好,世界".getBytes());//寫入內容,write方法需要處理異常
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
fileOutputStream.close();//呼叫close()方法時必須處理它的異常
System.out.println("已關閉");
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void readFile() {//讀檔案
FileInputStream fileInputStream = null;//需要處理FileInputStream的異常
try {
byte[] buffer = new byte[1024];//一次1K
int n = -1;
fileInputStream = new FileInputStream("d://122.sql");//檔案位置
while ((n = fileInputStream.read(buffer)) != -1) {//如果等於-1證明檔案已經讀完
System.out.println(new String(buffer));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();//呼叫close()方法時必須處理它的異常
System.out.println("已關閉檔案輸入流!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
HashMap轉成List並排序!!!
package com.lijiaxing.F;
import org.junit.Test;
import java.util.*;
public class D {
@Test
public void go(){
HashMap<String,Integer> hashMap = new HashMap();
hashMap.put("A",100);
hashMap.put("B",200);
hashMap.put("C",300);
hashMap.put("E",400);
hashMap.put("F",500);
hashMap.put("G",600);
hashMap.put("H",700);
hashMap.put("I",800);
hashMap.put("J",900);
hashMap.put("K",1000);
hashMap.put("L",1100);
hashMap.put("M",1200);
Collection<Integer> collection = hashMap.values();
// Iterator iterator = collection.iterator();
// while (iterator.hasNext()){
// System.out.println(iterator.next());
// }
for (Integer integer:collection){
System.out.println(integer);
}
//*******
//把hashMap轉成list做排序
List<Map.Entry<String,Integer>> list = new ArrayList(hashMap.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o1.getKey().compareTo(o2.getKey());
}
});
System.out.println("-------------------------------------------------------");
for (int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
}
執行緒方法