/*
抽象函数不能有实现体,抽象函数必须在抽象类里面,但抽象类里可以有非抽象函数,
抽象函数必须是public权限,抽象类不能实例化,子类必须实现抽象类的所有抽象函
数,否则子类仍然是抽象类,子类不能继承多个父类
*/
abstract class Animal
{
abstract public void sleep();
private void eat()
{
System.out.println("Animal eating");
}
public void walk()
{
System.out.println("Animal walking");
}
}
class Bird extends Animal
{
public void sleep()
{
System.out.println("Sheep sleeping");
}
public void fly()
{
System.out.println("bird flying");
}
}
/*
如果父类的一个函数被子类覆盖,而此函数在父类的构造函数里使用了,在创建子类
对象时父类构造函数调用的是由子类覆盖的函数,在C++中父类构造函数则会调用父
类的函数
*/
class Bird extends Animal
{
Bird()
{
fly();
}
public void sleep()
{
System.out.println("Sheep sleeping");
}
public void fly()
{
System.out.println("bird flying");
}
}
class MockingBird extends Bird
{
MockingBird()
{
fly();
}
public void fly()
{
System.out.println("mocking flying");
}
}
/*
接口的成员都是public权限的,接口里的数据成员一定是public static final型的,
一个接口可以继承多个接口,一个类可以继承一个类、实现多个接口
*/
interface Animal
{
public static final int EndangeredSpeciesNum = 10;
public void sleep();
}
interface Robot
{
public void work();
}
abstract class Life
{
abstract public void breath();
}
class RobotAnimal extends Life implements Animal, Robot
{
public void breath()
{
System.out.println("I don't need to breath");
}
public void sleep()
{
System.out.println("I don't need to sleep");
}
public void work()
{
System.out.println("working");
}
}
interface SuperRobot extends Animal, Robot
{
}
/*
final修饰的变量为常量,final修饰的函数不可以被覆盖,final修饰的类不可以被
继承
*/
final class Animal
{
final int EndangeredNum = 10;
public final void sleep()
{
System.out.println("sleeping");
}
}