天天看點

Java基礎教程--安卓入門教程(七)

Java基礎教程--安卓入門教程(七)

接口的基本文法

接口的基本文法(一)

使用interface定義

接口當中的方法都是抽象方法;

接口當中的方法都是public權限

接口中全是抽象函數,不能生成對象

interface USB{

 public void read();

 public void write();

}

class USBPhone implements USB{

//複寫

 public void read(){

  System.out.println("USBPhone read");

 }

 public void write(){

  System.out.println("USBPhone write");

class Test{

 public static void main(String args[]){

  USBPhone usbPhone = new USBPhone();

  USB usb = usbPhone;

  usb.read();

  usb.write();

實作接口使用implements關鍵字

一個類可以實作多個接口

一個接口可以繼承多個接口

class Phone implements USB,WiFi{

  System.out.println("USB read");

  System.out.println("USB write");

 public void open(){

  System.out.println("USB open");

 public void close(){

  System.out.println("USB close");

interface WiFi{

 public void open();

 public void close();

  Phone phone = new Phone();

  USB usb = phone;

 WiFi wifi = phone;

 wifi.open();

 wifi.close();

什麼是接口的應用

為什麼要用接口

工廠方法模式

class HPPrinter implements Printer{

  System.out.println("HP open");

  System.out.println("HP close");

 public void print(String s){

  System.out.println("HP print-->" + s);

class CanonPrinter implements Printer{

 private void clean(){

  System.out.println("clean");

  this.clean();

  System.out.println("Canon close");

  public void open(){

  System.out.println("Canon opern");

  public void print(String s){

   System.out.println("Canon print--->"+s);

  }

interface Printer{

 public void print(String s);

  //根據使用者的選擇,生成相應的列印機對象

  //并且向上轉型為Printer類型

  //Printer getPrinter(int flag)

  printer.open();

  printer.print("test");

  printer.close();

class PrinterFactory{

 public static Printer getPrinter(int flag){

   Printer printer = null;

  //int flag = 0;

  if(flag == 0){

   printer = new HPPrinter();

  else(flag == 1){

   printer = new CanonPrinter();

  return printer;

  int flag = 1;

  Printer printer = PrinterFactory.getPrinter(flag);

}                                                                                       

什麼是異常

異常的分類

try...catch...finally結構的使用方法

異常:中斷了正常指令流的事件。

Throwable Exception Error

class TestCheck{

  //check exception

  Thread.sleep(1000);

  System.out.println(1);

  try{

  System.out.println(2);

   int i = 1 / 0;

  System.out.println(3);

  catch(Exception e){

   e.printStackTrace();

   System.out.println(4);

  finally{

   System.out.println("finally");

  System.out.println(5);

   Thread.sleep(1000);

30總結

程式員對Error無能為力,隻能處理Exception

對異常的處理關系到系統的健壯性

使用try...catch...finally來處理可能出現異常的代碼

throws的作用

class User{

 private int age;

 public void setAge(int age) throws Exception{

  if(age<0){

   RuntimeException e = new RuntimeException("年齡不能為負數");

   throw e;

  this.age=age

  User user = new User();

   user.setAge(-20);

   System.out.println(e);

throws Exception誰調用誰處理

32Java當中的IO

IO操作的目标

IO的分類方法

讀取檔案和寫入檔案的方法

從資料源當中讀取資料,以及将資料寫入到資料目的地當中。

第一種分法:

輸入流

輸出流

第二種分類:

位元組流

字元流

第三種分類:

節點流

處理流

InputStream OutputStream FileInputStream FileOnputStream

位元組流的核心類

InputStream OutputStream所有位元組流的父類 抽象類,是不能生成對象的,子類可以生成對象。

核心類的核心方法

InputStream:   

int read(byte[] b,int off,int len)

OutputStream:

void write(byte[] b,int off,int len)

//第一步:導入類,

import java.io.*;

    //聲明輸入流引用

    FileInputStream fis = null;

   //讀取檔案資料的 輸入流,FileInputStream

   try{

     //生成代表輸入流的對象

     fis = new FileInputStream("e:/src/from.text");

     //讀取資料

     //生成一個位元組數組

     byte [] buffer = new byte[100];

     //調用輸入流對象的read方法,讀取資料

     //0,代表偏移量,如果是5,那麼就從第五個讀取

     fis.read(buffer,0,buffer.length);

     for(int i = 0;i<buffer.length;i++){

      System.out.println(buffer[i]);

     }

   }

   catch(Exception e){

    System.out.println(e);

    //聲明輸出流的引用

   FileOutputStream fos = null;

     fis = new FileInputStream("e:/src/from.txt");

     //生成代表輸出流的對象

     fos = new FileOutputStream("e:/src/to.txt");

     int temp = fis.read(buffer,0,buffer.length);

     fos.write(buffer,0,temp);

    /* String s = new String(buffer);

     //調用一個String對象的trim方法,将會去除掉這個字元串的

     //首尾空格和空字元

     s = s.trim();

     System.out.println(s);*/

資料的流向以JAVA程式為參照物

IO流可以有三種分類方法

read方法和write方法

大檔案的讀寫方法

字元流的使用方法

     byte [] buffer = new byte[1024];

     while(true){

       int temp = fis.read(buffer,0,buffer.length);

       if(temp == -1){

         break;

       }

       fos.write(buffer,0,temp);

   finally{

     try{

     fis.close();

     fos.close(); 

    }

    catch(Exception e){

      System.out.println(e);

位元組輸入流:Reader <--FileReader 

int read(char[] c,int off,int len)

位元組輸出流:Writer <--FileWriter

void write(char[] c,int off,int len)

public class TestChar{

   //字元資料流 FileReader

  FileReader fr = null;

  FileWriter fw = null;

    fr = new FileReader("e:/src/from.txt");

    fw = new FileWriter("e:;/src/to.txt");

    char[] buffer = new char[100];

    int temp = fr.read(buffer,0,buffer.length);

    fw.write(buffer,0,temp);

    /*for(int i = 0;i<buffer.length;i++){

     System.out.println(buffer[i]);

   }*/

34

處理流使用執行個體

裝飾者(Decorator)模式

節點流與處理流的關系

BufferedReader介紹

一行一行的讀取

BufferedReader使用方法,生成BufferedReader對象的方法

BufferedReader in = new BufferedReader(new FileReader("foo.in"));

處理流,Reader,Writer以及他們所有的子類都屬于字元流

BufferedReader屬于字元流,處理流,然後呢,它又是處理流

全稱:字元輸入處理流

public String readLine()

  throws IOException

  FileReader fileReader = null;

  BufferReader bufferReader = null;

    fileReader = new FileReader("e:/src/users.txt");

    bufferedReader = new BufferedReader(fileReader);

    String line = null;

    while(true){

     line = bufferedReader.readLine();

     if(line == null){

      break;

     System.out.println(line); 

    /*String line = bufferedReader.readLine();

    System.out.println(line);*/

    try{

      bufferedReader.close();

      fileReader.close();

     System.out.println(e);

worker.java

interface worker{

 public void doSomeWork();

class Plumber implements Worker{

 public void doSomeWork(){

  System.out.println("修水管");

class Carpenter implemments Worker{

  System.out.println("修門窗");

class AWorker implements Worker{

 private Worker worker;

 public AWorker(Worker worker){

  this.worker = worker;

    System.out.println("你好");

    worker.doSomeWork();

class Test01{

    //生成一個A公司水管工對象

     Plumber plumber = new Plumber();

     AWorker aWorker = new AWorker(plumber);

     aWorker.doSomeWork();

     Carpenter carpenter = new Carpenter();

     AWorker aWorker2 = new AWorker(capenter);

     aWorker2.doSomeWork();

什麼是内部類?

内部類的使用方法

匿名内部類的使用方法

class A{

  class B{

如何生成内部類的對象?

 public static void mian(String args[]){

  A a = new A();

  A.B b = a.new B();

 int i;

 class B{

  int j;

  int funB(){

    int result = i + j;

    return result;

  a.i = 3;

  b.j = 1;

  int result = b.funB();

  System.out.println(result); 

   int result = A.this.i + this.j;

   return result;

interface A{

 public void doSomething();

class B{

 public void fun(A a){

  System.out.println("B類的fun函數");

  a.doSomething();

class AImpl implements A{

 public void doSomething(){

  System.out.println("doSomething");

  AImpl al = new AImpl();

  A a = al;

  B b = new B();

  b.fun(a);

匿名内部類

  //AImpl al = new AImpl();

  ///A a = al; 

  b.fun(new A(){

   public void doSomething(){

    System.out.println("匿名内部類");

  });

1.程序和線程

2.多線程程式運作模式

3.定義線程的方法

多程序:

在作業系統中能(同時)運作多個任務(程式)

多線程:

在同一應用程式中多個順序流(同時)執行

方法1:

定義一個線程類,它繼承類Thread并重寫其中的方法run();方法run()稱為線程體;由于Java隻支援單繼承,用這種方法定義的類不能再繼承其他類。

FirstThread.java

class FirstThread extends Thread{

 public void run(){

    for(int i=0;i<100;i++){

     System.out.println("FirstThread"+i);

  //生成線程類的對象

  FirstThread ft = new FirstThread();

  //啟動線程

  ft.start();

  //ft.run();  千萬不能這樣寫

  for(int i = 0;i<100;i++){

   System.out.println("main"+i);

37

控制線程的常用函數

方法二:提供一個實作接口Runnable的類作為線程的目标對象,在初始化一個Thread類或者Thread子類的線程對象時,把目标對象傳遞給這個線程執行個體,由該目标對象提供線程體。

class RunnableImpl implements Runable{

   System.out.println("Runnable-->"+i);

  //生成一個Runnable接口實作類的對象

  //ri 代表的是線程體

  RunnableImpl ri = new RunnableImpl();

  //生成一個Thread對象,并将Runnable接口實作類的對象作為參數

  //傳遞給該Thread對象

  //代表的是線程

  Thread t = new Thread(ri);

  System.out.println(t.getPriority());

  //通知Thread對象,執行start方法

  t.start();

}   

中斷線程

Thread.sleep();

Thread.yield();//讓出自己正在使用的CPU

設定線程的優先級

getPriority();

setPriority();

class RunnableImpl implements Runnable{

  for(int i=0;i<100;i++){

   if(i==50){

     Thread.sleep(2000);

     catch(Exception e){

       System.out.println(e);

  //線程的優先級最大是10,最小是1,可以使用Thread所提供的靜态常理來設定線程的優先級

  t.setPriority(Thread.MAX_PRIORITY);

  //t.setPriority(Thread.MIN_PRIORITY);

38

同步線程的方法

  MyThread myThread = new MyThread();

  //生成兩個Thread對象,但是這兩個Thread對象共用同一個線程體

  Thread t1 = new Thread(myThread);

  Thread t2 = new Thread(myThread);

  //每一個線程都有名字,可以通過Thread對象的setName()方法設定線程名字,也可以使用getName方法擷取線程的名字

  t1.setName("線程a");

  t2.setName("線程b");

  //分别啟動兩個線程

  t1.start();

  t2.start();

class MyThread implements Runnable{

 int i = 100;

  while(true){

  System.out.println(Thread.currentThread().getName()+i);

  i--;

  Thread.yield();

  if(i<0){

   break;

//同步代碼塊

   synchronized(this){

    //Thread.currentThread();

    System.out.println(Thread.currentThread().getName()+i);

    i--;

    Thread.yield();

    if(i<0){

     break;

class Service{

 public void fun1(){

  synchronized(this){

    Thread.sleep(3*1000);

   System.out.println("fun1");

  public void fun2(){

    synchronized(this){

      System.out.println("fun2");

class MyThread1 implements Runnable{

 private Service service;

 public MyThread1(Service service){

  this.service = service;

  service.fun1();

class MyThread2 implements Runnable{

 public MyThread2(Service service){

  service.fun2();

  Service service = new Service();

  Thread t1 = new Thread(new MyThread1(service));

  Thread t2 = new Thread(new MyThread2(service));

同步鎖 鎖住的是service

同步方法,同步代碼塊鎖住this

 public synchronized void fun1(){

   Thread.sleep(3*1000);

  System.out.println("fun1");

    System.out.println("fun2");

數組的類型

數組的定義方法

數組的操作方法

  //數組的靜态聲明

  int arr [] = {5,2,7,8,9,0};

  arr[3] = 10;

  //System.out.println(arr[3]);

  for(int i = 0;i<5;i++){

    System.out.println(arr[i]);

  int arr[] = {2,4,6,7,8};

  System.out.println(arr.length);

數組的動态聲明

 //動态聲明

 int arr [] = new int [10];

 System.out.println("arr數組長度"+arr.length);

   for(int i = 0;i<arr.length;i++){

  //二維數組的定義方法,長度為3

   int arr [][] = {{1,2,3},{4,5,6},{7,8,9}};

   System.out.println(arr[1][1]);

   for(int i = 0; i < 3; i++){

    for(int j = 0; j < 3; j++){

     System.out.println(arr[i][j]);

優化

   int arr [][] = {{1,2,3},{4,5,6},{7,8}};

   for(int i = 0; i < arr.length; i++){

    for(int j = 0; j < arr[i].length; j++){

動态

   //int arr [][] = {{1,2,3},{4,5,6},{7,8}};

   int arr [][] = new int[3][5]; 

41類集架構

什麼是類集架構

集合的種類

類集架構的基礎結構

1.類集架構是一組類和接口;

2.位于java.util包當中;

3.主要使用者存儲和管理對象;

4.主要分為三大類---集合,清單和映射

集合中的對象不按特定的方式排序,并且沒有重複對象;

對象是沒有順序的,集合是沒有順序的

集合中對象按照索引位置排序,可以有重複的對象。

可以按照順序取,也可以指定取。

集合中的每一個元素包含一個鍵對象和一個值對象,鍵不可以重複,值可以重複。

資料結構 鍵值對

interface

Iterator Collection

ListIterator List Set Map

LinkeList ArrayList HashSet SortedSet HashMap SortedMap

LinkedHashSet TreeSet LinkedHashMap TreeMap

Comparable Comparator Collections Arrays

//arrayList預設10,可無限長,關于泛型

Test.java

import java.util.List;

import java.util.ArrayList;

public class Test{

  //ArrayList arrayList = new ArrayList();

  ArrayList<String> arrayList = new ArrayList<String>();

  arrayList.add("a");

  arrayList.add("b");

  arrayList.add("c");

  //String s = arrayList.get(1);

  //System.out.println(s);

   for(int i=0;i<3;i++){

    String s = arrayList.get(i);

    System.out.println(s);

42

集合 無序 不可重複

清單 有序 可重複

映射

boolean add(Object o) 向集合當中加入一個對象

void clear() 删除集合當中的所有對象

boolean isEmpty() 判斷集合是否為空

remove(Object o) 從集合中删除一個對象的引用

int size() 傳回集合中元素的數目

Set繼承了Collection

不可以重複

取資料,疊代  iterate器 (Iterator)

it.hasNext();

還有沒有下一個元素,如果這個遊标後面有元素就傳回true,否則,false;

it.next();

傳回遊标所指位置的下一個元素,取出,用hasNext()看有沒有,next取

ok

43Map

JDK幫助文檔

映射中的每一個元素包含一個鍵對象和一個值對象,鍵不可以重複,值可以重複。

Map存的是一對