小编PS *_*yak的帖子

在Java中,为什么可以在同一个包中从类外部访问受保护的成员?

在他的书中,Herbert Schildt在第172页(第3段)中说" 受保护仅在涉及继承时才适用".

在页228中,表9-1显示可以从同一个包中的非子类访问受保护的成员.

以下代码适用于并支持表9-1中的信息.

Class1.java:

package Mypack;
public class Class1
{
    protected pro=1;
    public Class1()
    {
        System.out.println(pro);
    }
}
Run Code Online (Sandbox Code Playgroud)

Class2.java

package Mypack;
class Class2 extends Class1
{
    Class2()
    {
        System.out.println(pro);
    }
}
Run Code Online (Sandbox Code Playgroud)

Class3.java

package Mypack;
class Class3
{
    Class3()
    {
        Class1 class1=new Class1();
        System.out.println(class1.pro);
    }
}
Run Code Online (Sandbox Code Playgroud)

可以从派生类Class2访问变量pro.但是如何从非派生类Class3通过对Class1对象的引用来访问它?它与第172页上的陈述相矛盾.如果是这样,那么在这种情况下,我发现公共和受保护的说明符之间没有区别.

java protected

16
推荐指数
1
解决办法
5084
查看次数

链接列表错误"分段错误"核心被转储

尝试使用Fedora gcc下面的代码,为一个简单的链接列表添加新节点到列表的尾部.编译时没有错误.在执行期间,它显示Segmentation Fault,Core Dumped.在MS Windows上,它正在运行.

#include<stdio.h>
#include<stdlib.h>

struct Node
{
    int data;
    struct Node *next;
};

void insertion(struct Node *);
void display(struct Node *);

int main(void)
{
    struct Node *head;
    head=NULL;
    head->next=NULL;

    int choice, cont;

    do
    {
        printf("1.Insert      2.Display       3.Exit");
        scanf("%d",&choice);

        if(choice==1)
        {
            insertion(head);
        }
        else if(choice==2)
        {
            display(head);
        }
        else if(choice==3)
        {
            exit(0);
        }
        else
        {
            printf("Wrong choice");
        }
        printf("Continue? Press 1 otherwise 0:");
        scanf("%d",&cont);
    }while(cont==1);

    return 0;
}

void insertion(struct Node *start)
{
    int data;
    struct Node *temp=NULL; …
Run Code Online (Sandbox Code Playgroud)

c pointers linked-list list singly-linked-list

0
推荐指数
1
解决办法
1816
查看次数

标签 统计

c ×1

java ×1

linked-list ×1

list ×1

pointers ×1

protected ×1

singly-linked-list ×1