我怎样才能给我的课程一个特殊的输入? [英] How can I give my class a special input?

查看:65
本文介绍了我怎样才能给我的课程一个特殊的输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以给我的班级一个特定的输入,如果可以,我怎么能给我的班级输入如下:

Can I give my class a specific input and if I can, how can I give my class an input like:

Foo foo = new Foo()
foo = 1.A;

推荐答案

如果您的目标是在创建类的实例创建时初始化类的字段或属性,为什么不使用构造函数或多个构造函数(如果需要)。
If your goal here is that to initialize a Field, or Property, of a Class at the time an instance of the Class is created, why not use a constructor, or multiple constructors, if needed.
public class Foo
{
    // public Field of a 'foo
    public int FooID;

    // public Property of a 'foo
    // only the class can set the value of FooString1
    public string FooString1 { get; private set; }

    // public Property of a 'foo
    // the value of FooString2 can be set from within
    // the class or from outside the class
    // since there's no method in the class that sets
    // its value, then it must be set outside the class
    public string FooString2 { get; set; }

    // parameterless constructor #1:
    public Foo()
    {
    }

    // constructor #2:
    public Foo(int fooId, string fooStr)
    {
        FooID = fooId;
        FooString1 = fooStr;
    }
}

测试:

Foo myFoo1 = new Foo();

Foo myFoo2 = new Foo(111, "Foo Two");

// set the value of FooString2 in myFoo2
myFoo2.FooString2 = "String Two of Foo Two";

请注意,因为没有调用:base ()在Foo中,无参数构造函数可以省略...如果没有使用。

Note that because no calls are made to :base() in Foo, the parameterless constructor can be omitted ... if not used.


你可以这样试试





you can try like this


{

           Foo foo = new Foo(); //creating a new instance for Foo class

           Foo fooTemp = GetFoo(); // Getting the Foo object from a method.

           foo = fooTemp;  // Assigning the new Foo object to foo


       }

       public   Foo GetFoo() // Method to get Foo object
       {
               // your coding here
           return new Foo();
       }


嗨。通常,当您的类需要任何外部数据进行工作时,您可以向类添加属性。也就是说,属性是提供读取,写入或计算私有字段值的灵活机制的成员。例如:

Hi. generally you can add property to your class when your class needs any external data for working.also properties are members that provide a flexible mechanism to read, write, or compute the values of private fields. for example :
#region Properties

private string _FirstName;
public string FirstName
{
    get { return _FirstName; }
    set { _FirstName = value; }
}

private string _LastName;
public string LastName
{
    get { return _LastName; }
    set { _LastName = value; }
}

#endregion

#region Constructor

public Person(string firstName, string lastName)
{
    this.FirstName = firstName;
    this.LastName = lastName;
}

#endregion

#region Methods

public string Greeting()
{
    string message = string.Format("Hi {0} {1}", this.FirstName, this.LastName);
    return message;
}

#endregion





现在您可以创建Person类的实例,如下所示:



now you can create instance of Person class like following:

Person myPer = new Person();
myPer.FirstName = "ali";
myPer.Greeting();


这篇关于我怎样才能给我的课程一个特殊的输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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