受保护和受保护的区别[this] [英] Difference between protected and protected[this]

查看:81
本文介绍了受保护和受保护的区别[this]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

class Base{
    protected val alpha ="Alpha";
    protected def sayHello = "Hello";
}
class Derived extends Base{
        val base = new Base;
        def hello = println(this.alpha +" "+this.sayHello) ;
       // def hello = println(base.alpha +" "+base.sayHello) ; // don't compile
    }

    object MyObj extends App{
            val x=new Derived;
            x.hello;
        }

Base 类中,如果我用this 标记protected,则代码按预期工作;如果我不用 this 标记它,一切也会按预期进行.

In class Base, if I label protected with this, the code works as expected; if I don't label it with this, everything works as expected too.

protectedprotected[this] 在 Scala 中是等价的吗?如果是,为什么 Scala 会同时提供两者?如果不是,它们有何不同?

Are protected and protected[this] equivalent in Scala? If yes, why would Scala provide both? If not, how do they differ?

推荐答案

Scala protected 及其兄弟 protected[this] , protected[pkg] 有点压倒性,但是我发现通过使用 protectedJava 哲学很容易记住解决方案.1 受保护的成员如何在 Java 中可见

Scala protected and its siblings protected[this] , protected[pkg] is somewhat overwhelming ,However I found easy to remember solution by using the Java philosophy of protected . 1st How protected member is visible to in Java

  1. 它们对子类可见(子类可能在同一个包或其他包中)
  2. 它们对与指定受保护成员的类位于同一包中的任何类都可见.

显然它们对类本身可见.

但是Scala对它们对子类可见的方式有一些限制.默认情况下,它们只对子类可见.它们对声明类的包不可见.但是存在两种情况,它们如何在子类中可见.

But Scala has some restriction on how they are visible to subclass.By default,They are visible to subclass only . They are not visible to package in which class is declared .However two case exist how they are visible in subclass .

如果受保护成员没有限定(普通保护),那么它对于另一个声明类到声明类以及this到类中都是可见的和子类例如

if protected member is not qualified(plain protected) then it is visible with another instances of declaring class into declaring class as well as with this into class and subclass e.g

class Base{
    protected val alpha ="Alpha";
    protected def sayHello = "Hello";
}
class Derived extends Base{
        def hello = println((new Derived()).sayHello) ;
        def hello2 = println(this.sayHello);

}

如果受保护的成员是用this限定的.它只能在类和子类中用this访问,不能被其他声明的实例访问类或子类,例如

if protected member is qualified with this .It is only accessible with this in class and subclass ,It can't be accessed by other instances of declaring class or subclass e.g.

class Base{
    protected val alpha ="Alpha";
    protected[this] def sayHello = "Hello";
    def foo = Console println(new Base().sayHello) // won't compile
    def bar = Console println(this.sayHello)
}
class Derived extends Base{
        def hello = println(this.sayHello) ;
        //def hello2 = println((new Derived() .sayHello) // won't compile
}

由于 Scala 默认不支持受保护成员的包级别访问.但是如果您想使其在包级别可用,则需要明确指定包,例如受保护[pkg].现在,如果在 pkg 或以下声明的类中访问该受保护的成员,则在声明类/子类实例时可见.

Since Scala do not support the package level access of protected member by default .but if you want to make it available at package level then you need to specify package explicitly e.g. protected[pkg]. Now this protected member is visible with declaring class/Subclass instances if they are accessed in classes declared in pkg or below.

例如

package com.test.alpha{
class Base{
    protected val alpha ="Alpha";
    protected[test] def sayHello = "Hello"; // if you remove [test] it won't compile
}

class Derived extends Base{
    val base = new Base
        def hello = println(base.sayHello) ;
}}

这就是人们如何记住 Scala protected

This is how one can remember Scala protected

这篇关于受保护和受保护的区别[this]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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