Java与C ++中的默认构造函数 [英] default constructor in Java versus C++

查看:92
本文介绍了Java与C ++中的默认构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试比较Java和C ++中的默认构造函数。


在C ++中,默认构造函数具有两种方法之一

1)a构造函数有ZERO参数


学生()

{//等等......

}

2)所有参数都有默认值的构造函数


Student(int age = 10,String name =" Joe")

{// etc ...

}


但是,在Java中,默认构造函数意味着构造函数只有ZERO参数。

学生()

{//等等......

}


以下内容会产生编译错误

学生(int age = 10,String name =" Joe")

{// etc ...

}


为什么Java不支持这个想法?


请指教。谢谢!!

解决方案

>任何想法为什么Java不支持它?


这可能会有所帮助。
http://www.google.com/search?hl=en&& ; l ... zation + C%2B%2B


-

Tony Morris
http://xdweb.net/~dibblego/


< blockquote>

" Matt" < JR ******** @ hotmail.com>在消息中写道

新闻:ba ************************* @ posting.google.co m ... < blockquote class =post_quotes>我尝试比较Java和C ++中的默认构造函数。

在C ++中,默认构造函数具有两种方法之一
1)构造函数具有ZERO参数

学生()
{//等等......

2)所有参数都有默认值的构造函数

Student(int age = 10,String name =" Joe")
{// etc ...

然而,在Java中,默认构造函数意味着构造函数只有ZERO
参数。
学生()
{//等等......


以下将产生编译错误
学生(int age = 10,String name =" Joe")
{// etc ...


任何想法为什么Java不支持那个?

请指教。谢谢!!




同样的原因algol60不支持它。

也许你可以告诉我们为什么C ++并不支持所有

algol60的功能。


Matt写道:

我尝试比较Java和C ++中的默认构造函数。


请注意,这两种语言以不同的方式使用该术语。

这是一个实现级别的概念,自然是实现级别的当语言发生变化时,
关注点会有所不同。在这种情况下,巧合的是,这个术语碰巧在两种语言中都有意义,当C ++含义为时,它会导致一些混乱。

应用于Java。


您似乎正在使用默认构造函数的C ++定义。下面。

C ++定义不适用于Java。在Java中,所有创建或

对象必须指定构造函数的参数列表,因此有

nodefault从这个意义上说。


A默认构造函数但是,在Java中,通常意味着编译器为您生成的构造函数
。例如,以下

类:


A类{}


有一个默认构造函数,自动生成由编译器,

,不带参数。以下课程:


B班

{

公共B(){}

}


在功能上是相同的,但它确实*没有*具有默认的

构造函数。相反,无参数构造函数明确地定义为



但是,在Java中,默认构造函数意味着构造函数仅具有ZERO参数。


确实,在Java中,所有默认构造函数都没有参数。

但是,并非所有零参数构造函数都是

" default",因此不是默认的是指零参数。

以下将产生编译错误
Student(int age = 10,String name =" Joe")
{//etc...
}

任何想法为什么Java不支持它?




Java的设计目标之一是简化分辨率语法。

默认参数的任务很容易通过重载完成,所以

不需要冗余机制。


-
www.designacourse.com

培训任何人的最简单方法......无处不在。


Chris Smith - 首席软件开发人员/技术培训师

MindIQ Corporation


I try to compare the default constructor in Java and C++.

In C++, a default constructor has one of the two meansings
1) a constructor has ZERO parameter

Student()
{ //etc...
}

2) a constructor that all parameters have default values

Student(int age = 10, String name = "Joe")
{ //etc...
}

However, In Java, default constructor means a constructor has ZERO parameter only.

Student()
{ //etc...
}

The following will yield compile errors
Student(int age = 10, String name = "Joe")
{ //etc...
}

Any ideas why Java doesn''t support that?

Please advise. Thanks!!

解决方案

> Any ideas why Java doesn''t support that?

This might help.
http://www.google.com/search?hl=en&l...zation+C%2B%2B

--
Tony Morris
http://xdweb.net/~dibblego/



"Matt" <jr********@hotmail.com> wrote in message
news:ba*************************@posting.google.co m...

I try to compare the default constructor in Java and C++.

In C++, a default constructor has one of the two meansings
1) a constructor has ZERO parameter

Student()
{ //etc...
}

2) a constructor that all parameters have default values

Student(int age = 10, String name = "Joe")
{ //etc...
}

However, In Java, default constructor means a constructor has ZERO parameter only.
Student()
{ //etc...
}

The following will yield compile errors
Student(int age = 10, String name = "Joe")
{ //etc...
}

Any ideas why Java doesn''t support that?

Please advise. Thanks!!



Same reason algol60 doesn''t support it.
Maybe you can tell us why C++ doesn''t support all the
algol60 features.


Matt wrote:

I try to compare the default constructor in Java and C++.

Please note that the two languages use that term in different ways.
It''s an implementation-level concept, and naturally implementation-level
concerns will differ when the language is changed. In this case, it''s
quite by coincidence that the term happens to have a meaning in both
languages, and it results in some confusion when the C++ meaning is
applied to Java.

You seem to be using the C++ definition of "default constructor" below.
The C++ definition does not apply in Java. In Java, all creations or
objects MUST specify an argument list for the constructor, so there is
no "default" in that sense.

A "default constructor" in Java, though, generally means a constructor
that''s generated for you by the compiler. For example, the following
class:

class A { }

has a default constructor, generated automatically by the compiler,
which takes no arguments. The following class:

class B
{
public B() { }
}

is identical in functionality, but it does *not* have a default
constructor. Instead, the no-argument constructor is defined quite
explicitly.
However, In Java, default constructor means a constructor has
ZERO parameter only.
It is true that, in Java, all default constructors have zero parameters.
However, it is not true that all zero-parameter constructors are
"default", and hence not true that default "means" zero-parameter.
The following will yield compile errors
Student(int age = 10, String name = "Joe")
{ //etc...
}

Any ideas why Java doesn''t support that?



One of the design goals of Java is to simplify the resolution of syntax.
The task of default parameters is easily accomplished by overloading, so
there is no need for the redundant mechanism.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation


这篇关于Java与C ++中的默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆