2016-03-22 62 views
0
public class Employee : IEmployee 
{ 
    private String name; 
    private String department; 
    private int salary; 
    private List<IEmployee> subordinates; 

    public void addEmployee(String name, String department, int salary) 
    { 
     this.name = name; 
     this.department = department; 
     this.salary = salary; 
    } 

    public void addSubordinate(IEmployee e) 
    { 
     subordinates.add(e); 
    } 

interface IEmployee 
{ 
    void addEmployee(String name, String department, int salary); 
    String getEmployeeInfo(); 
    void addSubordinate(IEmployee e); 
} 

public void If_We_Add_Alan_As_Johns_Subordinate_We_SHould_get_that() 
{ 
     ObjectFactory.Configure(x => 
     { 
      x.For<IEmployee>().Use<Employee>(); 
     }); 
     var DairyManager = ObjectFactory.GetInstance<IEmployee>(); 
     var Cashier = ObjectFactory.GetInstance<IEmployee>(); 

     Cashier.addEmployee("Alan", "cashier", 1000); 
     DairyManager.addEmployee("John", "CEO", 10000); 
     DairyManager.addSubordinate(Cashier); 

錯誤說:我得到一個「可訪問性不一致」錯誤

可訪問性不一致:參數類型IEmployee比方法Employee.addSubordinate(Supermarket.IEmployee)不太容易接近...

回答

1

你只需要添加一個無障礙修飾符到您的界面:

public interface IEmployee 

T他的意志會糾正錯誤。

2

您應該將接口標記爲公共。

public interface IEmployee 

由於Employee訪問修飾符是公共同樣要保持的interfacesEmployee工具。