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);
            }
        }
    }
  • 相关阅读:
    Jmeter之检查点
    Jmeter之集合点
    Jmeter之参数化
    Jmeter组件认识
    Jmeter目录认识
    sts的web工程创建
    Jmeter的BeanShell脚本开发
    Jmeter插件开发
    Appium配置app老是反复安装问题的处理
    eclipse通过git代码的下载和上传
  • 原文地址:https://www.cnblogs.com/lihong/p/1912672.html
Copyright © 2011-2022 走看看