super(new Something(...))这个构造错了吗?(super(new Something(…)) is this construct wrong?)
我正在看一些java代码,其中类的构造函数如下所示:
public class X extends ... { X(String s) { super(new Y(s)); } }
这总是错的吗? 换句话说,它是在构造函数中创建一个对象,将它传递给'super'总是错误的吗? 如果是这样的话?
编辑:为什么我认为这可能是错的?
在创建类X时,jvm做的第一件事就是在超类上调用super。 (即使暗示'super()'即可完成)你只能进行有限数量的操作,例如
super(new Y(s));
也许用三元运算符super( s != null ? new Y(s) : new Y() )
调用super( s != null ? new Y(s) : new Y() )
但是你不能再添加任何逻辑因为必须先调用super! 这就是为什么我认为这是错误的I am looking at some java code where the constructor of a class looks like this:
public class X extends ... { X(String s) { super(new Y(s)); } }
is that always wrong ? in other words is it creating an object in the constructor to pass it to 'super' always wrong ? if so why ?
EDIT: Why do I think it might be wrong ?
when creating a class X the first thing the jvm does is to call super on its super class. (that's done even if 'super()' is implied) you can only do a limited amount of operations such as
super(new Y(s));
maybe a call to super with the ternary operatorsuper( s != null ? new Y(s) : new Y() )
but you would not be able to put any more logic because super must be called first !!! That's why I think it's wrong
原文:https://stackoverflow.com/questions/15229941
最满意答案
这是实用程序方法递归地修剪页面(或父控件)中的所有文本框。
public static void TrimTextBoxesRecursive(Control root) { foreach (Control control in root.Controls) { if (control is TextBox) { var textbox = control as TextBox; textbox.Text = textbox.Text.Trim(); } else { TrimTextBoxesRecursive(control); } } }
用法
protected void Button1_Click(object sender, EventArgs e) { TrimTextBoxesRecursive(Page); }
Here is the utility method to trim all TextBoxes in a page (or a parent control) recursively.
public static void TrimTextBoxesRecursive(Control root) { foreach (Control control in root.Controls) { if (control is TextBox) { var textbox = control as TextBox; textbox.Text = textbox.Text.Trim(); } else { TrimTextBoxesRecursive(control); } } }
Usage
protected void Button1_Click(object sender, EventArgs e) { TrimTextBoxesRecursive(Page); }
相关问答
更多-
WebForms是一种抽象,它隐藏了开发人员的网络机制。 它使桌面开发人员能够相对轻松地将他们的技能转移到Web上。 虽然它的确部分实现了这一点,但在实际情况下,通常在抽象破裂之前不久,人们就必须投入凌乱的替代方案。 单元测试很困难,因为处理用户交互的逻辑与UI紧密结合。 典型的WebForms应用程序生成的HTML远非最佳。 它通常很臃肿,很难阅读,并且包含很多仅允许抽象工作的内容,例如viewstate,这是一个巨大的信息来帮助抽象给开发者带来幻觉,尽管网络是一个无国籍的媒体。 然而,MVC包含了网络的 ...
-
您可以检出规则添加功能 ,但基本上可以这样做: jQuery(function() { // You can specify some validation options here but not rules and messages jQuery('form').validate(); // Add a custom class to your name mangled input and add rules like this jQuery('.username'). ...
-
有3种口味的ASP.NET 第一个是最旧的,被称为Web窗体。 基本上它是一个高级的面向组件的Web框架,它与诸如按钮和网格之类的控件一起使用,这些控件封装了行为和查看。 这是ASP.NET最受欢迎的风格,但是由于缺乏对生成的标记的控制而被批评。 目前大多数新项目都是ASP.NET MVC,但绝对有很多Web Forms。 虽然这是我个人最喜欢的,但我必须指出,开始学习网络编程是一种糟糕的方法,因为它隐藏了您的实现细节(当您有经验时,这是很好的),并且学习有点复杂。 资料来源: http : //www.a ...
-
public class TrimModelBinder : DefaultModelBinder { protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor, object value) ...
-
动态ASP.net Web窗体(Dynamic ASP.net Web Forms)[2023-03-30]
如果没有完成这个或者真的像这样,我会认为你的数据库结构可能包括控制的类型,它的名字,它的值类型,如果验证的话,如何验证它,如果需要的话。 然后,在阅读所需控件的记录/数据集时,您可能会将记录集中的控件类型与翻译后的值进行比较,然后将其添加到网络表单中。 例如: if(drow["ControlType"].ToString().ToUpper() == "TEXTBOX") { Label lbl = new Label(); lbl.Text = drow["ControlLabel"]. ... -
什么是ASP.NET Web表单?(What is an ASP.NET web form?)[2023-09-02]
Web Form是一种Asp.net Web设计模式。 开发人员将其作为个人形式开发。 (Home.aspx ==主页)。 代码,客户端脚本,服务器脚本,Html,样式表一切都是耦合的。 因此它从头开始作为一种形式开发。 因此,当托管用户请求主页时,它会使用自己的样式,Html,Codebehind文件来访问页面类。 所有服务器控件必须出现在 -
我喜欢这样做的方法是创建CustomValidators,绑定到屏幕上的控件,我在OnServerValidate事件中调用我的BL验证。 这样,我的验证逻辑就停留在一个地方。 希望它会有所帮助 A way I like to do it is to create CustomValidators, bound to control on the screen and I call my BL validations in the OnServerValidate event. That way, my v ...
-
Anti-XSS库现在是ASP.Net WebForms中的标准库。 虽然它是次优的。 而最新版本( 4.2 )有几个突破性的错误 ,暂时没有修复。 另请参阅MSDN文章信息安全 - 反跨站点脚本 。 请参阅我应该在ASP.NET MVC中使用Anti-XSS安全运行时引擎吗? 关于MVC的答案。 从那个答案: Phil Haack在这里有一篇有趣的博客文章http://haacked.com/archive/2009/02/07/take-charge-of-your-security.aspx 。 他建 ...
-
这是实用程序方法递归地修剪页面(或父控件)中的所有文本框。 public static void TrimTextBoxesRecursive(Control root) { foreach (Control control in root.Controls) { if (control is TextBox) { var textbox = control as TextBox; textbox.Text = ...
-
在ASP.NET Web窗体中收集输入值的推荐方法是什么(What's the recommended way to collect input values in ASP.NET Web Form)[2022-02-21]
如果我理解正确,你的WebForm中有一个文本框,用户可以写一些应该是日期的东西,对吗? 有些事你可以做: 您可以使用掩码作为文本框,格式化为您的日期格式。 这将发送一个看起来像日期的字符串(检查以后的方式)。 我认为asp:textbox有这个选项; 无论如何,MaskedEdit控件也有它: http : //www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/MaskedEdit/MaskedEdit.aspx 您可以使用模型类,然后根据该模型中字 ...