接口(interface)中变量默认被public static final 修饰是一个静态常量,可以直接用接口名调用
每个类都直接或间接继承object类,object类只有一个无参的构造方法
接口成员方法只能是抽象方法,默认public abstract
接口作为形参和返回值时,需要的时该接口实现类

1
2
3
public interface Jumpping {
void jump();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public abstract class Animal {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Animal() {

// TODO Auto-generated constructor stub
}
public Animal(String name, int age) {

this.name = name;
this.age = age;
}
void eat() {
System.out.println("吃东西");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class Cat extends Animal implements Jumpping{

@Override
public void jump() {
// TODO Auto-generated method stub
System.out.println("猫跳高");
}
@Override
void eat() {
// TODO Auto-generated method stub

System.out.println("猫吃鱼");
}
public Cat() {
super();
// TODO Auto-generated constructor stub
}
public Cat(String name, int age) {
super(name, age);
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Cat [getName()=" + getName() + ", getAge()=" + getAge() + ", getClass()=" + getClass() + ", hashCode()="
+ hashCode() + ", toString()=" + super.toString() + "]";
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class AnimalDemo {

public static void main(String[] args) {
// TODO Auto-generated method stub
Jumpping j=new Cat();
j.jump();
Animal a=new Cat("小黑",13);
a.eat();
System.out.println(a.toString());
Cat c=new Cat("小花",11);
c.eat();
c.jump();
}

}
1
2
3
4
5
猫跳高
猫吃鱼
Cat [getName()=小黑, getAge()=13, getClass()=class 接口.Cat, hashCode()=366712642, toString()=接口.Cat@15db9742]
猫吃鱼
猫跳高