简体   繁体   English

为什么最终类WebView在JavaFX中包含受保护的方法getChildren()?

[英]Why does final class WebView contain protected method getChildren() in JavaFX?

WebView class is final. WebView是最终的。 It contains method getChildren() , which is protected. 它包含受保护的方法getChildren()

How to use it then? 那怎么用呢?

UPDATE UPDATE

The question is not about protected adn/or final keywords, but about why this is done this way? 问题不是关于protected adn /或final关键字,而是关于为什么要这样做吗? What is the reason to have protected members in final classes? 在最终课程中拥有受保护成员的原因是什么?

protected means "package and inheritance access" so classes in the same package can access this method. protected表示“程序包和继承访问”,因此同一程序包中的类可以访问此方法。

EDIT: 编辑:

answer to your edit: it's because designers of this class wanted this method to be available ONLY to classes within the same package and no other classes (thus you cannot extend it because their made it final) 对您的编辑的答案:这是因为此类的设计者希望此方法仅可用于同一包中的类,而不能用于其他任何类(因此,您无法对其进行扩展,因为他们将其定型)

When it declared as protected, Still the classes who instantiate the WebView class in the same package can use that method. 当声明为受保护时,在同一包中实例化WebView类的类仍可以使用该方法。

You might think that it cannot be extended as it is final , there is no way to access it outside of the class. 您可能会认为它无法扩展,因为它是final ,因此无法在类外部访问它。 Yes, that is true but with in the package, they can. 是的,这是事实,但是可以将其放在包装中。

Modifier    Class   Package Subclass    World
---------------------------------------------
protected   Y      **Y**        Y           N

As you know, final classes cannot be extended. 如您所知,最终课程无法扩展。 A final class can not be subclassed. 最终类不能被子类化。

protected Modifier - Accessed by other classes in the: protected Modifier-由以下类别中的其他类访问:

  • same package 同一包
  • any subclasses of the class in which they are referred (ie same package or different package ). 引用它们的类的任何子类(即, 相同的包不同的包 )。

You can't extend it because it's final. 您不能扩展它,因为它是最终的。

  • Create and instance of Webview class and call newWebViewInstance.getChildren() . 创建和创建Webview类的实例,然后调用newWebViewInstance.getChildren()
  • You can create a package with the same package of Webview, but anybody with right sense of Java programming language wouldn't do it. 您可以使用相同的Webview包创建一个包,但是任何具有Java编程语言正确知识的人都不会这样做。

Update 更新

I don't like to make member/methods of a final class protected . 我不喜欢对最后一堂课的成员/方法进行保护。 IMHO that is bad programming practise unless in this case. 恕我直言,这是不好的编程习惯,除非在这种情况下。

Why was it made protected? 为什么要对其进行保护?

WebView extends javafx.scene.Parent

And getAllChildren() is an method of javafx.scene.Parent which is of protected scope. getAllChildren()javafx.scene.Parent的方法,属于受保护范围。

protected javafx.collections.ObservableList<javafx.scene.Node> getChildren()

As we now know Webview is a subclass of javafx.scene.Parent and overrides getChildren() . 众所周知,Webview是javafx.scene.Parent的子类,并且重写getChildren() If it changes the scope (default being stricter than protected) it breaks the overriding contract. 如果更改范围(默认范围比受保护范围更严格),则会破坏覆盖合同。

Hope it's clear now. 希望现在清楚了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM