scala 访问修饰符_Scala中的访问修饰符
scala 訪(fǎng)問(wèn)修飾符
Access modifiers are used in order to restrict the usage of a member function to a class or a package. Using access modifiers data hiding takes place which is a very important concept of OOPs.
訪(fǎng)問(wèn)修飾符用于將成員函數(shù)的使用限制為類(lèi)或包。 使用訪(fǎng)問(wèn)修飾符進(jìn)行數(shù)據(jù)隱藏,這是OOP的非常重要的概念。
The access to a class, object or a package can be restricted by the use of three types of access modifiers that are 1) public (accessible to everyone), 2) private (accessible only in the class), and 3) protected (accessible to class and its subclasses).
可以通過(guò)使用三種類(lèi)型的訪(fǎng)問(wèn)修飾符來(lái)限制對(duì)類(lèi),對(duì)象或包的訪(fǎng)問(wèn): 1)公共(所有人都可以訪(fǎng)問(wèn)) , 2)私有(僅在類(lèi)中可以訪(fǎng)問(wèn))和3)受保護(hù)(可以訪(fǎng)問(wèn))類(lèi)及其子類(lèi)) 。
1)公共訪(fǎng)問(wèn)修飾符 (1) Public access modifier )
It is the default type of modifier in Scala. In Scala, if you do not use any access modifier then the member is public. Public members can be accessed from anywhere.
它是Scala中默認(rèn)的修飾符類(lèi)型。 在Scala中,如果不使用任何訪(fǎng)問(wèn)修飾符,則該成員是公共的。 可以從任何地方訪(fǎng)問(wèn)公共成員。
Syntax:
句法:
def function_name(){}or public def fuction_name(){}2)私人訪(fǎng)問(wèn)修飾符 (2) Private access modifier )
In private access, access to the private member is provided only to other members of the class (block). It any call outside the class is treated as an error.
在私有訪(fǎng)問(wèn)中,對(duì)私有成員的訪(fǎng)問(wèn)僅提供給該類(lèi)的其他成員(塊)。 在類(lèi)之外的任何調(diào)用都被視為錯(cuò)誤。
Syntax:
句法:
private def function_name(){}3)受保護(hù)的訪(fǎng)問(wèn)修飾符 (3) Protected access modifier )
In protected access, the availability of the member function is limited to the same class and its subclass. Excess without inheritance is treated as an error.
在受保護(hù)的訪(fǎng)問(wèn)中,成員函數(shù)的可用性?xún)H限于同一類(lèi)及其子類(lèi)。 沒(méi)有繼承的多余部分將被視為錯(cuò)誤。
Syntax:
句法:
protected def function_name(){}Scala示例演示使用公共,私有和受保護(hù)的訪(fǎng)問(wèn)修飾符 (Scala example to demonstrate use of public, private and protected access modifiers)
class school(rlno: Int , sname : String ,sch_no : Int) {//roll no can only be used by school or its subclassesprotected var rollno = rlno; var name = sname;// this variable is only for the classprivate var scholar=sch_no; } class seventh extends school {def dispaly(){// public and private member are used...print("Roll no of " + name + " is " + rollno) } }翻譯自: https://www.includehelp.com/scala/access-modifiers-in-scala.aspx
scala 訪(fǎng)問(wèn)修飾符
總結(jié)
以上是生活随笔為你收集整理的scala 访问修饰符_Scala中的访问修饰符的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 摩尔庄园相机怎么关闭
- 下一篇: MPEG的完整形式是什么?