Thematic class and Object

Source: Internet
Author: User

Class

1. A class is an abstraction of an object that is an instance of a class. Classes are abstract and do not consume memory, objects are specific and consume memory.

2. In Scala, a class is not declared public. Scala source files can contain multiple classes, all of which have public visibility.

3. When defining an parameterless class or method, define F = + call F, define f () = call F or f (); Definition c = new C or new C (), definition C () = new C or new C ()

Class Counter {//or class Counter ()

var value = 0//field value is private by default, but not equivalent to var private value = 0.

def increment () {value + = 1}//or increment, method default is public

def current = value//or current ()

}

Val myCounter = new Counter//Generate object or new Counter ()

Mycounter.increment ()//or increment

println (mycounter.current)

Spec. 1: The class name first letter is uppercase, the variables and functions are lowercase, the constant is all uppercase, the hump is used to identify.

Specification 2: No parameter class, () omitted, no parameter method, when the method returns the value = = () is omitted, otherwise does not save brackets. therefore increment () recommended.

Getter and setter

1. In Scala, fields in a class automatically have getter and setter methods, and when Scala generates a JVM-oriented class, fields without access control are private by default.

The corresponding Getter/setter method is public by default. When the field is decorated as private, the Getter/setter method becomes private.

Note: In Scala, the class field differs from Java, which is a private field + getter method (val) or + Getter/setter (Var),

There is no case of a setter but no getter, because write-only properties cannot be implemented in Scala. For a field, it may not have the same

Setter and getter, when the field is private to the object (Private[this] adornment).

Class Person {

var age = 0

}

Scalac Person.scala

Javap-private person

Public class person {

private int age; The character defaults thinks that Private;age is Val when private final int age

public int age (); The getter of the age in the field is called the

public void Age_$eq (int); The setter for the age of the field is called age_=, and the compiler will translate = $eq;val age-No setter method

public person ();

}

2. You can customize the getter and setter methods of the field.

Class Person {

var age = 0

def age_= (newage:int) {age = NewAge} //Error:method age_= is defined twice

}

Overwrite to:

Class Person {

private var privateage = 0

def age = Privateage

def age_= (newvalue:int) {if (newvalue> privateage) privateage = newvalue

}

Val fred = new person fred.age = Fred.age = 21 The result is: Fred.age is 30

The results of the anti-compilation are:

public class Person {

private int privateage;

private int privateage ();

private void Privateage _$eq (int);

public int age (); is not the Publice int age field.

public void Age _$eq (int);

public person ();

}

Description: All services provided by a module should be accessed through a uniform representation, as to whether they are obtained by storing (variables) or by calculation (method),

It is unknown from the way of access. In Scala, the caller of Fred.age does not know whether the age is implemented by field or by method. (In the JVM, the service

is always done by means of a compiler, or by a programmer. )

3. No setter and Getter method

Main constructor and secondary constructor

Object

Thematic class and Object

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.