Why is the bottom layer of public or private in Scala?

Students who have learned about Scala should know that Scala cancels the use of many keywords, such as public, static, etc; But this also brings us some puzzles. This article will answer one of them: why is the bottom layer of public or private in scala?

  • The attribute in the class is expressed as "public" without a scope qualifier
    We may have heard the above paragraph in the process of learning Scala, such as the following code:
object TestClass {
  def main(args: Array[String]): Unit = {
    val instance = new C_0
    println(instance.name)  // hello
  }
}

class C_0() {
  val name: String = "hello"
}

In the above code, for class C_ We do not add any qualifier to the attribute name of 0, but in the main program, we can still directly access the attribute of the object through the. Name method through the created object, which seems to be no different from the public qualified attribute in Java. But is that really the case?

  • Scala's understanding of public:
    For the class C of the above code_ 0, we can get the following:
//decompiled from C_0.class
package com.PinkGranite.sparkCore;

import scala.reflect.ScalaSignature;

@ScalaSignature(
   bytes = "\u0006\u0001\u00152A\u0001B\u0003\u0001\u0019!)1\u0003\u0001C\u0001)!9q\u0003\u0001b\u0001\n\u0003A\u0002B\u0002\u0013\u0001A\u0003%\u0011DA\u0002D?BR!AB\u0004\u0002\u0013M\u0004\u0018M]6D_J,'B\u0001\u0005\n\u0003-\u0001\u0016N\\6He\u0006t\u0017\u000e^3\u000b\u0003)\t1aY8n\u0007\u0001\u0019\"\u0001A\u0007\u0011\u00059\tR\"A\b\u000b\u0003A\tQa]2bY\u0006L!AE\b\u0003\r\u0005s\u0017PU3g\u0003\u0019a\u0014N\\5u}Q\tQ\u0003\u0005\u0002\u0017\u00015\tQ!\u0001\u0003oC6,W#A\r\u0011\u0005i\tcBA\u000e !\tar\"D\u0001\u001e\u0015\tq2\"\u0001\u0004=e>|GOP\u0005\u0003A=\ta\u0001\u0015:fI\u00164\u0017B\u0001\u0012$\u0005\u0019\u0019FO]5oO*\u0011\u0001eD\u0001\u0006]\u0006lW\r\t"
)
public class C_0 {
   private final String name = "hello";

   public String name() {
      return this.name;
   }
}

We can see some clues from here. In fact, the name attribute is still of private type, but a name() method is also added. The function of this method is similar to the get method (that is, returning the value of the name attribute). We also know that in Scala, if the method has no parameters, you can directly omit the parentheses, which will become instance.name, It shows the illusion that the attribute is public!

  • Further explore the interpretation of private attribute in Scala:
    See the following code:
class User_(age: Int) {
  private var myAge: Int = age
}

Decompile it and get the following results:

//decompiled from User_.class
package com.PinkGranite.sparkCore;

import scala.reflect.ScalaSignature;

@ScalaSignature(
   bytes = "\u0006\u0001!2AAB\u0004\u0001\u001d!AQ\u0003\u0001B\u0001B\u0003%a\u0003C\u0003\u001a\u0001\u0011\u0005!\u0004C\u0004\u001f\u0001\u0001\u0007I\u0011B\u0010\t\u000f\u0001\u0002\u0001\u0019!C\u0005C!1q\u0005\u0001Q!\nY\u0011Q!V:fe~S!\u0001C\u0005\u0002\u0013M\u0004\u0018M]6D_J,'B\u0001\u0006\f\u0003-\u0001\u0016N\\6He\u0006t\u0017\u000e^3\u000b\u00031\t1aY8n\u0007\u0001\u0019\"\u0001A\b\u0011\u0005A\u0019R\"A\t\u000b\u0003I\tQa]2bY\u0006L!\u0001F\t\u0003\r\u0005s\u0017PU3g\u0003\r\tw-\u001a\t\u0003!]I!\u0001G\t\u0003\u0007%sG/\u0001\u0004=S:LGO\u0010\u000b\u00037u\u0001\"\u0001\b\u0001\u000e\u0003\u001dAQ!\u0006\u0002A\u0002Y\tQ!\\=BO\u0016,\u0012AF\u0001\n[f\fu-Z0%KF$\"AI\u0013\u0011\u0005A\u0019\u0013B\u0001\u0013\u0012\u0005\u0011)f.\u001b;\t\u000f\u0019\"\u0011\u0011!a\u0001-\u0005\u0019\u0001\u0010J\u0019\u0002\r5L\u0018iZ3!\u0001"
)
public class User_ {
   private int myAge;

   private int myAge() {
      return this.myAge;
   }

   private void myAge_$eq(final int x$1) {
      this.myAge = x$1;
   }

   public User_(final int age) {
      this.myAge = age;
   }
}

You can see that the type of the attribute is still private, but the qualifier of the corresponding get class method myage() is also changed to private, that is, we can't directly reference it. That's the difference!!

Tags: Java Scala

Posted on Wed, 17 Nov 2021 00:14:26 -0500 by Nile