为什么javabean应该有一个no

编程入门 行业动态 更新时间:2024-10-26 12:22:44
本文介绍了为什么javabean应该有一个no-arg构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

无论我提到哪种材料,都提到它在JavaBean中有一个无参数构造函数。 我怀疑编译器是否创建了一个默认的无参数构造函数,如果它没有遇到任何程序中的构造函数。那么,为什么需要创建一个无参数构造函数呢? 我尝试过: 我在几个论坛中引用了这些解决方案,他们提到必须有一个无参数构造函数,以便需要自动创建Java Bean实例的工具可以这样做,没有复杂的机制来确定需要传递什么参数以及如何获得对这些参数的访问。但是编译器已经创建了一个,那么我们为什么要创建一个默认的无参数构造函数呢?在此先感谢您的解决方案。

Whichever material I refer to, its mentioned to have a no-arg constructor in a JavaBean. My doubt is that the compiler creates a default no-arg constructor if it doesn't encounter any constructor(s) in the program. So, why there is a need to create a no-arg constructor? What I have tried: I've referred the solutions in a few forums in which they've mentioned that there has to be a no argument constructor so that the tools that need to automatically create an instance of the Java Bean can do so, without complex mechanisms to figure out what parameters need to be passed and how to gain access to those parameters. But the compiler already creates one so why should we create a default no-arg constructor? Thanks in advance for your solutions.

推荐答案

如果没有构造函数,那么默认是没有参数的构造函数。 If there is no constructor then default is constructor without arguments. public class HelloWorld{ HelloWorld(){ System.out.println("test"+ l); } public static void main(String []args){ System.out.println("Hello World"); new HelloWorld(); } }

结果

Result

Hello World test

但是如果你用参数添加一个char,那么这是你唯一的构造函数。默认值消失。

But if you add a single char with argument then that's the only constructor you have. Defaults are gone.

public class HelloWorld{ HelloWorld(String l){ System.out.println("test"); } public static void main(String []args){ System.out.println("Hello World"); new HelloWorld(); } }

结果

Result

HelloWorld.java:7: error: constructor HelloWorld in class HelloWorld cannot be applied to given types; new HelloWorld(); ^ required: String found: no arguments reason: actual and formal argument lists differ in length 1 error

更多推荐

为什么javabean应该有一个no

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

发布评论

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

>www.elefans.com

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