ããã®ç¶æ¿ã¯å¤ãªæãã
æ°æã¡æªãçç±ãâ¦Point3D ãããªã Poit3D ã ãããã¨ããã®ã¯å㧠:D ï¼ä»¥éåæã«ä¿®æ£ãã¦æ¸ãã¾ãï¼
Point3D extends Point2D ã¨ããã¨ãããã¨ãã° Point3D.moveTo() ã¨ããã¡ã½ããã¯
class Point3D extends Point2D { void moveTo (double x, double y, double z) { ... } }
ã¨ã3å¼æ°ãåãã¹ãã§ãåç´ãªãªã¼ãã¼ã©ã¤ãã§ã¯ãã¾ããããªããªãã¾ããä»ã®ãPoint2D ãªãã¸ã§ã¯ããæä½ãã¦ããã¡ã½ããã«ã¯ãä¸æ§ã«ãã®åé¡ãçºçããæ°ãã
ColoredPoint2D extends Point2D ã§ã¯ãããããåé¡ã¯èµ·ãããªãã§ãããã¶ãã
ãããããåãã¯ã©ã¹ã§å®ç¾©ããããã ãã©ã
ã¯ã¦ä½ã ããã
ãã¾ãå®ç¾©ã§ããããã¨ããåãã«çãã¦ã¾ããããNonNegativeInteger ãå®ç¾©ããã¨ãå¼ãç®ãªãã¬ã¼ã¿ã¨ãã§çµæãè² ã«ãªãï¼NonNegativeInteger ã§æ±ããç¯å²ãè¶
ããï¼å ´åã©ããããã§ãããã
ãããããåéã®é¢ä¿ã¯ã
unionãªãvariantåãªãã使ãããªãããã£ã¡ã使ãã®ãçãªãã§ããããã©ã
ãã®å ´åã¯ãnumber 㨠handle ã®æ±ãã¯æ¬¡ã®ããã«ããã¨ãã
- Number ã Handle ãã®ã©ã¡ãããã¨ãåæå ID ãæã¤ã¡ã³ã which ãå ãã
- IDåã®1å¼æ°ãåãã³ã³ã¹ãã©ã¯ã¿ãå®ç¾©ããããã使ã£ã¦ããã®ã¤ã³ã¹ã¿ã³ã¹ã® which ã®å¤ã決å®ããã
- ã¢ã¯ã»ããµã¡ã½ããã« which ã®å¤å®ãä»è¾¼ãäºã§ãã»ããã®å¶éãè¡ãã
UserNumber 㨠UserHandle ã¯ãã³ã³ã¹ãã©ã¯ã¿ã§baseã¯ã©ã¹ã®IDãèªåçã«ã»ããããããã«ä»è¾¼ãããã«ãã¦ãå¾ã¯æ®éã«ç¶æ¿ã
å®éã«ãC#ã§ã³ã¼ãæ¸ãã¦ã¿ã¾ãããéè¦ãªã¨ããã ãæç²ã
/// <summary> /// number ã Handleãã決å®ããåæå /// </summary> enum ID { Number, Handle }; /// <summary> /// UserID ã¯ã©ã¹ /// </summary> class UserID { protected int number; protected String handle; protected ID which; /// <summary> /// number ã¸ã®ã¢ã¯ã»ããµã¡ã½ãã /// </summary> public virtual int Number { get { if (which == ID.Number) return number; else Error(); // Error() ã®ä¸èº«ã¯é©å½ã« } set { if (which == ID.Number) number = value; else Error(); } } /// <summary> /// handle ã¸ã®ã¢ã¯ã»ããµã¡ã½ãã /// </summary> public virtual String Handle { get { if (which == ID.Handle) return handle; else Error(); } set { if (which == ID.Handle) handle = value; else Error(); } } /// <summary> /// ã³ã³ã¹ãã©ã¯ã¿ /// </summary> /// <param name="select">IDã®è¨å®</param> public UserID(ID select) { which = select; } /// <summary> /// IDã表示ããã /// </summary> public void Show() { switch (which) { case ID.Number: Console.WriteLine("Number: " + number.ToString()); break; case ID.Handle: Console.WriteLine("Handle: " + handle); break; }; } } /// <summary> /// UeerNumberã¯ã©ã¹ï¼ï¼ã³ã³ã¹ãã©ã¯ã¿ã®å®ç¾©ã®ã¿ã /// </summary> class UserNumber : UserID { public UserNumber() : base(ID.Number) { } } /// <summary> /// UserHandleã¯ã©ã¹ï¼ï¼ã³ã³ã¹ãã©ã¯ã¿ã®å®ç¾©ã®ã¿ã /// </summary> class UserHandle : UserID { public UserHandle() : base(ID.Handle) { } } /// <summary> /// Mainããã°ã©ã /// </summary> class Program { static void Main(string[] args) { UserHandle handleID = new UserHandle(); handleID.Handle = "Takeo"; UserNumber numberID = new UserNumber(); numberID.Number = 5; handleID.Show(); numberID.Show(); } }
ãã©ã£ã¡ã«å³æ¹ãã¾ããã
ãã¼ãã¼ãã©ã£ã¡ãªãã§ããããããä»ã¾ã§ã®åé¡ã«å
±éãã話ã§ãããããã
ã§ãããåºæ¬ã¯ã©ã¹ã«ããã¡ã½ããã¯ãæ´¾çã¯ã©ã¹ã«ããããã¨èããã®ãååã§ãããã(C#ã® sealed 修飾åã®ãããªç©ãã¤ããªãéããï¼æ©è½ãã¤ã¶ãæ¹é¢ã§èããã¨ãdo-nothingã«ãªã¼ãã¼ã©ã¤ããããã¼ãã¼ãã¤ãã³ããã³ãã©ããnumber / handle ã®ä¾ã®ã¢ã¯ã»ããµã¡ã½ããã®ããã«ãã©ããã¦ãæ©è½ããªãã¡ã½ãããæ®ã£ã¦ãã¾ãã®ãæ°æã¡æªãã§ããã
ã¨ãããã¨ã§å¿æã¡Aãããå¿æ´ã
ãã¼ãå ¨ç¶ãã©ã¤ãããªããããã£ã¨ã¡ããã¨èããã¹ãï¼ï¼