类的新关键字 [英] new keyword of a class

查看:51
本文介绍了类的新关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;

interface IPerl
{
    void Read();
}

class Test : IPerl
{
    public void Read()
    {
	Console.WriteLine("Read");
    }
}

class Program
{
    static void Main()
    {
	IPerl perl = new Test(); // Create instance.
	perl.Read(); // Call method on interface.
    }
}


----------
在上面的代码行中......


----------
in above code the line ......

IPerl perl = new Test(); // Create instance.


这是什么意思

我只需在IPerl中创建一个本地变量即可访问接口中的Read方法,如下所示


what does this mean

I can access the Read method in interface just by creating a local variable for IPerl as below

Iperl perl;
perl.Read();


那么新关键字是什么意思或其作用

通常我们会使用以下新关键字


so what does new keyword mean or what does it do

normally we use new keyword like below

Class1 MyClass  = new Class1();


但是这里


but here

IPerl perl = new Test();    // only Test() is class name , but on other side Iperl perl which is given which is not a class but interface.


可以解释一下新关键字的作用.


can some explain what new keyword does.

推荐答案

IPerl perl = new Test();



一样


Does just the same as

Class1 MyClass = new Class1();


但是,您为新实例分配的变量可以包含实现IPerl接口的任何类,而不仅仅是特定的Class1类型.

如果您从Class1派生一个类:


But the variable you assign the new instance to can hold any class which implements the IPerl interface, instead of just the specific Class1 type.

If you derived a class from Class1:

public class Class2 : Class1 {}


您可以高兴地做同样的事情:


You could happily do the same thing:

Class1 myClassInstance = new Class2()


对类的引用可以包含从指定类型派生的任何类.


A reference to a class can contain any class derived from the specified type.


您似乎在这方面是个新手,因此您最好阅读一本有关OOP的好书.从头开始解释不是那么容易,但是我会尽力的.

通过继承,我们面对的是运行时类型编译时类型.首先,让我们来看一下没有接口的内容:

It looks like you are very new in such things, so you better need to read a good book on OOP. It''s not so easy to explain from scratch, but I''ll try.

With inheritance, we face such thing as run-time type vs. compile-time type. First, let''s look at it without interfaces:

class Base {
   internal A() { } 
}

class Derived : Base {
   internal B() { }
}

//...
Base instance = new Derived();

//can call:
instance.A();
//cannot call:
instance.B(); // but if you could, it would work, because actually during run time Base instance is Derived

//but work around is the typical abuse of OOP:
((Derived)instance).B(); // will certainly work in this simple example
//but it depends on previous run time, could cause "invalid typecast" exception



instance是什么类型?编译时类型为Base;这就是编译器如何看待它.运行时类型为Derived,可以在运行时使用运算符isas进行检查(在C#参考中自行查找).

现在,界面.要实现的接口具有基类列表的语法,并且实际上,接口是实现类继承"的,因为它是抽象类,在许多方面与接口相似.
该接口只能是编译时类型.在运行时,您可以使对象具有接口编译时类型,但是它们的运行时类型始终是实现该接口的某些类或结构.接口是某种类型,我可以这样说,它总是抽象的".就您的问题而言,情况与基类和派生类相同.

—SA



What is the type of instance? Compile-time type is Base; this is how compiler sees it. Run-time type is Derived, could be checked during run-time using operators is or as (find them by yourself in C# reference).

Now, interfaces. The interface to be implemented has the syntax of base class list, and indeed, interface is "inherited" by the implementing class as it was an abstract class, which is similar to interfaces in many aspects.
The interface can only be a compile-time type. During runtime, you can have objects which has the interface compile-time types, but their run-time type is always some class or structure implementing this interface. Interface is some type which is, it I may say so, "always abstract". In the context of your question, the situation is the same as with base and derived class.

—SA


这篇关于类的新关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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