1. 程式人生 > >abstract、 interface 關鍵字

abstract、 interface 關鍵字

//abstract interface 關鍵字
/* interface: 一個抽象類中所有的方法都是抽象的,就可以用介面的形式表示
	 interface中只能包含常量和方法定義,而不能包含變數和方法實現,interface
	 中所有的常量和方法訪問許可權都是 public 的,所以在 interface 中定義的常量
	 和方法前根本就沒有必要加上 public final(對應常量)或public(對應方法定義) 關鍵字,
	 在介面中 public String name;這樣的程式程式碼在編譯是會出現"需要賦值這樣的語法錯誤",
	 因為 name 是常量,而不是變數
 */
abstract class Person {
	
	abstract public void run();
	abstract public void talk();	
}

interface Person2 {
	
		String name = "wangwu";
		void run();
		void talk();
}



class Student extends Person {
	public void run() {
			
	}	
	public void talk() {
		
	}
}

class Student2 implements Person2 {
	
	/*象 void run() 這樣重寫 Person2 interface 中的方法時是會出錯的,
	  因為run方法在interface 中的訪問許可權為預設的public
	  所以重寫的方法訪問許可權必須至少也為 public
	*/
	public void run() {
	
	}
	public void talk() {
		
	}
		
}