Scala fields in Classes

Source: Internet
Author: User

Scala fields in Classes

private var definition field
Class Student (name:string, age:int) {private var _id:int = Name.hashcode def id:int = _id//reader method def id_= (newid:int): Unit = _id = NewId//writer method def sayhi (): String = {"My name is" + name + ", my-age was" + age} }

Within the class, define one such attribute field, private var _id:int = Name.hashcode. Decompile the following,

C:\workspace5-gitosc\scala-sample\out\production\scala-sample\com\usoft>javap-p Student.classCompiled from "  Student.scala "public class Com.usoft.Student {private final java.lang.String name;  private final int age;  private int _id;  private int _id ();  private void _id_$eq (int);  public int id ();  public void Id_$eq (int);  Public java.lang.String sayhi (); Public com.usoft.Student (java.lang.String, int);}

_id as an attribute field in the Scala class, explicitly defines the reader method and the writer method

Object Main0 {def main (args:array[string]) {println ("Hello world!!") val s = new Student ("Xxggy", "a") println (s.id) s.id = "Xxggyy". Hashcode println (s.id)}}

Read and write the value of _id through reader method and writer method. Also note that because the field is defined with private,

Scala also implicitly generates these two methods, both of which are private.

private int _id ();p rivate void _id_$eq (int);


Private Val Definition Field
Class Student0 (name:string, Age:int) {private Val _id:int = Name.hashcode def id:int = _id def sayhi (): String = { ' My name is ' + name + ', my age is ' + age '}

Using private Val in a class declares _id, because Val is immutable, there can be only reader method.

Val s0 = new Student0 ("XX", println) (s0.id)

Anti-compilation results,

C:\workspace5-gitosc\scala-sample\out\production\scala-sample\com\usoft>javap-p Student0.classCompiled from "  Student.scala "public class Com.usoft.Student0 {private final java.lang.String name;  private final int age;  private final int _id;  private int _id ();  public int id ();  Public java.lang.String sayhi (); Public com.usoft.Student0 (java.lang.String, int);}

The immutable variable of the Val definition is the final type of private final int _id;. You can also see Scala's implicitly generated private int _id () method, which is not defined by us.


Val and var define field
class student1 (name: string, age:  int)  {  val _id: int = name.hashcode  def id: int  = _id  def sayhi (): string = {     "My name  is  " + name + ",my age is  " + age  }}class  student2 (Name: string, age: int)  {  var _id: Int =  name.hashcode  def id: int = _id //is equivalent to Getter method   def id_= ( Newid: int):  unit = _id = newid //equivalent setter method   def sayhi ():  String = {     "my name is "  + name +  ", my age is  " + AGE  }} 

The above two classes are directly defined with Var and Val, without the addition of an access modifier, and we'll look at the effect of the no-access modifier by recompiling.

c:\workspace5-gitosc\scala-sample\out\production\scala-sample\com\usoft>javap -p  student1.classcompiled from  "Student.scala" public class com.usoft.student1 {   private final java.lang.String name;  private final int age;   PRIVATE FINAL INT _ID;  PUBLIC INT _ID ();//scala implicit generation of reader method   public int id ();//The reader method defined by   public java.lang.string sayhi ();   public com.usoft.student1 (java.lang.string, int);} c:\workspace5-gitosc\scala-sample\out\production\scala-sample\com\usoft>javap -p  student2.classcompiled from  "Student.scala" public class com.usoft.student2 {   private final java.lang.String name;  private final int age;   PRIVATE INT _ID;  PUBLIC INT _ID ();//scala implicit generation of reader method &nBsp; public void _id_$eq (int);  // scala implicitly generated   public int id ();// The reader method defined   public void id_$eq (int);//The writer method defined   public java.lang.string  sayhi ();   public com.usoft.student2 (java.lang.string, int);}

So the effect is obvious.


Val and private Val define Field
Class Student3 (name:string, Age:int) {val _id:int = Name.hashcode def sayhi (): String = {"My name is" + name +  ", my age was" + Age}}class Student4 (name:string, Age:int) {private Val _id:int = Name.hashcode def sayhi (): String = {' My name is ' + name + ', my age is ' + age}}

Anti-compilation comparison of similarities and differences,

c:\workspace5-gitosc\scala-sample\out\production\scala-sample\com\usoft>javap -p  student3.classcompiled from  "Student.scala" public class com.usoft.student3 {   private final java.lang.String name;  private final int age;   PRIVATE FINAL INT _ID;  PUBLIC INT _ID ();   public  java.lang.string sayhi ();   public com.usoft.student3 (java.lang.String, int);} c:\workspace5-gitosc\scala-sample\out\production\scala-sample\com\usoft>javap -p  student4.classcompiled from  "Student.scala" public class com.usoft.student4 {   private final java.lang.String name;  private final int age;   PRIVATE FINAL INT _ID;  PRIVATE INT _ID ();   public  java.lang.string sayhi ();   public com.uSoft. Student4 (java.lang.string, int);}

The reader method defined in Val is:

public int _id ();

The reader method defined with private Val is:

private int _id ();

The difference is right here. In fact, I think the access modifier added to the field definition can only be added to the private and protected access modifiers, which indicates that the access modifier for the field's automatically generated reader method is private.

Add protected access modifier, generated reader method is still public, can be verified,

Class Student5 (name:string, Age:int) {protected Val _id:int = Name.hashcode def sayhi (): String = {"My name is" + name + ", my age was" + age}}

Anti-compilation,

C:\workspace5-gitosc\scala-sample\out\production\scala-sample\com\usoft>javap-p Student5.classCompiled from "  Student.scala "public class Com.usoft.Student5 {private final java.lang.String name;  private final int age;  private final int _id;  public int _id ();  Public java.lang.String sayhi (); Public com.usoft.Student5 (java.lang.String, int);}

You see the public int _id (); This reader method is still public permission.

======================================end======================================

Scala fields in Classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.