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

  • 相关阅读:
    [转]了解ASP.NET MVC几种ActionResult的本质:EmptyResult & ContentResult
    [转]XPath 语法
    [转]XSL 语言
    [转]项目经理面试指南
    [转]《精通css》笔记1:css选择器与优先级
    [转]jQuery 简介
    [转]Android 70道面试题
    [书目20130316].NET应用架构设计:原则、模式与实践
    [转]XPath语法 在C#中使用XPath示例
    [转]Android面试3
  • 原文地址:https://www.cnblogs.com/testway/p/6071325.html
Copyright © 2011-2022 走看看