zoukankan      html  css  js  c++  java
  • .Net 学习之泛型(一)

    一、前言

      泛型的使用,是为了让我少写重复的代码,提高工作的效率添加的一个延迟声明的类型,重点我个人放在了泛型的约束这里。为什么要对泛型进行约束?主要有三点

      1、加了约束,可以获取更多的功能

      2、保证了程序的准确性和稳定性

      3、可以验证调用的准确性

    二、实践

      

    using System;
    
    namespace Demo1
    {
       public class Program
        {
    
            static void Main(string[] args)
            {
                China china = new China();
                DoSomething(china);
    
            }
    
            public static void DoSomething<T>(T tParameter)
           // where T : class //这是类型引用
            //where T : struct//这是值引用
            //where T : new()//这是无参构造函数引用
            where T : class,new() //这是嵌套使用
            {
                
            }
    
        }
    
        /// <summary>
        /// 做的事情接口
        /// </summary>
        public interface IPeople
        {
            void ReadBook();
            void Write();
            void Play();
        }
    
        /// <summary>
        /// 一个人
        /// </summary>
        public class People
        {
            public int IdCard { get; set; }
    
            public string Name { get; set; }
    
            public void DoHomeWORK() { 
            
            }
    
        }
    
        /// <summary>
        /// 中国人
        /// </summary>
        public class China : People, IPeople
        {
            public void Play()
            {
                throw new NotImplementedException();
            }
    
            public void ReadBook()
            {
                throw new NotImplementedException();
            }
    
            public void Write()
            {
                throw new NotImplementedException();
            }
        }
    
        /// <summary>
        /// 美国人
        /// </summary>
        public class American : People, IPeople
        {
            public void Play()
            {
                throw new NotImplementedException();
            }
    
            public void ReadBook()
            {
                throw new NotImplementedException();
            }
    
            public void Write()
            {
                throw new NotImplementedException();
            }
        }
    
        /// <summary>
        /// 韩国人
        /// </summary>
        public class Korea
        {
            public void Play()
            {
                throw new NotImplementedException();
            }
    
            public void ReadBook()
            {
                throw new NotImplementedException();
            }
    
            public void Write()
            {
                throw new NotImplementedException();
            }
        }
    
    }
  • 相关阅读:
    (C++)string类杂记
    理解inode
    WPF 未能加载文件或程序集“CefSharp.Core.dll”或它的某一个依赖项
    网络抓包 Fiddler
    网络抓包 wireshark
    Vistual Studio Community 2017 账号的许可证过期,公安网激活方法
    C#实现图片叠加,图片上嵌入文字,文字生成图片的方法
    编写html与js交互网页心得:编写两个按钮切换显示不同的图片
    WPF当中StaticResource调用方法
    WPF通过DynamicResource的用法
  • 原文地址:https://www.cnblogs.com/gzbit-zxx/p/12422376.html
Copyright © 2011-2022 走看看