如何在派生的内部类中编写构造函数?

时间:2015-08-28 09:52:54

标签: java generics inheritance

以下是基类相关代码public class Folder { public Guid Id { get; set; } public string Title { get; set; } [InverseProperty("MyFoder")] public virtual FolderSettings FolderSettings { get; set; } private ICollection<FolderSettings> _folders { get; set; } public ICollection<FolderSettings> Folders { get { return _folders ?? (_folders = new HashSet<FolderSettings>()); } set { _folders = value; } } private ICollection<File> _files { get; set; } public ICollection<File> Files { get { return _files ?? (_files = new HashSet<File>()); } set { _files = value; } } } public class File { public Guid Id { get; set; } public string Name { get; set; } } public class FolderSettings { [Key, ForeignKey("Folder")] public Guid FolderId { get; set; } public Folder Folder { get; set; } public virtual MVAppGroup AppGroup { get; set; } [ForeignKey("Group")] public Guid? GroupId { get; set; } public virtual MVAppGroup Group { get; set; } } public class MVAppGroup { public Guid Id { get; set; } public string Name { get; set; } } public class A : DbContext { public DbSet<Folder> Folders { get; set; } public DbSet<File> Files { get; set; } public DbSet<FolderSettings> FolderSettings { get; set; } public DbSet<MVAppGroup> MVAppGroups { get; set; } } class Program { static void Main(string[] args) { using (var a = new A()) { a.Folders.ToList(); } } } DblyLinkListhere的完整代码,

DblyLinkList

以下是同一个软件包中引入的类package JavaCollections.list; public class DblyLinkList<T> implements Iterable<T>{ protected DListNode<T> sentinel; protected int size; class DListNode<T> { private T item; private DListNode<T> prev; private DListNode<T> next; DListNode(T item, DListNode<T> p, DListNode<T> n) { this.item = item; this.prev = p; this.next = n; } } DListNode<T> newNode(T item, DListNode<T> prev, DListNode<T> next) { return new DListNode(item, prev, next); } public DblyLinkList() { this.sentinel = this.newNode(null, this.sentinel, this.sentinel); } ......... } ,它为每个节点创建将LockableList字段设置为lock。要删除false节点,LockableNode方法将检查remove()字段的值。

在致电lock时,错误super(item, p, n);

如何在下面派生的类The constructor DblyLinkList<T>.DListNode<T>(T, DblyLinkList<T>.DListNode<T>, DblyLinkList<T>.DListNode<T>) is undefined中编写一个可以被重写方法LockableList.LockableNode调用的构造函数?

newNode

注意:这是继承和封装的练习

2 个答案:

答案 0 :(得分:0)

您没有向contstructor提供acces level,但默认情况下:

Modifier    Class   Package     Subclass    World
no modifier     Y         Y            N        N

这称为 package-private ,即使内部子类位于同一个包中,也无法访问此构造函数。发生这种情况是因为它是一个内部类,所以即使子类可以访问super(),内部类也不会。

所以:

public class LockableList<T> extends DblyLinkList<T> {

    LockableList() {
        super();  //this work
    }

    class LockableNode<T> extends DListNode<T>{
        LockableNode(T item, DblyLinkList<T>.DListNode<T> p, DblyLinkList<T>.DListNode<T> n) {
        super(item, p, n); //this does not work
    }
}

如果声明DblyLinkList public或protected的构造函数可以从内部类访问。

protected DListNode<T> newNode(T item, DListNode<T> prev, DListNode<T> next) {
    return new DListNode(item, prev, next);
}

Modifier    Class   Package     Subclass    World
public          Y         Y            Y        Y
protected       Y         Y            Y        N

另请this question查看有关Java

中访问级别的说明

答案 1 :(得分:0)

试试这个:

LockableNode(T item, DListNode<T> p,
            DListNode<T> n) {
        super(item, p, n);
        this.lock = false;
}