Using an Interface as a Type

When you define a new interface, you are defining a new reference data type. You can use interface names anywhere you can use any other data type name. If you define a reference variable whose type is an interface, any object you assign to itmust be an instance of a class that implements the interface.

As an example, here is a method for finding the largest object in a pair of objects, for any objects that are instantiated from a class that implements Relatable:

public Object findLargest(Object object1, Object object2) {
   Relatable obj1 = (Relatable)object1;
   Relatable obj2 = (Relatable)object2;
   if ((obj1).isLargerThan(obj2) > 0)
      return object1;
   else
      return object2;
}

By casting object1 to a Relatable type, it can invoke the isLargerThan method.

If you make a point of implementing Relatable in a wide variety of classes, the objects instantiated from any of those classes can be compared with the findLargest() method—provided that both objects are of the same class. Similarly, they can all be compared with the following methods:

public Object findSmallest(Object object1, Object object2) {
   Relatable obj1 = (Relatable)object1;
   Relatable obj2 = (Relatable)object2;
   if ((obj1).isLargerThan(obj2) < 0)
      return object1;
   else
      return object2;
}

public boolean isEqual(Object object1, Object object2) {
   Relatable obj1 = (Relatable)object1;
   Relatable obj2 = (Relatable)object2;
   if ( (obj1).isLargerThan(obj2) == 0)
      return true;
   else
      return false;
}

These methods work for any "relatable" objects, no matter what their class inheritance is. When they implement Relatable, they can be of both their own class (or superclass) type and a Relatable type. This gives them some of the advantages of multiple inheritance, where they can have behavior from both a superclass and an interface.

Using an Interface as a Type

时间: 2024-10-31 19:24:50

Using an Interface as a Type的相关文章

接口和抽象类:Interface、abstract _【转】

一.接口 接口是C#中很常见的工具,概念什么的就不说了,这里讲几个值得注意的小地方: 1.接口内部只能有函数.属性和事件的声明: interface IParent { void Show(); string Type { get; set; } event AddChildren Add; } 在接口中声明的成员都不需要访问修饰符(public,private等),因为接口成员的权限默认都是public,另外值得注意的是接口中之所以能够声明事件是因为事件就是委托的特殊属性. 接口不能是静态的,

个人对golang的面向对象的理解------接口(interface)

golang是面向对象语言,但是他并没有class之类的关键字,他是通过struct和interface组合实现了面向对象的概念. 转载请注明出处:http://www.cnblogs.com/SSSR/p/6351661.html 在golang中面向对象的实现顺序: 1.先定义struct,是对象的属性值,如: type Person struct { id int name string} 2.定义对象需要实现的函数,如: func (self Person) codingEveryday

浅析Go语言的Interface机制

前几日一朋友在学GO,问了我一些interface机制的问题.试着解释发现自己也不是太清楚,所以今天下午特意查了资料和阅读GO的源码(基于go1.4),整理出了此文.如果有错误的地方还望指正. GO语言的interface是我比较喜欢的特性之一.interface与struct之间可以相互转换,struct不需要像JAVA在源码中显示说明实现了某个接口,可以通过约定的形式,隐式的转换到interface,还可以在运行时查询接口类型,这样有种用动态语言写代码的感觉,但是又可以在编译时进行检查,捕捉

Java Programming Tutorial Java Native Interface (JNI)

1.  Introduction At times, it is necessary to use native codes (C/C++) to overcome the memory management and performance constraints in Java. Java supports native codes via the Java Native Interface (JNI). JNI is difficult, as it involves two languag

Golang中interface{}作为函数参数和函数返回值的使用

package main import (     "errors"     "fmt" ) type item struct {     Name string } func (i item) String() string {     return fmt.Sprintf("item name: %v", i.Name) } type person struct {     Name string     Sex  string } func

Unity Interface Serialization-Expose Interface field In Inspector

Unity has some quirks about their inspector, so as a preface they are listed here: If you add a [Serializable] attribute to a class, Unity's Inspector will attempt to show all public fields inside that class. Any class extending Monobehaviour automat

abstract class and interface

Interface separates what a Class does and how does it work. List interface lets different type of List to implement its own method. interface has more flexibility. A class can implements multiple interfaces but can only extends one class. once interf

Golang的Interface是个什么鬼

问题概述 Golang的interface,和别的语言是不同的.它不需要显式的implements,只要某个struct实现了interface里的所有函数,编译器会自动认为它实现了这个interface.第一次看到这种设计的时候,我的第一反应是:What the fuck?这种奇葩的设计方式,和主流OO语言显式implement或继承的区别在哪儿呢? 直到看了SICP以后,我的观点发生了变化:Golang的这种方式和Java.C++之流并无本质区别,都是实现多态的具体方式.而所谓多态,就是“一

Golang之interface(多态)

多态用法 package main //一种事物的多种形态,都可以按照统一的接口进行操作 //多态 import ( "fmt" "math/rand" "sort" ) type Student struct { Name string Id string Age int sortType int } type Book struct { Name string Author string } //切片默认传地址 type StudentArr