范范(5)

explicit(显) 

implicit(隐)

class Celsius
    {
        public float degree;
        public Celsius(float _d)
        {
            degree = _d;
        }
        public static explicit operator Fahrenheit(Celsius c)
        {
           return  new Fahrenheit(c.degree * 9);
        }
    }
    class Fahrenheit
    {
        public float degree;
        public Fahrenheit(float _d)
        {
            degree = _d;
        }
        public  static explicit operator Celsius(Fahrenheit f)
        {
            return new Celsius(f.degree / 4);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Celsius c = new Celsius(16);
            Fahrenheit f = (Fahrenheit)c;
              c = (Celsius)f;
           // Fahrenheit f = c;
          //  c = f;
            Console.WriteLine(c.degree);
            Console.ReadLine();
        }
    }


static:

 不用实例化对象就可以使用。


abstract :

1.不可以实例化。

2.默认是virtual 所以不可以申明是virtual或者static。

3.不可以是sealed,因为 sealed不可以被继承。



interface 设计

1.默认public,所以函数不用再申明public。

2.类单继承但是可以很多interface 。

interface Paint
    {
        void print(int type);    //no public
    }
    class plainPaint : Paint
    {
        public void print(int type)
        {
            Console.WriteLine("printing from a plain text editor");
        }
    }

    class GuiPaint: Paint
    {
        public void print(int type)
        {
            Console.WriteLine("printing from a gui editor");  //different realization
        }

    }
    class Program
    {
        static void print (plainPaint e,int type)         //a good way to call print()
        {
            e.print(type);
        }
        static void Main(string[] args)
        {

            plainPaint i = new plainPaint();
            print(i, 2);
            i.print(2);
            Console.ReadLine();
        }
    }

继承多个interface 的方法一致,但不同实现,就像你的角色有女儿舍友等等,面对不同人的不同反应。

interface EnglishShape
    {
        float getWidth();
        float getHeight();
    }

    interface MetricShape
    {
        float getWidth();
        float getHeight();
    }

    class Box : EnglishShape, MetricShape
    {
       private float width, height;
        public Box(float w,float h)
        {
            width = w;
            height = h;
        }

        float MetricShape.getHeight()
        {
            return height * 2.54f;
        }

        float EnglishShape.getHeight()
        {
            return height;
        }

        float MetricShape.getWidth()
        {
            return width * 2.54f;
        }

        float EnglishShape.getWidth()
        {
            return width;
        }
    }
    class Program
    {
       
        static void Main(string[] args)
        {
            Box box = new Box(1, 2);
            EnglishShape enbox = (EnglishShape)box;
            Console.WriteLine(enbox.getHeight());
            MetricShape metribox = (MetricShape)box;
            Console.WriteLine(metribox.getHeight()); 
            Console.ReadLine();
        }
    }


get set方法可以保护变量,控制他的访问和读写权,以及写入的可行性。

但是get set只能比(比如这里 AmoutOfWheels)更加保守。

class Vehicle
    {
       private int amoutOfWheels;

        public int AmoutOfWheels
        {
            get               //can protect the amountOfWheels from being read by deletinf it;
            {
                return amoutOfWheels;
            }
            set            //can protect the am.. from being written;
            {
                if(value > 0)    //can prevent the stupid program to assign a negative value to ammout...;
                amoutOfWheels = value;
            }
        }
    }
    class Program
    {
       
        static void Main(string[] args)
        {
            Vehicle v = new Vehicle();
            v.AmoutOfWheels = 4;
            v.AmoutOfWheels = -9;   //useless;
            Console.WriteLine(v.AmoutOfWheels);
            Console.ReadLine();
        }
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值