Caminhoneiro

GENERICS

Apr 10th, 2018
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.99 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Generics : MonoBehaviour {
  6.  
  7.     //BASIC EXAMPLE
  8.     public void log<T>(T thing){
  9.         string s = thing.ToString();
  10.         Debug.Log(s);
  11.     }
  12.  
  13.     //GENERIC FUNCTION
  14.     public void swap<T>(ref T a,ref T b){
  15.         T temp = b;
  16.         b = a;
  17.         a = temp;
  18.     }
  19.  
  20.     void Start(){
  21.         //Discomment what you want to seee working
  22.  
  23.         //BASIC EXAMPLE
  24.         // log(9);
  25.         // GameObject g = new GameObject("My name is mud");
  26.         // log(g);
  27.         // log(30.55f);
  28.  
  29.         //GENERIC FUNCTION
  30.         // int first = 1;
  31.         // int second = 2;
  32.         // Robot first = new Robot("001");
  33.         // Robot second = new Robot("002");
  34.         // swap(ref first,ref second); // Discoment and comment again this method will work on any variables you want
  35.         // Debug.Log(first);
  36.         // Debug.Log(second);
  37.  
  38.         //GENERIC TYPES
  39.         Robot first = new Robot("Thing001");
  40.         Robot second = new Robot("Thing002");
  41.         Robot third = new Robot("Thing003");
  42.  
  43.         ThreeThings<Robot> threeThings = new ThreeThings<Robot>(first, second, third);
  44.         print("Abstract result: " + threeThings);
  45.  
  46.     }
  47.  
  48.     //GENERIC FUNCTION
  49.     public class Robot{
  50.         private string Name;//store the robot name
  51.         //constructor to assign a name on creation.
  52.         public Robot(string name)
  53.         {
  54.             Name = name;
  55.         }
  56.         //override the ToString() command by
  57.         //returning the robots name instead of its type.
  58.         public override string ToString()
  59.         {
  60.             return Name;
  61.         }
  62.     }
  63.  
  64.     //GENERIC TYPES
  65.     public class ThreeThings<T>
  66.     {
  67.         private T first;
  68.         private T second;
  69.         private T third;
  70.         //constructor for three things
  71.         public ThreeThings(T a, T b, T c)
  72.         {
  73.             first = a;
  74.             second = b;
  75.             third = c;
  76.         }
  77.         //override ToString()
  78.         public override string ToString()
  79.         {
  80.             return first + " " + second + " " + third;
  81.         }
  82.     }
  83.  
  84.     //Multiple Generic Values
  85.     public class TwoThings<T, U>
  86.     {
  87.         T firstThing;
  88.         U secondThing;
  89.         public void AssignThings(T first, U second)
  90.         {
  91.             firstThing = first;
  92.             secondThing = second;
  93.         }
  94.     }
  95.  
  96.  
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment