zoukankan      html  css  js  c++  java
  • 自定类型转换

    代码
    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);
            }
        }
    }
  • 相关阅读:
    ASP.NET Web API 2 之文件下载
    Windows 查看某个端口号是否被占用
    C# 数据类型之 String(字符串)
    数据表对应关系(一对一、一对多、多对多)
    嫁给程序员的好处,你get到了吗?
    ASP.NET Web API 2 之 HttpRequestMessage 对象
    C# 使用 log4net 记录日志
    Chrome 浏览器快捷键
    C# 获取程序运行时路径
    SpringBoot整合Pagehelper分页插件
  • 原文地址:https://www.cnblogs.com/lihong/p/1912672.html
Copyright © 2011-2022 走看看