static void Main(string[] args) { Rectangle r = new Rectangle(15, 4); Console.WriteLine(r); r.Draw(); Console.WriteLine(); Square s = (Square)r; Console.WriteLine(s); s.Draw(); Console.WriteLine(); Rectangle r2 = new Rectangle(4, 2); DrawSquare((Square)r2); //conversion to and from int Square sq2 = (Square)90; int side = (int)sq2; //implicit conversion; Rectangle rImpl = sq2; Console.WriteLine(rImpl); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Conversions *****\n"); // Make a Rectangle. Rectangle r = new Rectangle(15, 4); Console.WriteLine(r.ToString()); r.Draw(); Console.WriteLine(); // Convert r into a Square, based on the height of the Rectangle. Square s = (Square)r; Console.WriteLine(s.ToString()); s.Draw(); Console.ReadLine(); }
static void Main( string[] args ) { Console.WriteLine("***** Fun with Conversions *****\n"); // Make a Rectangle. Rectangle r = new Rectangle(15, 4); Console.WriteLine(r.ToString()); r.Draw(); Console.WriteLine(); // Convert r into a Square, // based on the height of the Rectangle. Square s = (Square)r; Console.WriteLine(s.ToString()); s.Draw(); Console.WriteLine(); // Convert Rectangle to Square to invoke method. Rectangle rect = new Rectangle(10, 5); DrawSquare((Square)rect); Console.WriteLine(); // Converting an int to a Square. Square sq2 = (Square)90; Console.WriteLine("sq2 = {0}", sq2); // Converting a Square to a int. int side = (int)sq2; Console.WriteLine("Side length of sq2 = {0}", side); Console.WriteLine(); Square s3 = new Square(); s3.Length = 83; // Attempt to make an implicit cast? Rectangle rect2 = s3; Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("******Fun with Conversions******"); Rectangle r = new Rectangle(15, 4); Console.WriteLine(r.ToString()); r.Draw(); Console.WriteLine(); Square s = (Square)r; Console.WriteLine(s.ToString()); s.Draw(); Rectangle rt = new Rectangle(5, 8); DrawSquare((Square)rt); //Console.ReadLine(); int length = 2; Square si = (Square)length; DrawSquare(si); Square its = new Square(3); Console.WriteLine((int)its); Console.WriteLine("********隐式类型转换*********"); Square iss = new Square(2); Rectangle rectangle = iss; rectangle.Draw(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Conversions *****\n"); Rectangle r = new Rectangle(15, 4); Console.WriteLine(r.ToString()); r.Draw(); Console.WriteLine(); Square s = (Square) r; Console.WriteLine(s.ToString()); s.Draw(); Console.WriteLine(); Rectangle rect = new Rectangle(10, 5); DrawSquare((Square) rect); Console.WriteLine(); Square sqs = (Square) 90; Console.WriteLine("sq2 = {0}\n", sqs); int side = (int) sqs; Console.WriteLine("Side length of sqs = {0}\n", side); Square s3 = new Square(); s3.Length = 7; Rectangle rect2 = s3; Console.WriteLine("rect2 = {0}", rect2); DrawSquare(s3); Console.WriteLine(); Square s4 = new Square(); s4.Length = 3; Rectangle rect3 = (Rectangle) s4; Console.WriteLine("rect3 = {0}", rect3); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("****** Fun with Conversion ******\n"); Rectangle r = new Rectangle(15, 4); Console.WriteLine(r.ToString()); r.Draw(); Console.WriteLine(); Square s = (Square)r; Console.WriteLine(s.ToString()); s.Draw(); Console.ReadLine(); Square sq2 = (Square)90; Console.WriteLine("sq2 = {0}", sq2); int side = (int)sq2; Console.WriteLine("Side length of sq2 = {0}", side); Square s3 = new Square { Lenght = 7 }; Rectangle rect2 = s3; Console.WriteLine("rect2 = {0}", rect2); Square s4 = new Square { Lenght = 3 }; Rectangle rect3 = (Rectangle)s4; Console.WriteLine("rect3 = {0}", rect3); }
static void Main(string[] args) { Console.WriteLine("**** Fun with conversions ****"); Rectangle r = new Rectangle(14, 5); Console.WriteLine(r.ToString()); r.Draw(); Square s = (Square)r; Console.WriteLine(s.ToString()); s.Draw(); DrawSquare((Square)r); Square s2 = (Square)90; Console.WriteLine("s2 = {0}", s2); s2.Draw(); int side = (int)s2; Console.WriteLine("Length of square side: {0}", side); Square s3 = new Square(83); Rectangle r2 = s3; Console.WriteLine("r2: {0}", r2.ToString()); Square s4 = new Square(7); Rectangle r3 = (Rectangle)s4; Console.WriteLine("r3: {0}", r3); Console.ReadLine(); }