zoukankan      html  css  js  c++  java
  • 【转载】#438

    You might wonder why you'd define an interface, have a class implement that interface and then access the class through the interface instead of just using the class directly.

    One benefit of interface is that it allows you to treat different types of objects in the same way, provided that they implement a common interface.

    For example, assume that we have Dog, Seal and DrillSergeant classes, all of which implement the IBark interface (which contains a Bark method). We can now store a collection of instances of these classes and ask all objects in our collection to Bark by using the IBark interface.

     1 Dog kirby = new Dog("Kirby", 12);
     2 Seal sparky = new Seal("Sparky");
     3 DrillSergeant sarge = new DrillSergeant("Sgt. Hartman", "Tough as nails");
     4 
     5 List<IBark> critters = new List<IBark>(){kirby, sparky, sarge};
     6 
     7 // Tell everyone to bark
     8 foreach (IBark barkingCritter in critters)
     9 {
    10     barkingCritter.Bark();
    11 }

    原文地址:#438 - Benefits of Using Interfaces

  • 相关阅读:
    一. js高级(1)-面向对象编程
    tips01- 定位
    h5c3 part6 flex
    h5c3 part5 background and transform
    template and pagination
    h5c3 part4
    h5c3 part3
    h5c3 part2
    h5c3 part1
    学习博客
  • 原文地址:https://www.cnblogs.com/yuthreestone/p/3596606.html
Copyright © 2011-2022 走看看