Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建引用类的方法_C# - Fatal编程技术网

C# 创建引用类的方法

C# 创建引用类的方法,c#,C#,请原谅我这个奇怪的标题,不过我在正确命名上有很大的问题 我在LoginPage类中遇到了一个问题: public class LoginPage : PageBase { private static readonly By IsLoadedLocator = By.Id("logo"); public LoginPage(IWebDriver webDriver) : base(webDriver) { if (!IsLoaded()) throw n

请原谅我这个奇怪的标题,不过我在正确命名上有很大的问题

我在
LoginPage
类中遇到了一个问题:

public class LoginPage : PageBase
{
    private static readonly By IsLoadedLocator = By.Id("logo");

    public LoginPage(IWebDriver webDriver) : base(webDriver)
    {
        if (!IsLoaded()) throw new NoSuchElementException("Login page is not loaded");
    }

    // (1) This method
    public LoginFormComponent GetLoginFormComponent()
    {
        var component = new LoginFormComponent(this);
        return component.IsLoaded() ? component : null;
    }

    public override By GetIsLoadedLocator()
    {
        return IsLoadedLocator;
    }
}
标记为(1)的方法。这里到底发生了什么,
LoginFormComponent
是一个类。我们基于不同的类创建方法,这样做的目的是什么

第二个问题

var loginPage = new LoginPage(driver);
当loginPage是变量时,我们正在初始化一个对象,但为什么不以正常方式进行初始化,如:

LoginPage loginPage = new LoginPage(driver)
第一个问题:

public LoginFormComponent GetLoginFormComponent()
{
   // Create an instance of LoginFormComponent
   var component = new LoginFormComponent(this);
   // Return component if IsLoaded == true or null if it's false.
   return component.IsLoaded() ? component : null;
}
public LoginFormComponent GetLoginFormComponent()
{
    var component = new LoginFormComponent(this);
    return component.IsLoaded() ? component : null;
}
第二个问题:

public LoginFormComponent GetLoginFormComponent()
{
   // Create an instance of LoginFormComponent
   var component = new LoginFormComponent(this);
   // Return component if IsLoaded == true or null if it's false.
   return component.IsLoaded() ? component : null;
}
public LoginFormComponent GetLoginFormComponent()
{
    var component = new LoginFormComponent(this);
    return component.IsLoaded() ? component : null;
}
关键字
var
类似于变量类型的占位符,编译代码后,编译器将“幕后”确定并用适当的数据类型替换var


var
不能与
dynamic
混用,因为它们是不同的。

要回答您的第一个问题:

public LoginFormComponent GetLoginFormComponent()
{
   // Create an instance of LoginFormComponent
   var component = new LoginFormComponent(this);
   // Return component if IsLoaded == true or null if it's false.
   return component.IsLoaded() ? component : null;
}
public LoginFormComponent GetLoginFormComponent()
{
    var component = new LoginFormComponent(this);
    return component.IsLoaded() ? component : null;
}
GetLoginFormComponent()
是一个返回属于
LoginFormComponent
的内容的方法。该方法的第一行是创建一个新的
LoginFormComponent
实例,并传递您当前所在的
LoginPage
实例(使用
this
)。第二行是使用,它基本上表示如果第一个表达式为true,则返回
后面的项,如果表达式为false,则返回
后面的项。它也可以写成:

if(component.IsLoaded())
{
    return component;
}
else
{
    return null;
}
至于第二个问题,
var
只是一种防止程序员编写冗余代码的语法糖。查看声明变量并实例化它的传统方式:

LoginPage loginPage = new LoginPage(driver);
这有点多余,对吧?为什么我们需要键入
LoginPage
两次?使用
var
时,编译器将隐式确定类型(以避免编写冗余代码):


请记住,
var
与JavaScript
var
不同
var
不是一个松散类型的对象,它只是程序员的简写。

var-loginPage=new-loginPage(…)和
loginPage-loginPage=new-loginPage(…)
之间没有区别,
var
只是一个快捷方式,上面写着“从我放在右边的东西中找出答案”。这称为隐式声明,当您使用VAR时,变量将在赋值时获取其类型。当您使用var时,应指定它,例如var i;不可能,但var i=0;好的,请试着每个“问题”问一个问题。为了理解为什么,请阅读你的第二个问题很容易通过谷歌搜索,我们很乐意帮助你,但在来这里之前,你应该在你的研究上更努力一点。我不认为将实际数据类型放在声明前面是“多余的代码”。注:我没有否决投票,但是你的答案与我的答案没有区别,只是教你如何使用三元运算符。@Adriani6在
LoginPage LoginPage=new LoginPage(driver)的情况下是的。不需要键入两次,因为只要查看初始化,类型就很明显了。但是,像这样的场景:
var someObj=someService.GetObject()我认为使用显式类型是首选的,因为类型不是很明显。另外,你的回答很好,我只想谈谈
这个
和三元运算符,因为我不确定OP的哪一部分被混淆了。非常感谢你的回答,先生。。。您说过这是一个从LoginFormComponent类返回某些内容的方法。我真的不明白,我们将类包含在不同类的方法中,让该类创建/初始化对象???@Ixi11我对你的评论有点困惑。该方法只是返回
LoginFormComponent
的一个实例。C#是一种面向对象的语言,您将在对象/类方面做很多工作,其中包括返回从其他类创建的对象。这在C#@Ixi11中很常见,没关系,甚至很难知道在刚开始的时候如何提问。我认为,如果您阅读一些关于C#中的类/对象的教程,会有很大帮助。可能会帮助你更好地理解这方面