[英]How do I use a class as a template in C#?
I'm not sure if I'm asking that correctly, so bear with me. 我不确定我是否正确提出了要求,请耐心等待。 I'm currently attempting to program a neural network.
我目前正在尝试对神经网络进行编程。 Below is some sample code.
下面是一些示例代码。
using System;
namespace Network
{
class BaseNeuron
{
public static void Main (string[] args)
{
public int Weight;
public void sendPulse()
{
}
public void receivePulse()
{
}
}
}
}
So basically, I want to be able to create multiple instances of the BaseNeuron class, with each "neuron" tied to other "neurons" to make a Neuron Network. 因此,基本上,我希望能够创建BaseNeuron类的多个实例,并将每个“神经元”绑定到其他“神经元”以构成神经元网络。 Is there any way to do this without having to reiterate this snippet of code for each neuron?
有什么方法可以执行而不必为每个神经元重复此代码片段吗?
First of all, there are some errors in the code you provided: 首先,您提供的代码中存在一些错误:
class BaseNeuron
{
public static void Main (string[] args)
{
public int Weight;
public void sendPulse()
{
}
public void receivePulse()
{
}
}
}
It should be: 它应该是:
class BaseNeuron
{
//class field
public int Weight;
//class method
public void SendPulse(/*Parameters ...*/)
{
//Do stuff
}
//class method
public void ReceivePulse(/*Parameters ...*/)
{
//Do stuff
}
}
Notice that the Main
is gone. 请注意,
Main
已消失。 Classes normally don't have a Main
method, which is usually just the entry point of the application (the first function to get executed when the program runs). 类通常没有
Main
方法,该方法通常只是应用程序的入口点 (程序运行时要执行的第一个函数)。
Basically, each class is a template for objects , which means you can create new instances of the class by using the new
keyword, like this: 基本上,每个类都是对象的模板 ,这意味着您可以使用
new
关键字创建该类的新实例 ,如下所示:
//creating two seperate neurons
neuron1 = new BaseNeuron();
neuron2 = new BaseNeuron();
//Notice that each SendPulse is called on separate objects!
neuron1.SendPulse();
neuron2.SendPulse();
I suggest you read a little bit about class (maybe here ) to get a better understanding how this works. 我建议您阅读一些有关课堂的知识(也许在这里 ),以更好地了解其工作原理。
Alright, so now we have our class figured out. 好了,现在我们上课了。 You said we wanted to make a network which consists of several neurons, which are instances of the class above.
您说过我们想建立一个由几个神经元组成的网络,这些神经元是上述类的实例。 So, what you will probably want to do is:
因此,您可能想要做的是:
class Program
{
public void Main(string[] args)
{
//the size of the network
int size = 5;
//create array of Neurons
BaseNeuron[] network = new BaseNeuron[size];
for(int i = 0; i < network.length; i++)
{
//actually create a new nueron (instantiate them)
network[i] = new BaseNeuron();
//link up neurons together, etc
}
}
}
Of course, the implementation depends of the internal implementation of the BaseNeuron
class. 当然,实现取决于
BaseNeuron
类的内部实现。 You also might need to create a NeuralNetwork
class to contain the above code and related functions. 您可能还需要创建一个
NeuralNetwork
类来包含上述代码和相关功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.