首页 > 代码库 > 范范(5)
范范(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.Amo范范(5)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。