在创建对象时传递参数以对其如何工作进行分类? [英] Passing parametres while creating an object to class how it works ?

查看:64
本文介绍了在创建对象时传递参数以对其如何工作进行分类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class1 objcls1 = new class1(param1,param2);



见上面的代码?我想知道上面的代码是什么意思?

我怀疑下面是这段代码的含义。



class1包含一个函数名称为class1,参数为param1,param2。





class1 objcls1= new class1 (param1, param2);

see above code ? i want to know what does the above code mean ?
I suspect below is the meaning of this code piece.

class1 contains a function with name class1 and parameters param1, param2.


public class class1 
{
   
    public class1 (string param1, string param2)
    {





我的猜测是真的吗?



Is my guess is true ?

推荐答案

当你使用 new class1(param1,param2)时,你创建一个 class1 对象和<$ c中的代码$ c> public class1(string param1,string param2)调用构造函数。因此,如果你在构造函数中添加一些与这些字符串相关的东西,那么在创建对象时会执行该代码。



简单的例子:

When you use new class1(param1, param2) you create a class1 object and the code in the public class1(string param1, string param2) constructor is called. So if you would put something in your constructor that does something with those strings, then that code is executed when you create the object.

Simple example:
// class:
public class class1
{
    public string aString;
    public string anotherString;
    public class1(string param1, string param2)
    {
        this.aString = param1;
        this.anotherString = param2;
    }
}

// code:
class1 objcls1 = new class1 ("string 1", "string 2");



然后 objcls1.aString string 1 objcls1。 anotherString string 2


此方法是具有两个参数的类的构造函数,用于通过使用给定参数创建对象(类类型)。



有关详细信息,请参阅下一篇文章: C#中构造函数介绍 [ ^ ]
This method is a constructor of the class with two parameters and is used to create an object (of class type) by using the given parameters.

For more details see the next article: An Intro to Constructors in C#[^]


在C#中,有方法与该类相同的名称称为构造函数。它充当...类...实例的构造函数(当你调用new时)。传递给它的任何参数都可以在创建类时获得,并用于初始化实例状态。



示例将是......嗯......你加倍单击包含来自客户端的订单列表的网格。双击打开新表单并传递orderID,如下所示



In C#, method that has the same name as the class is called constructor. It serves as...well...constructor of the class instance (when you call new). Any parameters passsed into it are available at the moment of class creation and serve for initialization of the instance state.

Example would be...hm...you double click on the grid containing list of orders from a client. Double click opens new form and passes in orderID like this

frmOrderDetails items = new frmOrderDetails (dgvOrder.CurrentRow["order_id"])





然后你可以在详细信息表格中保存该ID并从数据库中恢复填写特定订单详细信息所需的一切。



如果这有帮助,请花时间接受解决方案。谢谢。



Then you can in details form save that ID and recover from the database everything you need to fill that particular order details.

If this helps please take time to accept the solution. Thank you.


这篇关于在创建对象时传递参数以对其如何工作进行分类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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