[英]What is the equivalent of this syntax in Java?
Just a quick question about how to convert a certain line of code between c++ and Java. 关于如何在c ++和Java之间转换特定代码行的一个简短问题。 I've been learning about Neural Networks and I've been starting to write my own in the language I have the most experience with, Java.
我一直在学习有关神经网络的知识,并且已经开始用我最熟悉的Java语言来编写自己的语言。 It's been pretty straightforward to translate the code from C++ to Java so far, however there's one little issue I've ran into.
到目前为止,将代码从C ++转换为Java一直很简单,但是我遇到了一个小问题。 I'm puzzled by how to translate this specific line of code into a Java equivalent, and I can't find anything specific to this issue through searching.
我对如何将特定的代码行转换为等效的Java感到困惑,并且无法通过搜索找到与此问题相关的任何内容。
The original code is: 原始代码是:
Struct SNeuron {
//the number of inputs into the neuron
int m_NumInputs;
//the weights for each input
vector<double> m_vecWeight;
//ctor
SNeuron(int NumInputs);
};
and my code is: 我的代码是:
public class SNeuron {
public int m_NumInputs; // the number of inputs into the neuron
public ArrayList<Double> m_vecWeight = new ArrayList<Double>(); // the weights for each input
// ctor
My question is, how do I convert: 我的问题是,如何转换:
SNeuron(int NumInputs);
into its Java equivalent? 变成它的Java等效语言? From what i've read, Structs don't seem to be a feature Java uses, so i'm just struggling a bit to understand what exactly that line of code actually does in the context it's used.
从我读过的内容来看,Structs似乎不是Java所使用的功能,因此我只是在努力地了解一下该代码行在所使用的上下文中实际上是做什么的。
public class SNeuron
{
// the number of inputs into the neuron
public int m_NumInputs;
// the weights for each input
public List<Double> m_vecWeight = new ArrayList<Double>();
// ctor
SNeuron(int NumInputs) {
m_NumInputs = NumInputs;
}
Given the comments in code, I'm pretty sure the equivalent is: 给定代码中的注释,我很确定等同于:
public class SNeuron {
public final double[] weights;
public SNeuron(int numInputs) {
weights = new double[numInputs];
}
}
You don't really want to use a List<Double>
, it will be much slower and take much more memory - each double in such a list becomes a full-fledged object with all associated overheads. 您实际上并不想使用
List<Double>
,它会慢得多并且占用更多的内存-这样的列表中的每个double都会成为具有所有相关开销的成熟对象。
In C++, SNeuron(int NumInputs);
在C ++中,
SNeuron(int NumInputs);
is the declaration of a constructor taking an int
, and this is contained within the class declaration. 是采用
int
的构造函数的声明,它包含在类声明中。
You don't do that in Java - effectively all constructors, and all functions for that matter, are inlined in the class declaration. 您不需要在Java中执行此操作-实际上,所有构造函数以及与此相关的所有函数都内联到类声明中。 In other words
换一种说法
SNeuron(int NumInputs); // within the class declaration
SNeuron::SNeuron(int NumInputs) : m_NumInputs(NumInputs){} // In a translation unit
maps to 映射到
SNeuron(int NumInputs) {
m_NumInputs = NumInputs;
}
but note that using m_
for Java fields is idiosyncratic. 但请注意,对Java 字段使用
m_
是特质的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.