• 自定类型转换


    代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication1
    {
        
    class CustomConversion
        {
            
    static void Main()
            {
                Rectangle r 
    = new Rectangle(5050);
                Console.WriteLine(r.ToString());
                r.Draw();

                Square s 
    = new Square(6);
                Console.WriteLine(s.ToString());
                s.Draw();

                
    //将矩形显示强制转换为正方形
                s =(Square)r;
                Console.WriteLine(s.ToString());
                s.Draw();

                Console.ReadLine();
            }
        }

        
    //矩形
        public struct Rectangle
        {
            
    public Rectangle(int Widht, int Heiht)
            {
                
    this.widht = Widht;
                
    this.height = Heiht;
            }

            
    private int widht;

            
    public int Widht
            {
                
    get { return widht; }
                
    set { widht = value; }
            }

            
    private int height;

            
    public int Height
            {
                
    get { return height; }
                
    set { height = value; }
            }


            
    public override string ToString()
            {
                
    return string.Format("[{0},{1}]"this.widht, this.height);
            }

            
    public void Draw()
            {
                
    for (int i = 0; i < this.height; i++)
                {
                    
    for (int j = 0; j < this.widht; j++)
                    {
                        
    if (i == 0 || i == this.height - 1)
                        {
                            Console.Write(
    "-");
                        }
                        
    else
                        {
                            
    if (j == 0 || j == this.widht - 1)
                            {
                                Console.Write(
    "|");
                            }
                            
    else
                            {
                                Console.Write(
    " ");
                            }
                        }
                    }

                    Console.WriteLine();
                }
            }
        }

        
    //正方形
        public struct Square
        {
            
    private float lenght;

            
    public float Lenght
            {
                
    get { return lenght; }
                
    set { lenght = value; }
            }

            
    public Square(float length)
            {
                
    this.lenght = length;
            }

            
    public void Draw()
            {
                
    for (int i = 0; i < this.lenght; i++)
                {
                    
    for (int j = 0; j < this.lenght; j++)
                    {
                        
    if (i == 0 || i == this.lenght - 1)
                        {
                            Console.Write(
    "-");
                        }
                        
    else
                        {
                            
    if (j == 0 || j == this.lenght - 1)
                            {
                                Console.Write(
    "|");
                            }
                            
    else
                            {
                                Console.Write(
    " ");
                            }
                        }
                    }

                    Console.WriteLine();
                }
    //end of for
            }//end of for

            
    //实现矩形可显示强制转换为正方形
            public static explicit operator Square(Rectangle r)
            {
                Square s;
                s.lenght 
    = r.Height;
                
    return s;
            }

            
    public override string ToString()
            {
                
    return string.Format("Length:{0}",this.lenght);
            }
        }
    }
  • 相关阅读:
    有点成熟的短句,最新个性签名
    ACM2039_三角形三边关系
    Android 绘制中国地图
    Opengl-法线贴图(用来细化表面的表现表现的凹凸)
    Go的sync
    Laravel Study(使用 Laravel )
    对于宅男来说,硬盘里的数据就是命
    设计模式之模板方法模式
    游戏掉落道具掉落
    NEWMING
  • 原文地址:https://www.cnblogs.com/lihong/p/1912672.html
Copyright © 2020-2023  润新知