zoukankan      html  css  js  c++  java
  • C#视频

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Collections;
    
    namespace ConsoleApplication1
    {
        class Student: IComparable
        {
            private string _name;
            private int _age;
    
            public Student(string name, int age)
            {
                this._name = name;
                this._age = age;
            }
    
            public string Name
            {
                get
                {
                    return _name;
                }
                set
                {
                    _name = value;
                }
            }
    
            public int Age
            {
                get
                {
                    return _age;
                }
                set
                {
                    _age = value;
                }
            }
    
            int IComparable.CompareTo(object obj)
            {
                if(!(obj is Student))
                {
                    throw new Exception("obj must be Student.");
                }
    
                return this._name.CompareTo(((Student)obj)._name);
            }
    
            // improved
            public int CompareTo(Student obj)
            {
    
                return this._name.CompareTo(obj._name);
            }
    
            public override string ToString()
            {
                return _name + " age: " + _age.ToString();
            }
        }
        
        class Program
        {
           
            static void Main(string[] args)
            {
               Student[] ss = new Student[5];
               ss[0] = new Student("Zhu", 15);
               ss[1] = new Student("Jim", 12);
               ss[2] = new Student("Kate", 19);
               ss[3] = new Student("Kay", 22);
               ss[4] = new Student("Xi", 21);
    
                Array.Sort(ss);
                foreach(Student s in ss)
                {
                    Console.WriteLine(s.ToString());
                }
                
                Console.ReadKey();
            }
        }
    }
    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Collections;
    
    namespace ConsoleApplication1
    {
        class Student: IComparable
        {
            private string _name;
            private int _age;
    
            public Student(string name, int age)
            {
                this._name = name;
                this._age = age;
            }
    
            public string Name
            {
                get
                {
                    return _name;
                }
                set
                {
                    _name = value;
                }
            }
    
            public int Age
            {
                get
                {
                    return _age;
                }
                set
                {
                    _age = value;
                }
            }
    
            int IComparable.CompareTo(object obj)
            {
                if(!(obj is Student))
                {
                    throw new Exception("obj must be Student.");
                }
    
                return this._name.CompareTo(((Student)obj)._name);
            }
    
            // improved
            public int CompareTo(Student obj)
            {
    
                return this._name.CompareTo(obj._name);
            }
    
            public override string ToString()
            {
                return _name + " age: " + _age.ToString();
            }
    
    
            // IComparar
            private static AgeComparer ageCom = null;
            public static IComparer AgeCom
            {
                get
                {
                    if(ageCom == null)
                    {
                        ageCom = new AgeComparer();
                    }
    
                    return ageCom; 
                }
            }
    
            // internal class
            private class AgeComparer: IComparer
            {
                int IComparer.Compare(object x, object y)
                {
                    if (!(x is Student) || !(y is Student))
                    {
                        throw new Exception("objs must be Student.");
                    }
    
                    return ((Student)x)._age.CompareTo(((Student)y)._age);
                }
            }
        }
        
        class Program
        {
           
            static void Main(string[] args)
            {
               Student[] ss = new Student[5];
               ss[0] = new Student("Zhu", 15);
               ss[1] = new Student("Jim", 12);
               ss[2] = new Student("Kate", 19);
               ss[3] = new Student("Kay", 22);
               ss[4] = new Student("Xi", 21);
    
                // sort according to name
                Array.Sort(ss);
                foreach(Student s in ss)
                {
                    Console.WriteLine(s.ToString());
                }
    
                Console.WriteLine();
    
                // sort according to age
                Array.Sort(ss, Student.AgeCom);
                foreach (Student s in ss)
                {
                    Console.WriteLine(s.ToString());
                }
                
                Console.ReadKey();
            }
        }
    }
    View Code

    Note: 内部private Class。

  • 相关阅读:
    基于 IAR 修改工程名称
    Baidu IoT Study
    msp430f5438a Information Flash Seg Write -- Chapter
    MFC 编辑框内容更新方法以及滚动条设置
    通过打开按钮打开文件和通过左键移动打开文件并计算crc
    移动文件并将文件路径显示到编辑框内
    Aritronix Virtual Device 运行
    将一个char类型的数转换成曼切斯特数
    数组中重复的数字
    平衡二叉树
  • 原文地址:https://www.cnblogs.com/zzunstu/p/9121459.html
Copyright © 2011-2022 走看看