java面向对象接口课后第五题 及基础知识点

接口的作用

代表一种能力,主要是解决java中单继承的局限性

接口的声明

接口就是用来被继承的,被实现的,因此修饰符一般建议使用public或默认即可;
不能使用private和protected修饰接口。interface关键字用来声明一个接口。
//接口的声明
interface Animal {  
  //静态常量成员    
  public static final int AGE = 5;   
    //抽象方法成员    
    public void eat();    
    public void travel(); 
    }

接口的实现

当类实现接口的时候,类要实现接口中所有的方法。否则,类必须声明为抽象的类。类使用implements关键字实现接口。在类声明中,implements关键字放在class声明后面。实现一个接口的语法,可以使用这个公式:
… implements 接口名称[, 其他接口, 其他接口

接口的特性## 标题

A :不可以实例化对象 但是可以进行向上/向下转型
B:接口当中所以有的属性都是静态常量 在定义的时候必须赋值
C:接口中所有的方法都是抽象方法 可以不加abstract
D:接口当中属性和方法的权限只有一种public公开权限
E:类实现接口以后,必须强制重写接口中所有的抽象方法 除非这个类是抽象类
F:一个类只能继承自一个父类 但是可以同时实现多个接口
G:接口之间可以继承 多继承
H:接口中不可以定义任何构造函数

课后习题

 //定义Eatable接口 
public interface Eatable { 
//吃饭 
public void eat();
 } 
//定义Swimmable接口 
public interface Swimmable { 
//游泳 
public void Swim(); }
 //定义 FlyingDiscCatchable接口 
 public interface FlyingDiscCatchable {
  //接飞盘 
  public void catchingFlyDisc(); }
   //定义Pet类 
   public class Pet { public String name;
    public int health;
     public int love;
      public Pet(){}
       public Pet(String name, int health, int love) { 
       super();
        this.name = name; 
        this.health = health; 
        this.love = love; 
        } 
        public String getName() { 
        return name; 
        } 
        public void setName(String name) {
         this.name = name;
          } 
          public int getHealth() {
           return health;
            }
             public void setHealth(int health) {
              this.health = health; 
              } 
              public int getLove() { return love;
               } 
               public void setLove(int love) { 
               this.love = love; 
               } 
               public void print(){  
               System.out.println("宠物名称"+this.getName()+"健康值"+this.getHealth()+"亲密度"+this.getLove());
                } 
                } 
                //定义企鹅类‘ 
                public class Penguin extends Pet implements Eatable,Swimmable{
                 public String name; 
                 public int health;
                  public int love; 
                  public Penguin(){};
                   public String getName() { 
                   return name;
                    } 
                    public void setName(String name) {
                     this.name = name;
                      } 
                      public int getHealth() { 
                      return health; 
                      } 
                      public void setHealth(int health) {
                       this.health = health; 
                       } 
                       public int getLove() { 
                       return love;
                        } 
                        public void setLove(int love) { 
                        this.love = love; 
                        } 
                        public Penguin(String name, int health, int love) {
                         super(); 
                         this.name = name; 
                         this.health = health;
                          this.love = love; 
                          } 
                          @Override public void Swim() {
                             System.out.println("企鹅游泳");
                           } 
                           @Override public void eat() {
                             System.out.println("企鹅在吃饭"); } } 
                             //定义狗类
public class Dog1 extends Pet implements Eatable,FlyingDiscCatchable{                          
                    public int love;
                    public int health;
                    public String getName() {
                    return name;
 }
 public void setName (String name){
 this.name=name;
 }
 public int getLove(){
 return love;
 }
 public void setLove(int love){
 this.love=love;
 }
 public int getHealth(){
 return health;
 }
 public void setHealth(int health){
 this.health=health;
 }
 public Dog1(){};
 public Dog1(String name,int love,int health){
 super();
 this.name=name;
 this.love=love;
 this.health=health;
 }
 @Override
 public void catchingFlyDisc(){
 System.out.println("狗在接飞盘")
 }
 @Override
 public void eat(){
 System.out.println("狗在吃饭");
 }
}

//定义main函数
public static void main(String[] args) {
 Pet go=new Dog1("狗",100,100);
  go.print();
  Pet qq=new Penguin("企鹅",100,100);
  qq.print();
  Dog1 go1=new Dog1();
  go1.eat();
  go1.catchingFlyDisc();
  Penguin qq1=new Penguin();
  qq1.Swim();
  qq1.eat();

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值