• java基础练习 接口


    利用接口做参数,写个计算器,能完成+-*/运算

    1)定义一个接口Compute含有一个方法int computer(int n,int m);

    2)设计四个类分别实现此接口,完成+-*/运算

    3)设计一个类UseCompute,含有方法:

    public void useCom(Compute com, int one, int two)

    此方法要求能够:1.用传递过来的对象调用computer方法完成运算

                    2.输出运算的结果

    4)设计一个测试类,调用UseCompute中的方法useCom来完成+-*/运算

    package b;
    
    public interface Compute {
        int computer(int n,int m);
    
    }
    package b;
    
    
    
    class Jia implements Compute{
        @Override
        public int computer(int m, int n) {
            //加法
            return m+n;
        }
    }
    class Jian implements Compute{
        @Override
        public int computer(int m, int n) {
            //减法
            return m-n;
        }
    }
    class Cheng implements Compute{
        @Override
        public int computer(int m, int n) {
            //乘法
            return m*n;
        }
    }
    class Chu implements Compute{
        @Override
        public int computer(int m, int n) {
            //除法
            return m/n;
        }
    }
    package b;
    
    public class UseCompute {
        public void useCom(Compute com, int one, int two)
        {
            System.out.println("运算结果是:"+com.computer(one,two));
        }
    
    }
    package b;
    
    public class Test {
    
        public static void main(String[] args) {
            UseCompute use=new UseCompute();
            use.useCom(new Jia(),5, 6);//
            use.useCom(new Jian(), 7, 5);//
            use.useCom(new Cheng(),2, 3);//
            use.useCom(new Chu(),9, 3);//
    
        }
    }

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

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

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

    3)定义一个测试类,测试飞机和鸟,在main方法中创建飞机对象和鸟对象,

    再定义一个makeFly()方法,其中让会飞的事物飞。并在main方法中调用该方法,

    让飞机和鸟起飞。

    package b;
    
    public interface CanFly {
        public void fly();
    
    }
    package b;
    
     class Plane implements CanFly{
    
        @Override
        public void fly() {
            System.out.println("Plane fiy");
            
        }
    
    }
     class Bird implements CanFly{
    
        @Override
        public void fly() {
            System.out.println("Bird fly");
            
        }
        
    }
    package b;
    
    public class Test2 {
        public static void main(String[] args) {
            Plane p=new Plane();
            p.fly();
            Bird b=new Bird();
            b.fly();
            new Test2().fly(p,b);
        }
            public void fly(Plane p,Bird b)
            {
                System.out.println("plane can fiy");
                System.out.println("bird can fiy");
            }
            
        
    }
  • 相关阅读:
    小程序40001错误
    【2020-06-07】把心思放在赚钱上就对了
    【2020-06-05】下点功夫,看清题目,并做好选择
    【2020-06-04】人生十三信条
    【2020-06-03】顺境时也需要放空
    【2020-06-02】我感染别人的同时,也加强了自己的信念
    【2020-06-01】信息的吸收源自压力
    【一句日历】2020年6月
    【2020-05-30】向风、向雨、向阳光去讨教
    【2020-05-29】爱眼前的一切
  • 原文地址:https://www.cnblogs.com/wallan/p/5532659.html
Copyright © 2020-2023  润新知