方法重写的练习
using
System;
using System.Collections.Generic;
using System.Text;
namespace Interface
... {
public sealed class Program
...{
static void Main(string[] args)
...{
// Create the object.
Point p = new Point(5, 98);
// Test ToString with no formatting. 转到下面我们重写的方法
Console.WriteLine("This is my point: " + p.ToString());
// Use custom formatting style "x"
Console.WriteLine("The point's x value is {0:x}", p);
// Use custom formatting style "y"
Console.WriteLine("The point's y value is {0:y}", p);
try
...{
// Use an invalid format; FormatException should be thrown here.
Console.WriteLine("Invalid way to format a point: {0:XYZ}", p);
}
catch (FormatException e)
...{
Console.WriteLine("The last line could not be displayed: {0}", e.Message);
}
}
}
//说明 IFormattable 提供将对象的值格式化为字符串表示形式的功能
class Point : IFormattable
...{
public int x, y;
public Point(int x, int y)
...{
this.x = x;
this.y = y;
}
//在使用tostring的时候直接到这里 然后执行里面的内容后会启动下面的方法
public override String ToString() ...{ return ToString(null, null); }
//方法启动 因为传递过来的2个参数都是null 所以只会执行第一个if
public String ToString(String format, IFormatProvider fp)
...{
// If no format is passed, display like this: (x, y).
if (format == null) return String.Format("({0}, {1})", x, y);
// For "x" formatting, return just the x value as a string
if (format == "x") return x.ToString();
// For "y" formatting, return just the y value as a string
if (format == "y") return y.ToString();
// For any unrecognized format, throw an exception.
throw new FormatException(String.Format("Invalid format string: '{0}'.", format));
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Interface
... {
public sealed class Program
...{
static void Main(string[] args)
...{
// Create the object.
Point p = new Point(5, 98);
// Test ToString with no formatting. 转到下面我们重写的方法
Console.WriteLine("This is my point: " + p.ToString());
// Use custom formatting style "x"
Console.WriteLine("The point's x value is {0:x}", p);
// Use custom formatting style "y"
Console.WriteLine("The point's y value is {0:y}", p);
try
...{
// Use an invalid format; FormatException should be thrown here.
Console.WriteLine("Invalid way to format a point: {0:XYZ}", p);
}
catch (FormatException e)
...{
Console.WriteLine("The last line could not be displayed: {0}", e.Message);
}
}
}
//说明 IFormattable 提供将对象的值格式化为字符串表示形式的功能
class Point : IFormattable
...{
public int x, y;
public Point(int x, int y)
...{
this.x = x;
this.y = y;
}
//在使用tostring的时候直接到这里 然后执行里面的内容后会启动下面的方法
public override String ToString() ...{ return ToString(null, null); }
//方法启动 因为传递过来的2个参数都是null 所以只会执行第一个if
public String ToString(String format, IFormatProvider fp)
...{
// If no format is passed, display like this: (x, y).
if (format == null) return String.Format("({0}, {1})", x, y);
// For "x" formatting, return just the x value as a string
if (format == "x") return x.ToString();
// For "y" formatting, return just the y value as a string
if (format == "y") return y.ToString();
// For any unrecognized format, throw an exception.
throw new FormatException(String.Format("Invalid format string: '{0}'.", format));
}
}
}
using
System;
using System.Collections.Generic;
using System.Text;
namespace @object
... {
public sealed class Program
...{
static void Main(string[] args)
...{
// 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());
}
}
// The Point class is derived from System.Object.
class Point
...{
public int x, y;
public Point(int x, int y)
...{
this.x = 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();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace @object
... {
public sealed class Program
...{
static void Main(string[] args)
...{
// 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());
}
}
// The Point class is derived from System.Object.
class Point
...{
public int x, y;
public Point(int x, int y)
...{
this.x = 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();
}
}
}