声明函数时初始化vars(Java VS C ++) [英] initalizing vars when declaring functions (Java VS C++)
本文介绍了声明函数时初始化vars(Java VS C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我是java的新手,我想弄清楚在声明一个函数时是否可以为变量赋值.
这是一些解释 (Correct me if i''m wrong)
在C ++中,我们可以这样写:
Hi all,
I''m new to java and i''m trying to figure out if we could assign values to variables when declaring a function.
Here is some explanation (Correct me if i''m wrong)
in C++ We could write:
class Humain
{
public:
String nom;
int age;
public:
Humain(int age, String name): age(age), name(name){}
}
我想知道我们是否可以像这样用Java编写.
PS:我浏览了许多网站,并且所有网站都使用了以下内容
I''m wondering if we could write in java like that.
PS: i looked in many web sites and all used as below
Humain(int age, String name){
this.age = age;
this. name = name;
}
谢谢.
Thank you.
推荐答案
与C ++不同,您可以在类中指定初始值,但不能使用初始化程序列表.我不知道该怎么做才能提高性能,但我认为编译器会删除冗余的初始化.
干杯,
灰
PS:您是否有Arnold,Gosling和Holmes撰写的"Java编程语言"的副本?如果不抓取副本,它将得到类似这样的答案. (顺便说一句,这就是我得到这个问题的答案).
Unlike C++ you can specify initial values in your class but you can''t use an initialiser list. Quite what that does for performance I don''t know but I assume the compiler removes redundant initialisation.
Cheers,
Ash
PS: Have you got a copy of "The Java Programming Language" by Arnold, Gosling and Holmes? If not grab a copy, it''s got answers like this in it. (That''s where I got the answer to this question BTW).
这篇关于声明函数时初始化vars(Java VS C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文