Petzold假设我能搞清楚

编程入门 行业动态 更新时间:2024-10-26 14:34:46
本文介绍了Petzold假设我能搞清楚 - 我很难过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

首先,我要提前感谢那些可以帮助解释这个 神秘代码的人。 Petzold是一位很棒的老师,我很感激他的工作;但是,他的 解释了当一个类包含在一个类中时所需的额外代码行与在一个单独的文件中定义的类相比 $ b项目中的$ b不符合我的理解能力。 注意:我已经在底部包含了代码(4个文件),这个是一个 长篇大论;然而,为了强调 a库文件和一个包含文件之间的微小差异......我觉得有必要将代码全部包含在内。 整个代码。 重述上述>本地定义的 类中的事件覆盖不需要调用基类方法;然而, 在DLL中定义SAME类时具有完全相同的 功能......你必须调用基本方法。 protected override void OnClick(EventArgs args) { base.OnClick(args); // ***这是额外的代码行*** MessageBox.Show(MessageBoxText,Text); } ref:" 2005 Edition - Programming Microsoft Windows Forms - A 简化使用C#的方法,作者:Charles Petzold,第45页。 在Petzold的话中,>我还在OnClick 方法中添加了一个语句来调用基类中的相同方法(即Button)。 如果没有这个陈述,程序就无法为MessageButton附上一个Click事件 处理程序。 我的困惑引发了很多问题: 1)为什么库文件中定义的类需要这个额外的行 代码?在JIT编译器中是否存在问题,这只是一个技巧。使用DLL工作? (我对此表示怀疑。编程是非常精确的。我只是感到困惑!!) 2)你怎么知道何时调用基类方法?如果你是 重写方法...为什么必须调用基本方法?如果 基本方法做某事你希望避免或改变 覆盖它...是否有必要调用它? 3)库文件不是只是一块可重复使用的代码块和/ b $ b基本上与包含在项目中的相同? (让's $ 排除非托管DLL'和托管 代码之间的数据参数编组。确保有效的复杂性......但不是焦点。) 4)是公共还是公共访问图书馆的MessageButton类a 贡献因素? 5)可以应用哪些类型的理解规则,以便知道 何时调用基本方法? 注意:我已编译并运行以下两个程序。它们很坚固,而且b $ b表现相同。此外, 引用文本的所有示例和解决方案文件都可以在以下位置找到: www.microsoft/mspress/com...0-7356-2153-5/ 库文件: //库包含的MessageButton类文件:>> // -------------------------------------------- ----- //来自Charles Petzold的MessageButtonLib.cs(c)2005 // --------------- ---------------------------------- 使用系统; 使用System.Drawing; 使用System.Windows.Forms; 名称空间Petzold.ProgrammingWindowsForms { 公共类MessageButton:Button { string strMessageBoxText; public MessageButton() { 启用= false; } 公共字符串MessageBoxText { 设置 { strMessageBoxText = value; 启用=值!= null&& value.Length 0; } get { return strMessageBoxText; } } protected override无效OnClick(EventArgs args) { base.OnClick(args) ; MessageBox.Show(MessageBoxText,Text); } } } //使用库文件的文件:>> // --------------- ------------------------------------- // ProgramUsingLibrary.cs (c)2005年Charles Petzold // --------------------------------- ------------------- 使用System; 使用System.Drawing; 使用System.Windows.Forms; 使用Petzold.ProgrammingWindowsForms; class ProgramUsingLibrary:Form { [STAThread] public static void Main() { Application.EnableVisualStyles(); Application.Run(new ProgramUsingLibrary()); } public Pr ogramUsingLibrary() { Text =" Program Using Library" ;; MessageButton msgbtn = new MessageButton(); msgbtn.Parent = this; msgbtn.Text ="计算PI的10,000,000位数字; msgbtn.MessageBoxText ="此按钮还没有实现!" ;; msgbtn.Location = new Point(50,50); msgbtn.AutoSize = true; } } ============================ ====================== ======================== ============================================== ==== ======================== ============== ==================================== ============== ========== //本地定义的MessageButton类 //在单独的文件中:>> ; // ----------------------------------- ----------- //来自Charles Petzold的MessageButton.cs(c)2005 // --------- ------------ ------------------------- 使用System; 使用System.Drawing; 使用System.Windows.Forms; class MessageButton:Button { string strMessageBoxText; public MessageButton() { Enabled = false; } 公共字符串MessageBoxText { set { strMessageBoxText = value; 已启用=值!= null&& value.Length 0; } get { return strMessageBoxText; } } protected override void OnClick(EventArgs args) { MessageBox.Show(MessageBoxText,文字); } } //使用MessageButton类的文件:>> // -------------------------------------------- ------ //来自Charles Petzold的MessageButtonDemo.cs(c)2005 // -------------- ------------------------------------ 使用系统; 使用System.Drawing; 使用System.Windows.Forms; class MessageButtonDemo:Form { [STAThread] public static void Main() { Application.EnableVisualStyles(); Application.Run(new MessageButtonDemo()); } public MessageButtonDemo() { Text =" MessageButton Demo"; MessageBu tton msgbtn = new MessageButton(); msgbtn.Parent = this; msgbtn.Text ="计算PI的10,000,000位数; msgbtn.MessageBoxText ="此按钮尚未实现!" ;; msgbtn.Location = new Point(50,50); msgbtn.AutoSize = true ; } }

First, my thanks in advance to those who can help explain away this mystery line of code. Petzold is a great teacher and I appreciate his work; however, his explanation about an extra line of code needed when a class is contained in a library vs the class being defined in a separate file within the project falls short of my ability to understand. Note: I''ve included the code (4 files) at the bottom, making this a long winded posting; however, to emphasize the slight variance between a library file and an included file ... I felt compelled to include the code in it''s entirety. Restating the above >An event override within a locally defined class does NOT require the base class method to be called; however, when defining the SAME class within a DLL with the exact same functionality ... you MUST call the base method. protected override void OnClick(EventArgs args) { base.OnClick(args); // *** This is the extra line of code *** MessageBox.Show(MessageBoxText, Text); } ref: "2005 Edition - Programming Microsoft Windows Forms - A Streamlined Approach Using C#", by Charles Petzold, page #45. In Petzold''s words >"I''ve also added a statement to the OnClick method to call the same method in the base class (which is Button). Without this statement, a program couldn''t attach a Click event handler for MessageButton." My confusion raises many questions: 1) Why does a class defined in a library file require this extra line of code? Is there a problem within the JIT compilers and this is simply a "trick" to make using a DLL work? (I doubt it. Programming is very precise. I am just confused!!) 2) How do you know when to call a base class method? If you are overriding the method ... why must the base method be called? If the base method "does something" that you want to avoid or change by overriding it ... does it make sense to have to call it? 3) Is a library file not simply a block of code that is reusable and essentially the same as being included within the project? (Let''s exclude data argument marshaling between unmanaged DLL''s and managed code. A valid complexity for sure ... but not the focal point.) 4) Is the "public" access of the library''s MessageButton class a contributing factor? 5) What types of rules of understanding can be applied so one knows when to call the base method? Note: I''ve compiled and ran both programs below. They are solid and behave the same. Also, all of the examples and solution files for the referenced text can be found at: www.microsoft/mspress/com...0-7356-2153-5/ The library file: // The Library Contained MessageButton Class File: >> //------------------------------------------------- // MessageButtonLib.cs (c) 2005 by Charles Petzold //------------------------------------------------- using System; using System.Drawing; using System.Windows.Forms; namespace Petzold.ProgrammingWindowsForms { public class MessageButton: Button { string strMessageBoxText; public MessageButton() { Enabled = false; } public string MessageBoxText { set { strMessageBoxText = value; Enabled = value != null && value.Length 0; } get { return strMessageBoxText; } } protected override void OnClick(EventArgs args) { base.OnClick(args); MessageBox.Show(MessageBoxText, Text); } } } // The File For Using the library file: >> //---------------------------------------------------- // ProgramUsingLibrary.cs (c) 2005 by Charles Petzold //---------------------------------------------------- using System; using System.Drawing; using System.Windows.Forms; using Petzold.ProgrammingWindowsForms; class ProgramUsingLibrary: Form { [STAThread] public static void Main() { Application.EnableVisualStyles(); Application.Run(new ProgramUsingLibrary()); } public ProgramUsingLibrary() { Text = "Program Using Library"; MessageButton msgbtn = new MessageButton(); msgbtn.Parent = this; msgbtn.Text = "Calculate 10,000,000 digits of PI"; msgbtn.MessageBoxText = "This button is not yet implemented!"; msgbtn.Location = new Point(50, 50); msgbtn.AutoSize = true; } } ================================================== ======================== ================================================== ======================== ================================================== ======================== // The "Locally Defined MessageButton Class" // Within A Separate File: >> //---------------------------------------------- // MessageButton.cs (c) 2005 by Charles Petzold //---------------------------------------------- using System; using System.Drawing; using System.Windows.Forms; class MessageButton: Button { string strMessageBoxText; public MessageButton() { Enabled = false; } public string MessageBoxText { set { strMessageBoxText = value; Enabled = value != null && value.Length 0; } get { return strMessageBoxText; } } protected override void OnClick(EventArgs args) { MessageBox.Show(MessageBoxText, Text); } } // File For Using the MessageButton Class: >> //-------------------------------------------------- // MessageButtonDemo.cs (c) 2005 by Charles Petzold //-------------------------------------------------- using System; using System.Drawing; using System.Windows.Forms; class MessageButtonDemo: Form { [STAThread] public static void Main() { Application.EnableVisualStyles(); Application.Run(new MessageButtonDemo()); } public MessageButtonDemo() { Text = "MessageButton Demo"; MessageButton msgbtn = new MessageButton(); msgbtn.Parent = this; msgbtn.Text = "Calculate 10,000,000 digits of PI"; msgbtn.MessageBoxText = "This button is not yet implemented!"; msgbtn.Location = new Point(50, 50); msgbtn.AutoSize = true; } }

推荐答案

"汤姆" < Th ******** @ earthlinkwrote in message news:bn ********************** ********** @ 4ax ... "Tom" <Th********@earthlinkwrote in message news:bn********************************@4ax... 首先,我要提前感谢那些可以帮助解释这个问题的人> 神秘的代码行。 Petzold是一位优秀的老师,我很欣赏他的作品;但是,他的 解释了当一个类包含在一个类中时所需的额外代码行与在一个单独的文件中定义的类相比 $ b项目中的$ b不符合我的理解能力。 First, my thanks in advance to those who can help explain away this mystery line of code. Petzold is a great teacher and I appreciate his work; however, his explanation about an extra line of code needed when a class is contained in a library vs the class being defined in a separate file within the project falls short of my ability to understand.

实际上,Petzold并没有说出你归于他的一切;他所说的 点简单地说:我还在OnClick 方法中添加了一个语句来调用基类中的相同方法(这是按钮)。如果没有 这个语句,程序就无法为 MessageButton添加Click事件处理程序。" 我知道,这一切都是正确的,我不知道他在哪里说它是'* b $ b确定*不*在本地调用基本方法。它确实是好的。在 的情况下,但是你的 自己的代码可能会或可能不会关心;但如果你把你的类在DLL中分发给别人 期待不同的行为,那可能是个问题,不是吗? 无论是否有任何不同代码在库文件中或 和包含文件

Actually, Petzold doesn''t say everything you''re attributing to him; the point he makes is simply stated: "I''ve also added a statement to the OnClick method to call the same method in the base class (which is Button). Without this statement, a program couldn''t attach a Click event handler for MessageButton." Far as I know, this is all correct and I don''t know where he says that it''s ok to *not* call the base method "locally." It is indeed "ok" in either case but there is a consequence which you may or may not care about in your own code; but if you distribute your class in a DLL to someone else expecting different behavior, it could be a problem, no? It makes no difference whatsoever whether the code is in a library file or an "included file"

注意:我已经包含了底部的代码(4个文件),这是一个 长篇大论的帖子;然而,为了强调 a库文件和一个包含文件之间的微小差异......我觉得有必要将代码全部包含在内。 整个代码。 重述上述>本地定义的 类中的事件覆盖不需要调用基类方法;然而, 在DLL中定义SAME类时具有完全相同的 功能......你必须调用基本方法。 protected override void OnClick(EventArgs args) { base.OnClick(args); // ***这是额外的代码行*** MessageBox.Show(MessageBoxText,Text); } ref:" 2005 Edition - Programming Microsoft Windows Forms - A 简化使用C#的方法,作者:Charles Petzold,第45页。 在Petzold的话中,>我还在OnClick 方法中添加了一个语句来调用基类中的相同方法(即Button)。 如果没有这个陈述,程序就无法为MessageButton附上一个Click事件 处理程序。 我的困惑引发了很多问题: 1)为什么库文件中定义的类需要这个额外的行 代码?在JIT编译器中是否存在问题,这只是一个技巧。使用DLL工作? (我对此表示怀疑。编程是非常精确的。我只是感到困惑!!) 2)你怎么知道何时调用基类方法?如果你是 重写方法...为什么必须调用基本方法?如果 基本方法做某事你希望避免或改变 覆盖它...是否有必要调用它? 3)库文件不是只是一块可重复使用的代码块和/ b $ b基本上与包含在项目中的相同? (让's $ 排除非托管DLL'和托管 代码之间的数据参数编组。确保有效的复杂性......但不是焦点。) 4)是公共还是公共访问图书馆的MessageButton类a 贡献因素? 5)可以应用哪些类型的理解规则,以便知道 何时调用基本方法? 注意:我已编译并运行以下两个程序。它们很坚固,而且b $ b表现相同。此外, 引用文本的所有示例和解决方案文件都可以在以下位置找到: www.microsoft/mspress/com...0-7356-2153-5/ 库文件: //库包含的MessageButton类文件:>> // -------------------------------------------- ----- //来自Charles Petzold的MessageButtonLib.cs(c)2005 // --------------- ---------------------------------- 使用系统; 使用System.Drawing; 使用System.Windows.Forms; 名称空间Petzold.ProgrammingWindowsForms { 公共类MessageButton:Button { string strMessageBoxText; public MessageButton() { 启用=假; } public string MessageBoxText { set { strMessageBoxText = value; 已启用= value!= null&& value.Length 0; } get { return strMessageBoxText; } } protected override无效OnClick(EventArgs args) { base.OnClick(args) ; MessageBox.Show(MessageBoxText,Text); } } } //使用库文件的文件:>> // --------------- ------------------------------------- // ProgramUsingLibrary.cs (c)2005年Charles Petzold // --------------------------------- ------------------- 使用System; 使用System.Drawing; 使用System.Windows.Forms; 使用Petzold.ProgrammingWindowsForms; class ProgramUsingLibrary:Form { [STAThread] public static void Main() { Application.EnableVisualStyles(); Application.Run(new ProgramUsingLibrary()); } public ProgramUsingLibrary() { Text =" Program Using Library" ;; MessageButton msgbtn = new MessageButton(); msgbtn .Parent = this; msgbtn.Text ="计算PI的10,000,000位数字; msgbtn.MessageBoxText ="此按钮尚未实现!; msgbtn.Location = new Point(50,50); msgbtn.AutoSize = true; } } ==================================== ============== ======================== ==== ============================================== ==== ==================== ====================== ============================ ====================== == //本地定义的MessageButton类 //随着在单独的文件中:>> // --------------------------- ------------------- //来自Charles Petzold的MessageButton.cs(c)2005 // - --------------------------------------------- 使用System; 使用System.Drawing; 使用System.Windows.Forms; class MessageButton:Button { string strMessageBoxText; public MessageButton() { 启用= false; } 公共字符串MessageBoxText { set { strMessageBoxText = value; Enabled = value!= null&& value.Length 0; } get { return strMessageBoxText; } } protected override void OnClick(EventArgs args) { MessageBox.Show(MessageBoxText,文字); } } //使用MessageButton类的文件:>> // ---------------------------------------- ---------- //来自Charles Petzold的MessageButtonDemo.cs(c)2005 // ---------- ---------------------------------------- 使用System ; 使用System.Drawing; 使用System.Windows.Forms; class MessageButtonDemo:Form { [STAThread] public static void Main() { Application.EnableVisualStyles( ); Application.Run(new MessageButtonDemo()); } public MessageButtonDemo() { Text =" MessageButton Demo" ;; MessageButton msgbtn = new MessageButton(); msgbtn.Parent = this; msgbtn.Text ="计算PI的10,000,000位数字; msgbtn.MessageBoxText ="此按钮尚未实现!" ;; msgbtn.Location = new Point(50,50); msgbtn.AutoSize = true; } } Note: I''ve included the code (4 files) at the bottom, making this a long winded posting; however, to emphasize the slight variance between a library file and an included file ... I felt compelled to include the code in it''s entirety. Restating the above >An event override within a locally defined class does NOT require the base class method to be called; however, when defining the SAME class within a DLL with the exact same functionality ... you MUST call the base method. protected override void OnClick(EventArgs args) { base.OnClick(args); // *** This is the extra line of code *** MessageBox.Show(MessageBoxText, Text); } ref: "2005 Edition - Programming Microsoft Windows Forms - A Streamlined Approach Using C#", by Charles Petzold, page #45. In Petzold''s words >"I''ve also added a statement to the OnClick method to call the same method in the base class (which is Button). Without this statement, a program couldn''t attach a Click event handler for MessageButton." My confusion raises many questions: 1) Why does a class defined in a library file require this extra line of code? Is there a problem within the JIT compilers and this is simply a "trick" to make using a DLL work? (I doubt it. Programming is very precise. I am just confused!!) 2) How do you know when to call a base class method? If you are overriding the method ... why must the base method be called? If the base method "does something" that you want to avoid or change by overriding it ... does it make sense to have to call it? 3) Is a library file not simply a block of code that is reusable and essentially the same as being included within the project? (Let''s exclude data argument marshaling between unmanaged DLL''s and managed code. A valid complexity for sure ... but not the focal point.) 4) Is the "public" access of the library''s MessageButton class a contributing factor? 5) What types of rules of understanding can be applied so one knows when to call the base method? Note: I''ve compiled and ran both programs below. They are solid and behave the same. Also, all of the examples and solution files for the referenced text can be found at: www.microsoft/mspress/com...0-7356-2153-5/ The library file: // The Library Contained MessageButton Class File: >> //------------------------------------------------- // MessageButtonLib.cs (c) 2005 by Charles Petzold //------------------------------------------------- using System; using System.Drawing; using System.Windows.Forms; namespace Petzold.ProgrammingWindowsForms { public class MessageButton: Button { string strMessageBoxText; public MessageButton() { Enabled = false; } public string MessageBoxText { set { strMessageBoxText = value; Enabled = value != null && value.Length 0; } get { return strMessageBoxText; } } protected override void OnClick(EventArgs args) { base.OnClick(args); MessageBox.Show(MessageBoxText, Text); } } } // The File For Using the library file: >> //---------------------------------------------------- // ProgramUsingLibrary.cs (c) 2005 by Charles Petzold //---------------------------------------------------- using System; using System.Drawing; using System.Windows.Forms; using Petzold.ProgrammingWindowsForms; class ProgramUsingLibrary: Form { [STAThread] public static void Main() { Application.EnableVisualStyles(); Application.Run(new ProgramUsingLibrary()); } public ProgramUsingLibrary() { Text = "Program Using Library"; MessageButton msgbtn = new MessageButton(); msgbtn.Parent = this; msgbtn.Text = "Calculate 10,000,000 digits of PI"; msgbtn.MessageBoxText = "This button is not yet implemented!"; msgbtn.Location = new Point(50, 50); msgbtn.AutoSize = true; } } ================================================== ======================== ================================================== ======================== ================================================== ======================== // The "Locally Defined MessageButton Class" // Within A Separate File: >> //---------------------------------------------- // MessageButton.cs (c) 2005 by Charles Petzold //---------------------------------------------- using System; using System.Drawing; using System.Windows.Forms; class MessageButton: Button { string strMessageBoxText; public MessageButton() { Enabled = false; } public string MessageBoxText { set { strMessageBoxText = value; Enabled = value != null && value.Length 0; } get { return strMessageBoxText; } } protected override void OnClick(EventArgs args) { MessageBox.Show(MessageBoxText, Text); } } // File For Using the MessageButton Class: >> //-------------------------------------------------- // MessageButtonDemo.cs (c) 2005 by Charles Petzold //-------------------------------------------------- using System; using System.Drawing; using System.Windows.Forms; class MessageButtonDemo: Form { [STAThread] public static void Main() { Application.EnableVisualStyles(); Application.Run(new MessageButtonDemo()); } public MessageButtonDemo() { Text = "MessageButton Demo"; MessageButton msgbtn = new MessageButton(); msgbtn.Parent = this; msgbtn.Text = "Calculate 10,000,000 digits of PI"; msgbtn.MessageBoxText = "This button is not yet implemented!"; msgbtn.Location = new Point(50, 50); msgbtn.AutoSize = true; } }

" Tom" < Th ******** @ earthlinkwrote in message news:bn ********************** ********** @ 4ax ... "Tom" <Th********@earthlinkwrote in message news:bn********************************@4ax... 首先,我要提前感谢那些可以帮助解释这个问题的人> 神秘的代码行。 Petzold是一位优秀的老师,我很欣赏他的作品;但是,他的 解释了当一个类包含在一个类中时所需的额外代码行与在一个单独的文件中定义的类相比 $ b项目中的$ b不符合我的理解能力。 First, my thanks in advance to those who can help explain away this mystery line of code. Petzold is a great teacher and I appreciate his work; however, his explanation about an extra line of code needed when a class is contained in a library vs the class being defined in a separate file within the project falls short of my ability to understand.

用下面的代码替换ProgramUsingLibrary.cs文件(这只是订阅Click事件的),然后注释掉 声明: base.OnClick(args); MessageButtonLib.cs中的 你应该看到没有调用基本方法的问题。 // -------------------- -------------------------------- // ProgramUsingLibrary.cs(c)2005 by Charles Petzold // -------------------------------------- -------------- 使用System; 使用System.Drawing; 使用System.Windows .Forms; 使用Petzold.ProgrammingWindowsForms; class ProgramUsingLibrary:Form { [ STAThread] public static void Main() { Application.EnableVisualStyles(); Application.Run (new ProgramUsingLibrary()); } public ProgramUsingLibrary() { Text =" Program Using Library" ;; MessageButton msgbtn = new MessageButton(); msgbtn.Parent = this; msgbtn.Text ="计算PI的10,000,000位数字; msgbtn.MessageBoxText =此按钮尚未实现! ;; msgbtn.Location = new Point(50,50); msgbtn.AutoSize = true; msgbtn。点击+ = new EventHandler(msgbtn_Click); } void msgbtn_Click(object sender,EventArgs e) { MessageBox.Show(" click"); } }

replace the ProgramUsingLibrary.cs file with the code below (which does nothing more than subscribe to the Click event), then comment out the statement: base.OnClick(args); in MessageButtonLib.cs you should see the problem with not calling the base method. //---------------------------------------------------- // ProgramUsingLibrary.cs (c) 2005 by Charles Petzold //---------------------------------------------------- using System; using System.Drawing; using System.Windows.Forms; using Petzold.ProgrammingWindowsForms; class ProgramUsingLibrary: Form { [STAThread] public static void Main() { Application.EnableVisualStyles(); Application.Run(new ProgramUsingLibrary()); } public ProgramUsingLibrary() { Text = "Program Using Library"; MessageButton msgbtn = new MessageButton(); msgbtn.Parent = this; msgbtn.Text = "Calculate 10,000,000 digits of PI"; msgbtn.MessageBoxText = "This button is not yet implemented!"; msgbtn.Location = new Point(50, 50); msgbtn.AutoSize = true; msgbtn.Click += new EventHandler(msgbtn_Click); } void msgbtn_Click(object sender, EventArgs e) { MessageBox.Show("click"); } }

注意:我已经在底部包含了代码(4个文件),这是一个 冗长的帖子;然而,为了强调 a库文件和一个包含文件之间的微小差异......我觉得有必要将代码全部包含在内。 整个代码。 重述上述>本地定义的 类中的事件覆盖不需要调用基类方法;然而, 在DLL中定义SAME类时具有完全相同的 功能......你必须调用基本方法。 protected override void OnClick(EventArgs args) { base.OnClick(args); // ***这是额外的代码行*** MessageBox.Show(MessageBoxText,Text); } ref:" 2005 Edition - Programming Microsoft Windows Forms - A 简化使用C#的方法,作者:Charles Petzold,第45页。 在Petzold的话中,>我还在OnClick 方法中添加了一个语句来调用基类中的相同方法(即Button)。 如果没有这个陈述,程序就无法为MessageButton附上一个Click事件 处理程序。 我的困惑引发了很多问题: 1)为什么库文件中定义的类需要这个额外的行 代码?在JIT编译器中是否存在问题,这只是一个技巧。使用DLL工作? (我对此表示怀疑。编程是非常精确的。我只是感到困惑!!) 2)你怎么知道何时调用基类方法?如果你是 重写方法...为什么必须调用基本方法?如果 基本方法做某事你希望避免或改变 覆盖它...是否有必要调用它? 3)库文件不是只是一块可重复使用的代码块和/ b $ b基本上与包含在项目中的相同? (让's $ 排除非托管DLL'和托管 代码之间的数据参数编组。确保有效的复杂性......但不是焦点。) 4)是公共还是公共访问图书馆的MessageButton类a 贡献因素? 5)可以应用哪些类型的理解规则,以便知道 何时调用基本方法? 注意:我已编译并运行以下两个程序。它们很坚固,而且b $ b表现相同。此外, 引用文本的所有示例和解决方案文件都可以在以下位置找到: www.microsoft/mspress/com...0-7356-2153-5/ 库文件: //库包含的MessageButton类文件:>> // -------------------------------------------- ----- //来自Charles Petzold的MessageButtonLib.cs(c)2005 // --------------- ---------------------------------- 使用系统; 使用System.Drawing; 使用System.Windows.Forms; 名称空间Petzold.ProgrammingWindowsForms { 公共类MessageButton:Button { string strMessageBoxText; public MessageButton() { 启用=假; } public string MessageBoxText { set { strMessageBoxText = value; 已启用= value!= null&& value.Length 0; } get { return strMessageBoxText; } } protected override无效OnClick(EventArgs args) { base.OnClick(args) ; MessageBox.Show(MessageBoxText,Text); } } } //使用库文件的文件:>> // --------------- ------------------------------------- // ProgramUsingLibrary.cs (c)2005年Charles Petzold // --------------------------------- ------------------- 使用System; 使用System.Drawing; 使用System.Windows.Forms; 使用Petzold.ProgrammingWindowsForms; class ProgramUsingLibrary:Form { [STAThread] public static void Main() { Application.EnableVisualStyles(); Application.Run(new ProgramUsingLibrary()); } public ProgramUsingLibrary() { Text =" Program Using Library" ;; MessageButton msgbtn = new MessageButton(); msgbtn .Parent = this; msgbtn.Text ="计算PI的10,000,000位数字; msgbtn.MessageBoxText ="此按钮尚未实现!; msgbtn.Location = new Point(50,50); msgbtn.AutoSize = true; } } ==================================== ============== ======================== ==== ============================================== ==== ==================== ====================== ============================ ====================== == //本地定义的MessageButton类 //随着在单独的文件中:>> // --------------------------- ------------------- //来自Charles Petzold的MessageButton.cs(c)2005 // - --------------------------------------------- 使用System; 使用System.Drawing; 使用System.Windows.Forms; class MessageButton:Button { string strMessageBoxText; public MessageButton() { 启用= false; } 公共字符串MessageBoxText { set { strMessageBoxText = value; Enabled = value!= null&& value.Length 0; } get { return strMessageBoxText; } } protected override void OnClick(EventArgs args) { MessageBox.Show(MessageBoxText,文字); } } //使用MessageButton类的文件:>> // ---------------------------------------- ---------- //来自Charles Petzold的MessageButtonDemo.cs(c)2005 // ---------- ---------------------------------------- 使用System ; 使用System.Drawing; 使用System.Windows.Forms; class MessageButtonDemo:Form { [STAThread] public static void Main() { Application.EnableVisualStyles( ); Application.Run(new MessageButtonDemo()); } public MessageButtonDemo() { Text =" MessageButton Demo" ;; MessageButton msgbtn = new MessageButton(); msgbtn.Parent = this; msgbtn.Text ="计算PI的10,000,000位数字; msgbtn.MessageBoxText ="此按钮尚未实现!" ;; msgbtn.Location = new Point(50,50); msgbtn.AutoSize = true; } } Note: I''ve included the code (4 files) at the bottom, making this a long winded posting; however, to emphasize the slight variance between a library file and an included file ... I felt compelled to include the code in it''s entirety. Restating the above >An event override within a locally defined class does NOT require the base class method to be called; however, when defining the SAME class within a DLL with the exact same functionality ... you MUST call the base method. protected override void OnClick(EventArgs args) { base.OnClick(args); // *** This is the extra line of code *** MessageBox.Show(MessageBoxText, Text); } ref: "2005 Edition - Programming Microsoft Windows Forms - A Streamlined Approach Using C#", by Charles Petzold, page #45. In Petzold''s words >"I''ve also added a statement to the OnClick method to call the same method in the base class (which is Button). Without this statement, a program couldn''t attach a Click event handler for MessageButton." My confusion raises many questions: 1) Why does a class defined in a library file require this extra line of code? Is there a problem within the JIT compilers and this is simply a "trick" to make using a DLL work? (I doubt it. Programming is very precise. I am just confused!!) 2) How do you know when to call a base class method? If you are overriding the method ... why must the base method be called? If the base method "does something" that you want to avoid or change by overriding it ... does it make sense to have to call it? 3) Is a library file not simply a block of code that is reusable and essentially the same as being included within the project? (Let''s exclude data argument marshaling between unmanaged DLL''s and managed code. A valid complexity for sure ... but not the focal point.) 4) Is the "public" access of the library''s MessageButton class a contributing factor? 5) What types of rules of understanding can be applied so one knows when to call the base method? Note: I''ve compiled and ran both programs below. They are solid and behave the same. Also, all of the examples and solution files for the referenced text can be found at: www.microsoft/mspress/com...0-7356-2153-5/ The library file: // The Library Contained MessageButton Class File: >> //------------------------------------------------- // MessageButtonLib.cs (c) 2005 by Charles Petzold //------------------------------------------------- using System; using System.Drawing; using System.Windows.Forms; namespace Petzold.ProgrammingWindowsForms { public class MessageButton: Button { string strMessageBoxText; public MessageButton() { Enabled = false; } public string MessageBoxText { set { strMessageBoxText = value; Enabled = value != null && value.Length 0; } get { return strMessageBoxText; } } protected override void OnClick(EventArgs args) { base.OnClick(args); MessageBox.Show(MessageBoxText, Text); } } } // The File For Using the library file: >> //---------------------------------------------------- // ProgramUsingLibrary.cs (c) 2005 by Charles Petzold //---------------------------------------------------- using System; using System.Drawing; using System.Windows.Forms; using Petzold.ProgrammingWindowsForms; class ProgramUsingLibrary: Form { [STAThread] public static void Main() { Application.EnableVisualStyles(); Application.Run(new ProgramUsingLibrary()); } public ProgramUsingLibrary() { Text = "Program Using Library"; MessageButton msgbtn = new MessageButton(); msgbtn.Parent = this; msgbtn.Text = "Calculate 10,000,000 digits of PI"; msgbtn.MessageBoxText = "This button is not yet implemented!"; msgbtn.Location = new Point(50, 50); msgbtn.AutoSize = true; } } ================================================== ======================== ================================================== ======================== ================================================== ======================== // The "Locally Defined MessageButton Class" // Within A Separate File: >> //---------------------------------------------- // MessageButton.cs (c) 2005 by Charles Petzold //---------------------------------------------- using System; using System.Drawing; using System.Windows.Forms; class MessageButton: Button { string strMessageBoxText; public MessageButton() { Enabled = false; } public string MessageBoxText { set { strMessageBoxText = value; Enabled = value != null && value.Length 0; } get { return strMessageBoxText; } } protected override void OnClick(EventArgs args) { MessageBox.Show(MessageBoxText, Text); } } // File For Using the MessageButton Class: >> //-------------------------------------------------- // MessageButtonDemo.cs (c) 2005 by Charles Petzold //-------------------------------------------------- using System; using System.Drawing; using System.Windows.Forms; class MessageButtonDemo: Form { [STAThread] public static void Main() { Application.EnableVisualStyles(); Application.Run(new MessageButtonDemo()); } public MessageButtonDemo() { Text = "MessageButton Demo"; MessageButton msgbtn = new MessageButton(); msgbtn.Parent = this; msgbtn.Text = "Calculate 10,000,000 digits of PI"; msgbtn.MessageBoxText = "This button is not yet implemented!"; msgbtn.Location = new Point(50, 50); msgbtn.AutoSize = true; } }

谢谢Liz - 我确实评论出了神秘我认为 行为没有区别。我将Petzold归因为不的原因是不。在本地定义的文件中调用 基本方法>那行代码是 不存在。他不遗余力地说他已经将它添加到了 DLL版本中,如果没有它就会出现问题......但是我没有看到这么多b $ b看到了导致代码行为出现问题。 我在本书的其他一些例子中注意到,对于基本方法的类似 调用存在于受保护的覆盖中。 对我来说,似乎应该避免使用继承的方法,只执行 重写代码?调用一种不需要行为的方法似乎不合逻辑吗?也许如果覆盖仅仅是添加要基本方法 ...然后你打电话给基地并用你的 额外编码跟着它? 我留下困惑...但非常感谢你的评论。 事实上,在评论出该行之后我看不到任何行为改变 让我想知道是否JIT编译器可能有一个错误, 现在已修复? Petzold在第xiii页讨论了 示例基于特定的预发布版本:2005年8月 社区技术评论。 我正在试图仔细审查Petzold的工作并完全理解它。他/ b $ b确实有时跳跃,因此迫使这个学生稍微挖掘一下 了解这些例子。痛苦,但从长远来看,旅程也会显示额外的花絮。这只是特定的代码行 和推理。在它背后让我难过。 再次感谢。 - 汤姆 周五,2007年11月2日18:00:08 -0500,Liz, < li*@tiredofspamwrote: Thanks Liz -- I did comment out the "mystery" line and I see no difference in behavior. The reason I attribute Petzold for it to be OK "not" to call the base method in the locally defined file >that line of code is not there. He goes out of his way to say that he has added it in the DLL version and that without it there is a problem ... but I do not see the resulting problems in the code behavior. I notice in some of the other examples within the book that similar calls to the base method are present in protected overrides. To me it seems the inherited methods should be avoided and only the overridden code be executed? It seems illogical to call a method who''s behavior is not desired? Perhaps if the override is simply "adding" to the base method ... then you call the base and follow it with your additional coding? I remain confused ... but very much appreciate your comments. The fact I see no behavior changes after commenting out that line makes me wonder if the JIT compiler might have had a bug in it that has now been fixed? Petzold does discuss on page xiii that the examples were based on a specific pre-release version: August 2005 Community Technical Review. I''m trying to scrutinize Petzold''s work and understand it fully. He does leap at times and thus forces this student to dig a bit to understand the examples. Painful, but in the long run the journey also reveals additional tidbits. It is just this particular line of code and the "reasoning" behind it has me stumped. Thanks again. -- Tom On Fri, 2 Nov 2007 18:00:08 -0500, "Liz" <li*@tiredofspamwrote: > " Tom" < Th ******** @ earthlinkwrote in message 新闻:bn ************************** ******@4ax .. >"Tom" <Th********@earthlinkwrote in messagenews:bn********************************@4ax.. . >首先,我要提前感谢那些可以帮助解释这个神秘的代码行的人。 Petzold是一位伟大的老师,我很欣赏他的作品;但是,当一个类包含在一个库中而不是在一个单独的文件中定义的类时,他需要额外的一行代码来解释这个项目在项目中没有达到我的理解能力。 >First, my thanks in advance to those who can help explain away thismystery line of code.Petzold is a great teacher and I appreciate his work; however, hisexplanation about an extra line of code needed when a class iscontained in a library vs the class being defined in a separate filewithin the project falls short of my ability to understand.

用下面的代码替换ProgramUsingLibrary.cs文件(它只是订阅Click事件),然后注释掉语句: base.OnClick(args);in MessageButtonLib.csyou should see the problem with not calling the base method. //---------------------------------------- ------------// ProgramUsingLibrary.cs (c) 2005 by Charles Petzold//---------------- ------------------------------------using System;using System. Drawing;using System.Windows.Forms;using Petzold.ProgrammingWindowsForms;class ProgramUsingLibrary: Form{ [STAThread] public static void Main() { Application.EnableVisualStyles(); Application.Run(new ProgramUsingLibrary()); } public ProgramUsingLibrary() { Text = "Program Using Library"; MessageButton msgbtn = new MessageButton(); msgbtn.Parent = this; msgbtn.Text = "Calculate 10,000,000 digits of PI"; msgbtn.MessageBoxText = "This button is not yet implemented!"; msgbtn.Location = new Point(50, 50); msgbtn.AutoSize = true; msgbtn.Click += new EventHandler(msgbtn_Click); } void msgbtn_Click(object sender, EventArgs e) { MessageBox.Show("click"); } }

replace the ProgramUsingLibrary.cs file with the code below (which doesnothing more than subscribe to the Click event), then comment out thestatement: base.OnClick(args);in MessageButtonLib.csyou should see the problem with not calling the base method. //----------------------------------------------------// ProgramUsingLibrary.cs (c) 2005 by Charles Petzold//----------------------------------------------------using System;using System.Drawing;using System.Windows.Forms;using Petzold.ProgrammingWindowsForms;class ProgramUsingLibrary: Form{ [STAThread] public static void Main() { Application.EnableVisualStyles(); Application.Run(new ProgramUsingLibrary()); } public ProgramUsingLibrary() { Text = "Program Using Library"; MessageButton msgbtn = new MessageButton(); msgbtn.Parent = this; msgbtn.Text = "Calculate 10,000,000 digits of PI"; msgbtn.MessageBoxText = "This button is not yet implemented!"; msgbtn.Location = new Point(50, 50); msgbtn.AutoSize = true; msgbtn.Click += new EventHandler(msgbtn_Click); } void msgbtn_Click(object sender, EventArgs e) { MessageBox.Show("click"); } }

\t \t\t\t\t>Note: I’’ve included the code (4 files) at the bottom, making this along winded posting; however, to emphasize the slight variance betweena library file and an included file ... I felt compelled to includethe code in it’’s entirety.Restating the above >An event override within a locally definedclass does NOT require the base class method to be called; however,when defining the SAME class within a DLL with the exact samefunctionality ... you MUST call the base method.protected override void OnClick(EventArgs args){base.OnClick(args); // *** This is the extra line of code ***MessageBox.Show(MessageBoxText, Text);}ref: "2005 Edition - Programming Microsoft Windows Forms - AStreamlined Approach Using C#", by Charles Petzold, page #45.In Petzold’’s words >"I’’ve also added a statement to the OnClickmethod to call the same method in the base class (which is Button).Without this statement, a program couldn’’t attach a Click eventhandler for MessageButton."My confusion raises many questions:1) Why does a class defined in a library file require this extra lineof code? Is there a problem within the JIT compilers and this issimply a "trick" to make using a DLL work? (I doubt it. Programming isvery precise. I am just confused!!)2) How do you know when to call a base class method? If you areoverriding the method ... why must the base method be called? If thebase method "does something" that you want to avoid or change byoverriding it ... does it make sense to have to call it?3) Is a library file not simply a block of code that is reusable andessentially the same as being included within the project? (Let’’sexclude data argument marshaling between unmanaged DLL’’s and managedcode. A valid complexity for sure ... but not the focal point.)4) Is the "public" access of the library’’s MessageButton class acontributing factor?5) What types of rules of understanding can be applied so one knowswhen to call the base method?Note: I’’ve compiled and ran both programs below. They are solid andbehave the same. Also, all of the examples and solution files for thereferenced text can be found at:www.microsoft/mspress/com...0-7356-2153-5/The library file:// The Library Contained MessageButton Class File: >>//-------------------------------------------------// MessageButtonLib.cs (c) 2005 by Charles Petzold//-------------------------------------------------using System;using System.Drawing;using System.Windows.Forms;namespace Petzold.ProgrammingWindowsForms{ public class MessageButton: Button { string strMessageBoxText; public MessageButton() { Enabled = false; } public string MessageBoxText { set { strMessageBoxText = value; Enabled = value != null && value.Length 0; } get { return strMessageBoxText; } } protected override void OnClick(EventArgs args) { base.OnClick(args); MessageBox.Show(MessageBoxText, Text); } }}// The File For Using the library file: >>//----------------------------------------------------// ProgramUsingLibrary.cs (c) 2005 by Charles Petzold//----------------------------------------------------using System;using System.Drawing;using System.Windows.Forms;using Petzold.ProgrammingWindowsForms;class ProgramUsingLibrary: Form{ [STAThread] public static void Main() { Application.EnableVisualStyles(); Application.Run(new ProgramUsingLibrary()); } public ProgramUsingLibrary() { Text = "Program Using Library"; MessageButton msgbtn = new MessageButton(); msgbtn.Parent = this; msgbtn.Text = "Calculate 10,000,000 digits of PI"; msgbtn.MessageBoxText = "This button is not yet implemented!"; msgbtn.Location = new Point(50, 50); msgbtn.AutoSize = true; }}================================================= ========================================================================== ========================================================================== =========================// The "Locally Defined MessageButton Class"// Within A Separate File: >>//----------------------------------------------// MessageButton.cs (c) 2005 by Charles Petzold//----------------------------------------------using System;using System.Drawing;using System.Windows.F orms;class MessageButton: Button{ string strMessageBoxText; public MessageButton() { Enabled = false; } public string MessageBoxText { set { strMessageBoxText = value; Enabled = value != null && value.Length 0; } get { return strMessageBoxText; } } protected override void OnClick(EventArgs args) { MessageBox.Show(MessageBoxText, Text); }} // File For Using the MessageButton Class: >>//--------------------------------------------------// MessageButtonDemo.cs (c) 2005 by Charles Petzold//--------------------------------------------------using System;using System.Drawing;using System.Windows.Forms;class MessageButtonDemo: Form{ [STAThread] public static void Main() { Application.EnableVisualStyles(); Application.Run(new MessageButtonDemo()); } public MessageButtonDemo() { Text = "MessageButton Demo"; MessageButton msgbtn = new MessageButton(); msgbtn.Parent = this; msgbtn.Text = "Calculate 10,000,000 digits of PI"; msgbtn.MessageBoxText = "This button is not yet implemented!"; msgbtn.Location = new Point(50, 50); msgbtn.AutoSize = true; }} >Note: I''ve included the code (4 files) at the bottom, making this along winded posting; however, to emphasize the slight variance betweena library file and an included file ... I felt compelled to includethe code in it''s entirety.Restating the above >An event override within a locally definedclass does NOT require the base class method to be called; however,when defining the SAME class within a DLL with the exact samefunctionality ... you MUST call the base method.protected override void OnClick(EventArgs args){base.OnClick(args); // *** This is the extra line of code ***MessageBox.Show(MessageBoxText, Text);}ref: "2005 Edition - Programming Microsoft Windows Forms - AStreamlined Approach Using C#", by Charles Petzold, page #45.In Petzold''s words >"I''ve also added a statement to the OnClickmethod to call the same method in the base class (which is Button).Without this statement, a program couldn''t attach a Click eventhandler for MessageButton."My confusion raises many questions:1) Why does a class defined in a library file require this extra lineof code? Is there a problem within the JIT compilers and this issimply a "trick" to make using a DLL work? (I doubt it. Programming isvery precise. I am just confused!!)2) How do you know when to call a base class method? If you areoverriding the method ... why must the base method be called? If thebase method "does something" that you want to avoid or change byoverriding it ... does it make sense to have to call it?3) Is a library file not simply a block of code that is reusable andessentially the same as being included within the project? (Let''sexclude data argument marshaling between unmanaged DLL''s and managedcode. A valid complexity for sure ... but not the focal point.)4) Is the "public" access of the library''s MessageButton class acontributing factor?5) What types of rules of understanding can be applied so one knowswhen to call the base method?Note: I''ve compiled and ran both programs below. They are solid andbehave the same. Also, all of the examples and solution files for thereferenced text can be found at:www.microsoft/mspress/com...0-7356-2153-5/The library file:// The Library Contained MessageButton Class File: >>//-------------------------------------------------// MessageButtonLib.cs (c) 2005 by Charles Petzold//-------------------------------------------------using System;using System.Drawing;using System.Windows.Forms;namespace Petzold.ProgrammingWindowsForms{ public class MessageButton: Button { string strMessageBoxText; public MessageButton() { Enabled = false; } public string MessageBoxText { set { strMessageBoxText = value; Enabled = value != null && value.Length 0; } get { return strMessageBoxText; } } protected override void OnClick(EventArgs args) { base.OnClick(args); MessageBox.Show(MessageBoxText, Text); } }}// The File For Using the library file: >>//----------------------------------------------------// ProgramUsingLibrary.cs (c) 2005 by Charles Petzold//----------------------------------------------------using System;using System.Drawing;using System.Windows.Forms;using Petzold.ProgrammingWindowsForms;class ProgramUsingLibrary: Form{ [STAThread] public static void Main() { Application.EnableVisualStyles(); Application.Run(new ProgramUsingLibrary()); } public ProgramUsingLibrary() { Text = "Program Using Library"; MessageButton msgbtn = new MessageButton(); msgbtn.Parent = this; msgbtn.Text = "Calculate 10,000,000 digits of PI"; msgbtn.MessageBoxText = "This button is not yet implemented!"; msgbtn.Location = new Point(50, 50); msgbtn.AutoSize = true; }}================================================= ========================================================================== ========================================================================== =========================// The "Locally Defined MessageButton Class"// Within A Separate File: >>//----------------------------------------------// MessageButton.cs (c) 2005 by Charles Petzold//----------------------------------------------using System;using System.Drawing;using System.Windows.Forms;class MessageButton: Button{ string strMessageBoxText; public MessageButton() { Enabled = false; } public string MessageBoxText { set { strMessageBoxText = value; Enabled = value != null && value.Length 0; } get { return strMessageBoxText; } } protected override void OnClick(EventArgs args) { MessageBox.Show(MessageBoxText, Text); }} // File For Using the MessageButton Class: >>//--------------------------------------------------// MessageButtonDemo.cs (c) 2005 by Charles Petzold//--------------------------------------------------using System;using System.Drawing;using System.Windows.Forms;class MessageButtonDemo: Form{ [STAThread] public static void Main() { Application.EnableVisualStyles(); Application.Run(new MessageButtonDemo()); } public MessageButtonDemo() { Text = "MessageButton Demo"; MessageButton msgbtn = new MessageButton(); msgbtn.Parent = this; msgbtn.Text = "Calculate 10,000,000 digits of PI"; msgbtn.MessageBoxText = "This button is not yet implemented!"; msgbtn.Location = new Point(50, 50); msgbtn.AutoSize = true; }}

更多推荐

Petzold假设我能搞清楚

本文发布于:2023-06-07 15:13:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/568453.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我能   Petzold

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!