ts generics, generic methods, generic class, generic interface

What is generic it? We can be understood as a generic type is (the wide meaning) method of uncertainty, when the method is called, designated by the generic programmer at compile time what type of specific points. Generics in the traditional object-oriented programming language is very common, ts of course also perform generic, if you understand c # or java generics, Benpian believe it will be very easy to understand.

Generic function, generic class, generic interface.

08_generic.ts

/ *
* TS generics
* it is a generic type (a wide range of meaning) method of uncertainty, when the method is called, designated by the generic programmer at compile specific point to what type
* /

 
 
/*
 * Ts generics
 * Generics is the type (of a wide range of meaning) method of uncertainty, when the method is called, is specified by the programmer during compilation generics specific point to what type
 */
 
// 1 generic function
 
/**
 * Get array minimum (number)
 * @param {number[]} arr
 * @returns {number}
 */
function  getMinNumber(arr:number[]):number{
    where min = arr [ 0 ];
    arr.forEach((value)=>{
        if(value<min){
            min=value;
        }
    });
    return min;
}
 
/**
 * Get the minimum array (string)
 * @param {number[]} arr
 * @returns {number}
 */
function  getMinStr(arr:string[]):string{
    var min=arr[0];
    arr.forEach((value)=>{
        if(value<min){
            min=value;
        }
    });
    return min;
}
 
console.log(getMinNumber([1, 3, 5, 7, 8]));//1
console.log(getMinStr(["tom","jerry","jack","sunny"]));//jack
 
/**
 * Get array minimum value (T generic generic)
 * @param {T[]} arr
 * @returns {T}
 */
function getMin<T>(arr:T[]):T{
   var min=arr[0];
   arr.forEach((value)=>{
      if(value<min){
          min=value;
      }
   });
    return min;
}
 
console.log(getMin([1, 3, 5, 7, 8]));
console.log(getMin(["tom","jerry","jack","sunny"]));
 
 
// 2 generic class 
class GetMin <T> {
     arr:T[]=[];
     add (he: T) {
         this.arr.push(ele);
     }
    min () T {
       var min=this.arr[0];
       this.arr.forEach(function (value) {
          if(value<min){
              min=value;
          }
       });
       return min;
    }
}
  var gm1= new  GetMin<number>();
   gm1.add(5);
   gm1.add(3);
   gm1.add(2);
   gm1.add(9);
console.log(gm1.min());
 
var gm2= new  GetMin<string>();
gm2.add("tom");
gm2.add("jerry");
gm2.add("jack");
gm2.add("sunny");
console.log(gm2.min());
 
 
 
/**
 * 3 generic function interface
 */
interface ConfigFn{
    <T>(value:T):T;
}
 
var getData:ConfigFn=function<T>(value:T):T{
    return value;
}
the getData < String > ( ' John Doe ' );
 // the getData <String> (1243 of);   // Error
 
 
// Similar Map <String, Object> Param interface to 
interface the Param {
    [index:string]:any
}
 
 
 
// 4 generic class Interface
 
/**
 * Page pagination objects
 * / 
Class Page {
     Private The currentPage: Number = . 1 ; // current page by default. 1 
    Private the pageSize: Number = 10 ; // page number of the default is 10 
    Private remove the sortName: String ; // sort field 
    Private the sortOrder: String = " ASC " ; // collation asc | desc default is asc positive sequence
 
 
     constructor(param:Param){
         if(param["currentPage"]){
             this.currentPage=param["currentPage"];
         }
         if(param["pageSize"]){
             this.pageSize=param["pageSize"];
         }
         if(param["sortName"]){
             this.sortName=param["sortName"];
         }
         if(param["sortOrder"]){
             this.sortOrder=param["sortOrder"];
         }
     }
 
    public    getStartNum():number{
        return   (this.currentPage-1)*this.pageSize;
    }
}
 
 
class User{
     the above mentioned id: Number The; // the above mentioned id auto-increment primary key 
     name: String ; // Name 
     Sex: Number The; // Sex 1 Male 2 Female 
     Age: Number The; // Age 
     City: String ; // city 
     DESCRIBE: String ; // Description
 
}
 
// generic interface 
interface   BaseDao <T> {
     the findById (id: Number): T; // a primary key id entity queries 
     findPageList (param: the Param, Page: Page): T []; // Query tab listing 
     findPageCount (param: the Param): Number; // Query tab COUNT 
     save (O: T): void ; // save an entity 
     update (O: T): void ; // update an entity 
     deleteById (ID: Number); // To delete a 
}
 
/**
 * Interface implementation class
 */
class UserDao<User> implements BaseDao<User>{
     findById(id:number):User{
 
        return null;
     }
    findPageList(param:Param,page:Page):User[]{
         return [];
    }
    findPageCount(param:Param):number{
         return   0;
    }
    save(o:User):void{
 
    }
    update(o:User):void{
 
    }
    deleteById(id:number){
 
    }
}
 
 

 

 

Guess you like

Origin www.cnblogs.com/plBlog/p/12365627.html