• 自定类型转换


    代码
    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);
            }
        }
    }
  • 相关阅读:
    【Linux】Vim编辑器-批量注释与反注释
    java_部署jar
    linux_UBUNTU 12.04 上使用 SQUID 架设HTTP正向代理服务器
    crawler_java应用集锦9:httpclient4.2.2的几个常用方法,登录之后访问页面问题,下载文件_设置代理
    linux_coom _ Linux文件比较,文本文件的交集、差集与求差
    linux_之sed用法
    算法-跑道与马-百度面试题
    《Python》常用内置模块
    《Python》内置方法进阶和常用模块
    《Python》反射、内置方法(__str__,__repr__)
  • 原文地址:https://www.cnblogs.com/lihong/p/1912672.html
Copyright © 2020-2023  润新知