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的对象,那么它将调用这两个函数?
你只能调用SetAge
,因为这是接口IPerson
中定义的唯一方法。因此,如果您声明了一个可以保存对实现此接口的对象的引用的变量,那么我们在此时期望的唯一方法是SetAge
。
您只能使用接口引用调用SetAge(int age)
方法。没有其他方法可以访问。
由于对象'p'的类型为'IPerson',因此您只能调用IPerson下的方法。这里'SetAge'是Interface IPerson中定义的唯一方法。