Generics

Geniric helps to reuse the methods which avoid overloading methods that will become hard to maintain

Public Class Equalizer

{

    public bool compare (string s1, string s2)

     {return s1 == s2;}

    public bool compare (int i1, int i2)

    {return i1 == i2;}

}

If we have a need to add a cross cutting aspect to all compare method like logging, how would current design accomodate the need

Public Class EqualizerWithLogging

{

    public bool compare (string s1, string s2)

     {

        iLoogger.log(XXXX);

       return s1 == s2;

     }

    public bool compare (int i1, int i2)

    {

       iLoogger.log(XXXX);

return i1 == i2;}

}

By simply resort this to generic class can resolve this issue

Public Class EqualizerWithLogging <T>
{
    public bool compare (T s1, T s2)
     {        iLoogger.log(XXXX);
       return s1 == s2;
     }
}
时间: 2024-11-05 03:13:21

Generics的相关文章

ylbtech-Unitity-CS:Generics

ylbtech-Unitity-CS:Generics 1.A,效果图返回顶部 Unsorted List: Raul:35 Alessandro:30 Maria:72 Hiroyuki:108 Alok:9 Gunnar:18 Sandra:23 Li:28 Bill:19 Franscoise:45 Sorted List: Alok:9 Gunnar:18 Bill:19 Sandra:23 Li:28 Alessandro:30 Raul:35 Franscoise:45 Maria:

Swift 泛型(generics)

Swift 使用<>来声明泛型函数或泛型类型: 1 func repeat<ItemType>(item: ItemType, times: Int) -> ItemType[] { 2 var result = ItemType[]() 3 for i in 0..times { 4 result += item 5 } 6 return result 7 } 8 repeat ("knock", 4) Swift 也支持在类.枚举和结构中使用泛型: 1

sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class异常解决方法

package com.wzs; import java.lang.reflect.ParameterizedType; public class T1<T> {     private Class classt;     public T1() {         ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();         this.classt = (Class)

C# - Generics

Generics were added to version 2.0 of the C# language and the common language runtime (CLR). Generics introduce to the .NET Framework the concept of type parameters, which make it possible to design classes and methods that defer the specification of

[TypeScript] The Basics of Generics in TypeScript

It can be painful to write the same function repeatedly with different types. Typescript generics allow us to write 1 function and maintain whatever type(s) our function is given. This lesson covers syntax and a basic use case for Typescript generics

Java generics

Provides compile-time type safety for collections and eliminates the pain of casting. Generics creates strongly typed classes to do generic operation. 1 Vector v = new Vector(); 2 v.add("hello"); 3 v.add(new Integer(1)); 4 v.add(new HashTable())

#读书笔记#Illustrated C# 2012第17章 Generics泛型(1)

什么是泛型(广泛意义上的"泛型")-->在特定语言(C#)里的泛型-->C#中的5个泛型类型 1.什么是泛型? 2.C#中提供了五种泛型(C#中的泛型) 3.C#中的泛型类(Generic Classes ) 4.泛型方法Generic Methods 5.泛型结构Generic Structs 6.泛型委托Generic Delegates 7.泛型接口Generic Interfaces ================= 1.什么是泛型 泛型是通过一种抽象的方式达到方

#读书笔记#Illustrated C# 2012第17章 Generics泛型(2)

================= 4.泛型方法Generic Methods Unlike the other generics, a method is not a type but a member. You can declare generic methods in both generic and nongeneric classes, and in structs and interfaces. 4.1.Declaring a Generic Method Generic meth

Xcode 7新特性Lightweight Generics 轻量级泛型与__kindof修饰符

Lightweight Generics 轻量级泛型,轻量是因为这是个纯编译器的语法支持(llvm 7.0),和 Nullability 一样,没有借助任何 objc runtime 的升级,也就是说,这个新语法在 Xcode 7 上可以使用且完全向下兼容(更低的 iOS 版本) 带泛型的容器 1 2 NSArray<NSString *> *strings = @[@"sun", @"yuan"]; NSDictionary<NSString *

Java 语法 索引 ----- 泛型(Generics)

class B<T extends A> {} class A {} -------------------------- class C<T extends I> {} interface I {} ----------------------- class D<T extends A & I> {} class E<T extends A & I, U extends A & I> {} 参考文献: Java Quick Synt