任何人都可以解释以下程序,即用户定义转换是如何显式和隐式发生的?
另请参阅我对显式转换方法和隐式转换方法的评论。
/*** conversion.cs ***/
using System;
using System;
struct RomanNumeral {
public RomanNumeral(int value) {
this.value=value; // what happen here??
}
static public implicit operator RomanNumeral(int value) {
// here the default constructor is called and the parameter in the
// argument is passed for the conversion to RomanNumeral but the
// constructor is of the type int so how it happen please explain??
return new RomanNumeral(value);
}
static public explicit operator int(RomanNumeral roman) {
return roman.value;//how it is happen here??
}
static public implicit operator string(RomanNumeral roman) {
return ("Conversion not yet implemented");
}
private int value;
}
class Test {
static public void Main() {
RomanNumeral numeral;
numeral=10;
Console.WriteLine((int)numeral);
Console.WriteLine(numeral);
short s=(short)numeral;
Console.WriteLine(s);
}
}