• 重写Object的ToString和Equals方法


      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 
      6 namespace 集合
      7 {
      8     class Student
      9     {
     10         //学生id
     11         private int sid;
     12 
     13         public int Sid
     14         {
     15             get { return sid; }
     16             set { sid = value; }
     17         }
     18         //学生姓名
     19         private string name;
     20 
     21         public string Name
     22         {
     23             get { return name; }
     24             set { name = value; }
     25         }
     26         //班级
     27         private string class1;
     28 
     29         public string Class1
     30         {
     31             get { return class1; }
     32             set { class1 = value; }
     33         }
     34         //学生成绩(html)
     35         private double html;
     36 
     37         public double Html
     38         {
     39             get { return html; }
     40             set { html = value; }
     41         }
     42         //学生成绩(c)
     43         private double c;
     44 
     45         public double C
     46         {
     47             get { return c; }
     48             set { c = value; }
     49         }
     50         //学生成绩(sql)
     51         private double sql;
     52 
     53         public double Sql
     54         {
     55             get { return sql; }
     56             set { sql = value; }
     57         }
     58 
     59 
     60         //构造函数(用来以名字来查询的)
     61         public Student(string name)
     62         {
     63             this.Name = name;
     64         }
     65         //构造方法(用来以学号来查询的和删除学生)
     66         public Student(int sid)
     67         {
     68             this.Sid = sid;
     69         }
     70 
     71         //构造方法(用来添加学生信息的)
     72         public Student(int sid, string name, string class1, double c, double html, double sql)
     73         {
     74             this.Sid = sid;
     75             this.Name = name;
     76             this.Class1 = class1;
     77             this.C = c;
     78             this.Html = html;
     79             this.Sql = sql;
     80         }
     81         /// <summary>
     82         /// 重写object的Tostring方法
     83         /// </summary>
     84         /// <returns></returns>
     85         public override string ToString()
     86         {
     87             //顺序决定显示的顺序
     88             return this.sid.ToString()+"	" + name +"	"+class1+"	" + c +"	"+html+"	" +sql ;
     89         }
     90         /// <summary>
     91         /// 重写Object的Equals方法
     92         /// </summary>
     93         /// <param name="obj"></param>
     94         /// <returns></returns>
     95         public override bool Equals(object obj)
     96         {
     97             // 判断是否属于Stundet数据类型(除开基本类型外其他的都是引用类型)
     98             if (!(obj is Student))
     99             {
    100                 return false;
    101             }
    102             //姓名是否相等(this.name这里不是指当前一个对象,而是指当前这个集合的对象)
    103             if (this.name.Equals(((Student)obj).name))
    104             {
    105                 return true;
    106             }
    107             //学号(它会遍历集合)
    108             if (this.sid.Equals(((Student)obj).sid))
    109             {
    110                 return true;
    111             }
    112             return false;
    113         }
    114     }
    115 }

          Student stu=new Student();

           Console.WriteLine(stu);//执行的结果为你重写toString方法的结果而不是Student这个对象

  • 相关阅读:
    61. Rotate List
    60. Permutation Sequence
    59. Spiral Matrix II ***
    58. Length of Last Word
    57. Insert Interval
    328. Odd Even Linked List
    237. Delete Node in a Linked List
    关于找List的中间Node
    234. Palindrome Linked List
    203. Remove Linked List Elements *
  • 原文地址:https://www.cnblogs.com/Dream-High/p/3385511.html
Copyright © 2020-2023  润新知