如果创建从接口继承的类的对象,我可以调用什么函数?

问题描述 投票:-3回答:3
public Interface IPerson 
{   
    void SetAge(int age);
}

public class Man : IPerson
{
    public void SetAge(int age) {}
    public int GetAge() {return 20; }
}

IPerson p = new Man();

我们可以用p调用哪些函数?既然已经创建了man的对象,那么它将调用这两个函数?

c# .net c#-4.0
3个回答
0
投票

你只能调用SetAge,因为这是接口IPerson中定义的唯一方法。因此,如果您声明了一个可以保存对实现此接口的对象的引用的变量,那么我们在此时期望的唯一方法是SetAge


0
投票

您只能使用接口引用调用SetAge(int age)方法。没有其他方法可以访问。


0
投票

由于对象'p'的类型为'IPerson',因此您只能调用IPerson下的方法。这里'SetAge'是Interface IPerson中定义的唯一方法。

© www.soinside.com 2019 - 2024. All rights reserved.