Coder Social home page Coder Social logo

dsc-enterprise-hsbc-scala-classes's Introduction

Classes

About class

  • Classes are the templates or blueprints of a construct that encapsulates state and manages behavior.
  • Classes have been around for a long time and in every object oriented language.
  • Since Scala is a half object oriented language, half functional language, there are naturally classes.

Our first class

  • A class is public by default, so no need for a public modifier
  • The (firstName:String, lastName:String) is the primary constructor!
  • In Scala the primary constructor is "top-heavy" with a constructor that contains all the information
  • Other constructors are smaller constructors that feed the top constructor
  • The reason for the top heavy constructor is immutability
class Employee(firstName:String, lastName:String)
defined class Employee

Instantiating the class

Instantiating the class is fairly straightforward

val emp = new Employee("Dennis", "Ritchie")
emp: Employee = Employee@1d79abe9

Can’t access or modify member of a class?

  • As it stands in our class above, we can neither access or modify our class
  • Most of the time we don’t want to modify our class for immutability purposes
  • To be able to access the members, we will predicate each of the values with val
  • To be able to mutate the member variables, we predicate each of the values with var (Not recommended)

As it stands, running javap -p Employee from the shows the following Java code:

public class Employee {
  public Employee(java.lang.String, java.lang.String);
}

NOTE: javap -p shows the translated Java code from Scala.

Accessing and Mutating

  • val will create a Scala-style "getter"
  • var will create a Scala-style "setter"
class Employee(val firstName:String, var lastName:String)
val emp = new Employee("Dennis", "Ritchie")
defined class Employee
emp: Employee = Employee@79034a3b
println(emp.firstName)           //Works because of val
Dennis
println(emp.lastName)            //Works because of var
Ritchie
emp.lastName = "Hopper" //Works because of var
emp.lastName: String = Hopper
println(emp.lastName)
Hopper

Viewing bytecode with val and var

  • The bytecode generated from javap -p Employee
    • Using val for firstName
    • Using var for lastName
public class Employee {
  private final java.lang.String firstName;
  private java.lang.String lastName;
  public java.lang.String firstName();
  public java.lang.String lastName();
  public void lastName_$eq(java.lang.String);
  public Employee(java.lang.String, java.lang.String);
}

Classes Conclusion

  • Classes are templates or blueprints
  • val creates accessors, methods that will allow to access the inner state
  • var create mutators and accessors, mutators allow us to change inner state.

IMPORTANT: We rarely use var, it would be best to avoid.

dsc-enterprise-hsbc-scala-classes's People

Contributors

mike-kane avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.