为什么我不能访问受保护的java方法,即使我已经扩展了类? [英] why can't i access protected java method even thought i've extended the class?

查看:108
本文介绍了为什么我不能访问受保护的java方法,即使我已经扩展了类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是受保护方法的文档:

Here's the documentation for the protected method:

/** Converts jmusic score data into a MIDI Sequence */
protected  javax.sound.midi.Sequence scoreToSeq(Score score)

我做了这个小班扩展scoreToSeq方法来自的类:

And I made this little class to extend the class that scoreToSeq method comes from:

public class MidiSequence extends MidiSynth{

    public Sequence getSequence(Score score){
        MidiSynth synth = new MidiSynth();
        Sequence sequence = null;
        try
        {
                    // Here I get the error saying that the method has
                    // protected access in MidiSynth
            sequence = synth.scoreToSeq(score);

        }
        catch (InvalidMidiDataException e)
        {
            /*
             *  In case of an exception, we dump the exception
             *  including the stack trace to the console.
             *  Then, we exit the program.
             */
            e.printStackTrace();
            System.exit(1);
        }

        return sequence;

    }
}


推荐答案

(编辑: theycallmemorty的回答提供了在你的案例中避免这个问题的实用建议。这个答案给出了为什么你必须遵循这个建议的原因,即为什么语言是这样设计的。)

( theycallmemorty's answer gives the practical advice to avoiding this problem in your case. This answer gives the reasons for why you have to follow that advice, i.e. why the language has been designed that way.)

您只能访问与访问代码(或子类)属于同一类型的另一个对象的受保护成员 - 即使成员已声明在超类型中。

You can only access a protected member of another object which is of the same type as the accessing code (or a subclass) - even though the member is declared in a supertype.

来自 Java语言规范,第6.6.2节


设C为声明
保护成员m的类。访问
仅允许在
子类S的主体内。另外,如果Id
表示实例字段或实例
方法,则:

Let C be the class in which a protected member m is declared. Access is permitted only within the body of a subclass S of C. In addition, if Id denotes an instance field or instance method, then:


  • 如果访问是通过限定名称Q.Id,其中Q是ExpressionName,
    则允许访问,如果和
    仅当表达式Q
    的类型是S或S的子类时。

  • 如果访问是通过字段访问表达式E.Id,其中E是主
    表达式,或者通过方法调用
    表达式E.Id(...),其中E是
    主表达式,当且仅当类型允许时才允许访问
    E
    是S或S的子类。

这是为了允许键入以访问与其自己的继承树相关的成员,而不会破坏其他类的封装。例如,假设我们有:

This is to allow a type to access members relevant to its own inheritance tree, without defeating encapsulation of other classes. For example, suppose we have:

     A
    / \
   B   Other
  /
 C

和A声明受保护的成员 x 。如果没有规则按照它的方式工作,你可以通过在其他中添加一个成员来实现封装:

and A declared a protected member x. Without the rule working the way it does, you could get round encapsulation by putting a member in Other:

public int getX(A a)
{
    return a.x;
}

并且只是调用传入 B的实例 C - 该成员将有效地公开,因为您可以通过引入另一个类来解决它...不是一个好主意。根据当前规则,您必须继承 B C - 您可能无法在第一名。

and just calling that passing in an instance of B or C - the member would effectively become public, because you could always work around it by introducing another class... not a good idea. With the current rule, you'd have to subclass B or C - which you may not be able to in the first place.

这篇关于为什么我不能访问受保护的java方法,即使我已经扩展了类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆