/*
知识点:1,类与类的关系是继承,而且只能是单继承,可以支持多层继承
2,接口与接口的关系是继承关系,可以支持单继承,也可以支持多继承
3,类与接口的关系是实现,可以单实现,也可以多实现.
4,接口中必须是抽象类.
*/
- class Demo {
- public static void main(String[] args) {
- Cat c = new Cat("TOM",8);
- System.out.println(c.getName() + " is " + c.getAge() + "years old");
- c.sleep();
- c.eat();
- System.out.println("---------------------------------------");
- Cat c1 = new FlyCat("Jerry",4);
- System.out.println(c1.getName() + " is " + c1.getAge() + " years old ");
- c1.Fly();
- }
- }
- interface Inter {
- public abstract void Fly(); //接口中必须是抽象方法.
- }
- abstract class Animal { //抽象类
-
- private String name;
- private int age;
- public Animal(){}
- public Animal(String name,int age){
- this.name = name;
- this.age = age;
- }
- public void setName(String name){
- this.name = name;
- }
- public String getName(){
- return name;
- }
- public void setAge(int age){
- this.age = age;
- }
- public int getAge(){
- return age;
- }
- public abstract void eat();
- public abstract void sleep();
- }
- class Cat extends Animal { //类与类之间继承关系
- public Cat() {}
- public Cat(String name,int age){
- super(name,age);
- }
- public void eat(){
- System.out.println("猫吃鱼");
- }
- public void sleep(){
- System.out.println("趴着睡");
- }
- public void Fly(){};
- }
- class FlyCat extends Cat implements Inter { //继承与实现
-
- public FlyCat(){}
- public FlyCat(String name, int age){
- super(name,age);
- }
- public void Fly(){ //注意方法重写
- System.out.println("Jerry can fly");
- }
- }
复制代码
|
|