关于object类

 

class Point
    {
        public int x, y;

        public Point(int x, int y)
        {
            this.x = 
            this.y = y;
        }

        public override bool Equals(object obj)
        {
            // If this and obj do not refer to the same type, then they are not equal.
            if (obj.GetType() != this.GetType()) return false;

            // Return true if  x and y fields match.
            Point other = (Point)obj;
            return (this.x == other.x) && (this.y == other.y);
        }

        // Return the XOR of the x and y fields.
        public override int GetHashCode()
        {
            return x ^ y;
        }

        // Return the point's value as a string.
        public override String ToString()
        {
            return String.Format("({0}, {1})", x, y);
        }

        // Return a copy of this point object by making a simple field copy.
        public Point Copy()
        {
            return (Point)this.MemberwiseClone();
        }
    }

    public sealed class App
    {
        static void Main()
        {
            // Construct a Point object.
            Point p1 = new Point(1, 2);

            // Make another Point object that is a copy of the first.
            Point p2 = p1.Copy();

            // Make another variable that references the first Point object.
            Point p3 = p1;

            // The line below displays false because p1 and p2 refer to two different objects.
            Console.WriteLine(Object.ReferenceEquals(p1, p2));

            // The line below displays true because p1 and p2 refer to two different objects that have the same value.
            Console.WriteLine(Object.Equals(p1, p2));

            // The line below displays true because p1 and p3 refer to one object.
            Console.WriteLine(Object.ReferenceEquals(p1, p3));
         

            // The line below displays: p1's value is: (1, 2)
            Console.WriteLine("p1's value is: {0}", p1.ToString());
            Console.ReadLine();
        }
    }
 // This code produces the following output.
    //
    // False
    // True
    // True
    // p1's value is: (1, 2)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值