scala programming idea -- class method

Coverage method

So far, none of the classes we inherit actually perform anything that distinguishes them

package com.test1

object Scala09_test13 {
  def main(args: Array[String]): Unit = {
    //Authentic scala

    //Define operator
    //Method names can contain almost all characters. For example, when you create a math package, you can follow a mathematician's approach
    class Molecule{
      //var name = _ (if the variable var cannot be val)
      var attached:Molecule = _
      def plus(other:Molecule)=attached=other
      def +(other:Molecule)=attached=other
    }
    var m1 = new Molecule
    var m2 = new Molecule
    println(m1.plus(m2))
    println(m1.+(m2))
    //The symmetry of this class is
    //Note that in section
    import language.postfixOps
    class Swearing{
      def #!>% = "Rowzafrazca"
    }
    val x = new Swearing
    println(x.#!>%)
    println(x.#!>%)

    //Automatic string conversion
    //The case class can properly format an object with its parameters in a form suitable for display
    case class Bicycle(riders:Int)
    val forTwo = Bicycle(2)
    println(forTwo.riders)
    println(forTwo)
    //A method called toString is automatically created whenever a case class is created. Whenever you operate on an object
    //And I want it to be a String,
    class Surrey(val adorment:String){
      override def toString: String = s"Surrey with the $adorment"
    }
    val fancy = new Surrey("fringe on top ")
    println(fancy)

    //tuple
    //Suppose more than one result must be returned from a method, such as a value and some information about the value

    //Companion object
    //Methods act on specific objects of a class
    class X(val n:Int){def f = n *10}

    object X{
      val n = 2
      def f = n*10
      def g = this.n*20
    }
    val x1 = new X(1)
    val x2 = new X(2)
    //When f is called, it must be called on an object. During the call, f can directly access the members of the object
    println(X.n)
    println(X.f)
    println(X.g)

    //Base class initialization
    // Scala ensures the correct object creation process by ensuring that all constructors are called
    class GreatApe(val weight:Double,val age:Int)
    class Bononbo(weight:Double,age:Int) extends GreatApe(weight,age)
    class Chimpanzee(weight:Double,age:Int) extends GreatApe(weight,age)
    class BonoboB(weight:Double,age:Int) extends Bononbo(weight,age)

    def display(ape: GreatApe)=s"weight:${ape.weight} age:${ape.age}"
    println(display(new GreatApe(100,12)))
    println(display(new Bononbo(100,12)))
    println(display(new Chimpanzee(100,12)))
    println(display(new BonoboB(100,12)))

    class House(val addrees:String,val state:String,val zip:String){
      def this(state:String,zip:String)={
        this("address?",state,zip)
      }
      def this(zip:String)={
        this("address?","state?",zip)
      }
    }

    class Home(addrees:String,state:String,zip:String,val name:String)  extends
      House(addrees,state,zip){
      override def toString: String = s"$name:$addrees,$state,$zip"
    }

    class VacationHouse(state:String,zip:String,val startMonth:Int,val endMonth:Int) extends House(state,zip)

    class TreeHouse(val name:String,zip:String) extends House(zip)
    val h = new Home("888 N. Main st.","KS","66632","Metropolis")



  }
}

enumeration

Enumeration is a collection of names. scala's enumeration class provides a convenient way to manage these names.

Abstract class:

Abstract classes are just like ordinary classes, but one or more of them are incomplete

scala continues the java approach

120 original articles published, 27 praised, 310000 visitors+
Private letter follow

Tags: Scala Java

Posted on Sun, 02 Feb 2020 08:52:06 -0800 by akreation