scala-Classes and objects

Source: Internet
Author: User
Tags class definition constructor
http://blog.csdn.net/lovehuangjiaju/article/details/47009607
The main contents of this section

1 class definitions, creating objects
2 Main constructor
3 Auxiliary constructor class definition, creating object

Use keyword class to define class person
{
  //class member must initialize, otherwise error
  //This is defined as a public member
  var name:string=null
}
1 2 3 4 5 6 1 2 3 4 5 6

The person class generates a Person.class file after compiling

Using the javap-prviate Person command to view the contents of a bytecode file, you can see the following

D:\scalaworkspace\scalachapter06\bin\cn\scala\xtwy>javap-private Person
Warning: The binary file person contains Cn.scala.xtwy.Person
Compiled from the "Person.scala" public
class Cn.scala.xtwy.Person {
  private java.lang.String name;
  Public java.lang.String name ();
  public void Name_$eq (java.lang.String);
  public Cn.scala.xtwy.Person ();
}
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9

The contents of the bytecode file can be seen: although we define only one class member (domain) name in the person class, the type is string, but Scala will default to help us generate name () and name_= () and constructor person (). where name () corresponds to the Getter method in Java, name_= () corresponds to the setter method in Java (because it is not allowed to appear in the JVM =, it is replaced with $eq. It is important to note that a public member is defined, but the resulting bytecode is implemented in a private way, and the generated getter, setter method is public
Therefore, you can create a person object directly from the new operation

The default already has a build function, so you can directly new
scala> Val p=new person ()
P:person = person@84c504

//Call getter and setter method directly
/ /setter method
scala> p.name_= ("John")
//getter method
scala> p.name
res2:string = John

//Direct modification, But actually the call is p.name_= ("Jonh")
scala> p.name= "Jonh"
p.name:string = Jonh

//getter method
scala> P.name
res28:string = Jonh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

You can also define your own getter and setter methods.

Class person{
  //define Private member
  var Privatename:string=null;

  Getter Method
  def name=privatename
  //setter method
  def name_= (name:string) {
    this.privatename=name
  }

}
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12
D:\scalaworkspace\scalachapter06\bin\cn\scala\xtwy>javap-private Person
Warning: The binary file person contains Cn.scala.xtwy.Person
Compiled from the "Person.scala" public
class Cn.scala.xtwy.Person {
  Private java.lang.String privatename;
  Private java.lang.String privatename ();
  private void Privatename_$eq (java.lang.String);
  Public java.lang.String name ();
  public void Name_$eq (java.lang.String);
  public Cn.scala.xtwy.Person ();
}
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12

From the generated bytecode can be seen: (1) defined as a private member, its getter, setter method is also private; (2) directly accessible is our own definition of getter, setter method. The following is the invocation method

Scala> Val p=new person ()
P:person = person@12d0b54

scala> p.name
res29:string = null

//Direct Assignment Method 
  scala> p.name= "John"
p.name:string = John

scala> p.name
res30:string = John
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12

From the result of the execution of the code, we can know that the caller does not need to know whether to operate through a method call or field access by P.name= "John", which is the famous unified access principle

If the member domain of the class is a variable of type Val, only the getter method is generated

Class Person {
  //classes member must initialize, otherwise error
  //This is defined as a Val public member
  Val name:string= "John"
}

D:\ScalaWorkspace\ Scalachapter06\bin\cn\scala\xtwy>javap-private Person
Warning: The binary file person contains Cn.scala.xtwy.Person
Compiled From ' Person.scala ' public
class Cn.scala.xtwy.Person {
  private final java.lang.String name;
  Public java.lang.String name ();
  public Cn.scala.xtwy.Person ();
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

As can be seen from the bytecode file: The Val variable corresponds to the final type variable in Java, only the Getter method is generated

If the member field is defined as Private[this], the getter, setter method is not generated

Class Person {
  //member must initialize, otherwise error
  //private[this] modifier
  Private[this] var name:string= "John"
}

D:\ Scalaworkspace\scalachapter06\bin\cn\scala\xtwy>javap-private Person
Warning: The binary file person contains Cn.scala.xtwy.Person
Compiled from the "Person.scala" public
class Cn.scala.xtwy.Person {
  private java.lang.String name;
  public Cn.scala.xtwy.Person ();
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 8 9 10 11 12 13 14

In the Java language, the Setxxx (), GetXxx () methods are generated when defining JavaBean, but the getter method and setter method generated by the Scala language are not. If you also need the program to automatically generate getter methods and setter methods, you need to introduce Scala.reflect.BeanProperty
Then modify the variable in the way of annotations

Class Person {//
  member must initialize, otherwise error
  //@BeanProperty used to generate Getxxx,setxxx method
  @BeanProperty var name:string= "John "
}

d:\scalaworkspace\scalachapter06\bin\cn\scala\xtwy>javap-private person
Warning: The binary file person contains Cn.scala.xtwy.Person
Compiled from the "Person.scala" public
class Cn.scala.xtwy.Person {
  private java.lang.String name;
  Public java.lang.String name ();
  public void Name_$eq (java.lang.String);
  public void SetName (java.lang.String);
  Public java.lang.String getName ();
  public Cn.scala.xtwy.Person ();
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

The following figure shows the rules generated by getter and setter methods.

Source: Scala for the impatient class main constructor

The definition of the main constructor is intertwined with the definition of the class, and the constructor parameters are placed directly after the class name, as in the following code:

The following code defines not only a class person, but also the main constructor, with the parameters of the main constructor String, the Int type
class person (val name:string,val age:int)

d:\ Scalaworkspace\scalachapter06\bin\cn\scala\xtwy>javap-private Person
Warning: The binary file person contains Cn.scala.xtwy.Person
Compiled from the "Person.scala" public
class Cn.scala.xtwy.Person {
  Private final java.lang.String name;
  

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.