package test;
/*
* 语法
* interface 接口名{
* 方法定的定义;
* ....
* }
* 1、接口中的方法的定义不需要Abstract 来修饰,默认就是抽象
* 2、接口是不可以实例化的,需要有类来实现接口的的语法
* class 类名 implements 接口名,接口名,...{
* 实现接口中的方法
* }
* 3、接口中低端方法不能和private,static和final共存
* 4、在接口中可以定义"属性",可以通过接口的实现类的实例来访问(不推荐),推荐使用接口名.常量,
* 这是一个常量默认是public,static,final
* 5、接口可以进行多继承,接口可以继承接口
*/
interface A{
public void methoda();
public void method();
}
interface B extends A{
public void methodb();
public void method();
}
interface C extends B{
public void methodc();
}
//接口中可以多继承,两个方法重复时做合并
interface D extends A,B{
}
class CImp2 implements D{
public void methodb() {
}
public void methoda() {
}
public void method() {
}
}
class CImp1 implements C{//多级继承
public void methodb() {
}
public void methodc() {
}
public void methoda() {
}
public void method() {
}
}
interface Person{
//这不是属性,是一个常量
double PI = 3.14;//相当于public static final double PI = 3.14
public void sleep();
public void smoke();
}
class Hero implements Person{
String name;
public void sleep() {
System.out.println(name+"在绳子上睡觉");
}
public void smoke() {
System.out.println(name+"大摇大摆抽雪茄");
}
}
public class Demo1{
public static void main(String[] args) {
//Person p = new Person();接口是无法实例化的
Hero h = new Hero();
h.name = "洪七公";
h.sleep();
h.smoke();
System.out.println(h.PI);
System.out.println(Person.PI);
}
}