首页 \ 问答 \ super(new Something(...))这个构造错了吗?(super(new Something(…)) is this construct wrong?)

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 operator super( 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
更新时间:2024-01-31 08:01

最满意答案

这是实用程序方法递归地修剪页面(或父控件)中的所有文本框。

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) ...
  • 如果没有完成这个或者真的像这样,我会认为你的数据库结构可能包括控制的类型,它的名字,它的值类型,如果验证的话,如何验证它,如果需要的话。 然后,在阅读所需控件的记录/数据集时,您可能会将记录集中的控件类型与翻译后的值进行比较,然后将其添加到网络表单中。 例如: if(drow["ControlType"].ToString().ToUpper() == "TEXTBOX") { Label lbl = new Label(); lbl.Text = drow["ControlLabel"]. ...
  • Web Form是一种Asp.net Web设计模式。 开发人员将其作为个人形式开发。 (Home.aspx ==主页)。 代码,客户端脚本,服务器脚本,Html,样式表一切都是耦合的。 因此它从头开始作为一种形式开发。 因此,当托管用户请求主页时,它会使用自己的样式,Html,Codebehind文件来访问页面类。 所有服务器控件必须出现在
    标记内, 标记必须包含runat="server"属性。 runat="server"属性表示应在服务器上处理表单。 它还表明服务器脚本可以访问 ...
  • 我喜欢这样做的方法是创建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 = ...
  • 如果我理解正确,你的WebForm中有一个文本框,用户可以写一些应该是日期的东西,对吗? 有些事你可以做: 您可以使用掩码作为文本框,格式化为您的日期格式。 这将发送一个看起来像日期的字符串(检查以后的方式)。 我认为asp:textbox有这个选项; 无论如何,MaskedEdit控件也有它: http : //www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/MaskedEdit/MaskedEdit.aspx 您可以使用模型类,然后根据该模型中字 ...

相关文章

更多

最新问答

更多
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • 电脑高中毕业学习去哪里培训
  • 电脑系统专业就业状况如何啊?
  • IEnumerable linq表达式(IEnumerable linq expressions)
  • 如何在Spring测试中连接依赖关系(How to wire dependencies in Spring tests)
  • Solr可以在没有Lucene的情况下运行吗?(Can Solr run without Lucene?)
  • 如何保证Task在当前线程上同步运行?(How to guarantee that a Task runs synchronously on the current thread?)
  • 在保持每列的类的同时向数据框添加行(Adding row to data frame while maintaining the class of each column)
  • 的?(The ? marks in emacs/haskell and ghc mode)
  • 一个线程可以调用SuspendThread传递自己的线程ID吗?(Can a thread call SuspendThread passing its own thread ID?)
  • 延迟socket.io响应,并“警告 - websocket连接无效”(Delayed socket.io response, and “warn - websocket connection invalid”)
  • 悬停时的图像转换(Image transition on hover)
  • IIS 7.5仅显示homecontroller(IIS 7.5 only shows homecontroller)
  • 没有JavaScript的复选框“关闭”值(Checkbox 'off' value without JavaScript)
  • java分布式框架有哪些
  • Python:填写表单并点击按钮确认[关闭](Python: fill out a form and confirm with a button click [closed])
  • PHP将文件链接到根文件目录(PHP Linking Files to Root File Directory)
  • 我如何删除ListView中的项目?(How I can remove a item in my ListView?)
  • 您是否必须为TFS(云)中的每个BUG创建一个TASK以跟踪时间?(Do you have to create a TASK for every BUG in TFS (Cloud) to track time?)
  • typoscript TMENU ATagParams小写(typoscript TMENU ATagParams lowercase)
  • 武陟会计培训类的学校哪个好点?
  • 从链接中删除文本修饰(Remove text decoration from links)