---
title: "abstract、 interface 关键字"
author: "hellojim"
date: "2009-01-07T16:41:14Z"
tags: []
slug: "abstract-interface"
draft: false
lastmod: "2009-01-07T16:47:20Z"
id: 311619
url: "https://hellojim.iteye.com/blog/311619"
---

```
//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() {
		
	}
		
}
```