• java接口示例


    接口示例

    package auto.test;
    
    
    public class RerridingTest {
        public static void main(String args[]){
    
        test("Car","100","45","67" );
        test("Plane","100","45","67" );
        test("Bike","100","45","67" );
        
        }
    
        public static void test(String tool, String a,String b, String c) {
            System.out.println("交通工具: " + tool);
            System.out.println(" 参数A: " + a);
            System.out.println(" 参数B: " + b);
            System.out.println(" 参数C: " + c);
            double A = Double.parseDouble(a);
            double B = Double.parseDouble(b);
            double C = Double.parseDouble(c);
            double v, t;
            try {
                Common d = (Common) Class.forName("auto.test." + tool).newInstance();
                v = d.runTimer(A, B, C);
                t = 1000 / v;
                System.out.println("平均速度: " + v + " km/h");
                System.out.println("运行时间:" + t + " 小时");
            } catch (Exception e) {
                System.out.println("class not found");
            }
        }
    }
    
    class Plane implements Common {
        public double runTimer(double a, double b, double c) {
            return (a + b + c);
        }
    }
    
    class Car implements Common {
        public double runTimer(double a, double b, double c) {
            return (a * b / c);
        }
    }
    
    
    class Bike implements Common {
        public double runTimer(double a, double b, double c) {
            return ((a+b+c)*2);
        }
    }
    interface Common {
        double runTimer(double a, double b, double c);
    }

    示例二

    package auto.test;
    //1、先建一个动物接口,接口里面有吃的方法,没有方法体
    interface Animal {
        public void eat();
    
    }
    //2 建一个狗的类,implements 动物类,Animal
    class Dog implements Animal{
        public void eat(){  //重写animal的eat 方法
            System.out.println("eat bone");
        }
    }
    //3 建一个猫的类,implements 动物类,Animal
    class Cat implements Animal{
        public void eat(){//重写animal的eat 方法
            System.out.println("eat fish");
        }
    }
    //统一处理所有animal 类的行为
    class TestInterface{
        public void getEat(Animal a){// 传入动物类
            a.eat();
        }
    }
    //测试类
    class TestAnimal{
        public static void main(String arg[]){
            Dog d = new Dog(); 
            Cat c = new Cat();
            TestInterface te = new TestInterface();
            te.getEat(d);
            te.getEat(c);        
        }    
    }

    来源:http://blog.sina.com.cn/s/blog_48c0812c0101alaz.html

  • 相关阅读:
    spring mybatis 关于 basepackage 和 mapperLocations 的通配符匹配实例
    nginx 隐藏版本信息
    nginx 虚拟主机配置
    分析解决 spring quartz 中出现的执行两次问题
    nginx 安装配置和常用命令
    JMX 远程监控 Linux tomcat 功能实现
    jvm 类加载机制
    jvm 类文件结构学习
    转 MySQL: Starting MySQL….. ERROR! The server quit without updating PID file解决办法
    MySQL提示:The server quit without updating PID file问题的解决办法
  • 原文地址:https://www.cnblogs.com/testway/p/6071325.html
Copyright © 2020-2023  润新知