zoukankan      html  css  js  c++  java
  • 接口—飞

    按要求编写一个Java应用程序程序:

    (1)定义一个接口CanFly,描述会飞的方法public void fly();

    (2)分别定义类飞机和鸟,实现CanFly接口。

    (3)定义一个测试类,测试飞机和鸟,在main方法中创建飞机对象和鸟对象,再定义一个makeFly()方法,其中让会飞的事物飞。并在main方法中调用该方法,让飞机和鸟起飞。

    1 public interface CanFly {
    2 
    3     public void fly();
    4 }
    1 public class airplane implements CanFly{
    2 
    3     @Override
    4     public void fly(){
    5         System.out.println("我是飞机");
    6     }
    7 }
    1 public class Bird implements CanFly {
    2 
    3     @Override
    4     public void fly() {
    5         System.out.println("我是鸟  ");
    6 
    7     }
    8 
    9 }
     1 public class Text {
     2     void makeFly(CanFly f) {
     3         f.fly();
     4         System.out.println("可以起飞");
     5     }
     6 
     7     public static void main(String[] args) {
     8         Text aa = new Text();
     9         airplane a = new airplane();
    10         aa.makeFly(a);
    11 
    12         Bird b = new Bird();
    13         aa.makeFly(b);
    14 
    15     }
    16 
    17 }

    结果:

  • 相关阅读:
    [zoj3627]模拟吧
    [zoj3623]背包模型
    [hdu4358]树状数组
    [hdu1272]并查集
    [hdu3308]线段树
    [hdu5033]单调队列
    [hdu1506]单调队列(栈)
    [hdu2888]二维RMQ
    [hdu4123]dfs区间化+RMQ
    [hdu1242]优先队列
  • 原文地址:https://www.cnblogs.com/ouyangtangfeng99/p/5525809.html
Copyright © 2011-2022 走看看