zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    我的2015年ccf的解答
    VS中出现“链接器工具错误,XXX工具模块对于SAFESEH映像是不安全的”的解决方法
    记录每次运行的时刻的小程序
    c语言中strcpy与strlen函数对字符串最后的''的处理
    windows8.1的启动目录的路径
    使用feof()判断文件结束时会多输出内容的原因
    [转]android sqlite db-journal文件产生原因及说明
    安卓备份 To Do(待办事项)的数据库
    下载YouTube视频的方法
    Firefox及我使用的firefox扩展
  • 原文地址:https://www.cnblogs.com/testway/p/6071325.html
Copyright © 2011-2022 走看看