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

问题描述

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

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;

推荐答案

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.


{

           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();
       }


#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();


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

10-24 14:03