Scala Object Oriented - Detailed Class 2 (Inherited related)

1, singleton class

Package com.zzzy 

class the AAA { // singleton 
  / * // Java ideas - Constructor privatization, providing feasible disclosed getAAA 
  Private the this DEF () { 
    the this () 
  } 

  DEF getAAA (): = {the AAA 
    Val AAA the AAA new new = () 
    return AAA 
  } 
* / 
}
Package com.zzzy
 // singleton 

// companion class 
class the BBB Private () { // . 1 privatization constructor 


} 

// companion objects 
Object the BBB { 

  // do not write Apply 
  Private Val BBB = new new the BBB () // 2 Create a private object 
  DEF getBBB: the BBB = { // . 3 provides a get method disclosed 
    BBB 
  } 

}
package com.zzzy
object test {
  def main(args: Array[String]): Unit = {

    val b1 = BBB.getBBB
    val b2=BBB.getBBB
    println(b1==b2)//true

  }

}

2, object inheritance and App

Package com.zzzzy 

abstract  class the Register { // define an abstract class
   // abstract method (Method body not only defined method) 
  DEF REGIST (name: String) 

  DEF unregist (name: String) 

}
Package com.zzzzy
 // implemented in the extends 
Object User1 the extends the Register { 
  the override DEF REGIST (name: String): Unit = { 
    the println (name + "registered" ) 

  } 

  the override DEF unregist (name: String): Unit = { 
    the println ( name + "logged out" ) 

  } 
}
package com.zzzzy

class User2(n:String) extends Register {//主构造方法
  override def regist(name: String): Unit = {
    println(name+"注册"+n)

  }

  override def unregist(name: String): Unit ={
    println(name+"注销"+n)
  }
}
Package com.zzzzy 

Object Test the extends App { // . 1 if a main object inherits methods App may not need to run directly 
  / * DEF main (args: the Array [String]): Unit = { 
    the println ( "AAA") 
  } * / 

  the println ( "bbb" ) 

  // 2 let a object implement an abstract class method to rewrite the keyword extends unrealized 
  User1.regist ( "Mandy" ) 
  User1.unregist ( "smile" ) 

  // 3 allows a class to achieve abstract class does not implement a method of rewriting can be configured with keyword extends to a method 
  new new User2 ( "for nothing") .regist ( "Lili" )
   new new User2 ( "for nothing") .unregist ( "Jack" ) 

}

3, class inheritance

Package com.zzzzzy 

class the Person { 
  var name: String = _ // need to type, can not be inferred 
  var Age: Int = _
   Private var NUM = 99 
  DEF Show (): Unit = { 
    Print ( "parent class" ) 
  } 

}
Package com.zzzzzy
 class Student the extends the Person {
   // can not be overwritten by the modified method of final 


  var Sex: Char = 'M' 

  the override DEF show (): Unit = { 
    the println ( "override method show the parent class" ) 
  } 

  DEF Speak (): Unit = {
     // call the parent class property measured can not be used in the operation 
    / * println (super.age) 
    Print (super.name) * / 
      // private property can not be found by num Super 


    Super .Show ( ) // parent method 
    Show () // subclass rewritable 
  } 



}
Package com.zzzzzy 

Object Test the extends the App { 
  Val STU = new new Student 
  stu.speak () 


// upwardly polymorphic transition 
  var S: the Person = new new Student ()
   // also get unique subclass of methods and attributes? Not 
  s.show () // subclass rewriting process called 
  the println (s.name) // null 
  the println (s.age) 
  

}

4, type checking and type conversion

package com.zzzzzzy

class Person(name:String,age:Int){
  var name1="小明"
  var age1=50
  def show(): Unit ={
    println(name+"\t"+age)
  }



}
Package com.zzzzzzy 

// in a file scala n classes can be defined on one line must be written; braces can be omitted 
class Emp the extends the Person ( "John Doe", 40 )
 class Student the extends the Person ( "Joe Smith", 30)
Package com.zzzzzzy 

Object Test the extends the App { 
   Val EM = new new Emp 
  em.show () 
   Val STU = new new Student 
  stu.show () 
  
  the println ( "------------------ --- " )
   // determine whether an object is a certain type of 
 Val SS = new new Student ()
   // on inheritance A iS 
  println (ss.isInstanceOf [the Person]) 

  // conversion - upward transition 
  val pp: Person = ss.asInstanceOf [the Person] 
  pp.show () 

}

5, protected keywords

Package com.yz.extendz 

class Animal { // parent class A and B is not a subclass of Animal. Then can use the following attributes 
  Val A = 100            // A useful class            // Class B available 
  protected   Val B = 200 is
   protected [ the this ] C = 300 Val
   protected [extendz] D = 400 Val @ YZ package name      // a useful class 


  // here four attributes as are Animal subclass can be used, both in the same package not 



}
package com.yz.extendz

class Cat extends Animal{
  val y=888
  def show: Unit ={
    println(s"$a-------$b------$c---------$d")
  }

}
package com.yz.extendz

class Dog extends Animal {
  val x=999
  def show: Unit ={
    println(s"$a-------$b------$c---------$d")
  }

}
package com.yz.extendz

class A {
  def show: Unit ={
    val animal = new Animal
    println(animal.a)
    println(animal.d)
  }


}
package com.yz2

import com.yz.extendz.Animal

class B {
  def show: Unit ={
    val animal = new Animal
    println(animal.a)
  }

}

6, anonymous subclass

package com.yz3

class Person {
  def aaa="中国"

}
Package Penalty for com.yz3 

Object the Test   the extends App { 

  Val PP = new new the Person { 
    the override DEF aaa: String = "new China" 
  } 
 // this process creates an anonymous subclass [rewrite occurs, subclasses override the parent class method but custom processing and no display ---- anonymous] 

   Val the Result = pp.aaa 
  println (the Result) 


  // defined method 
  println ( "-------------------" ) 
  Show DEF (P: the Person): Unit = { 
    the println (p.aaa) 
  } 

  // call 
  Show ( new new the Person) 
}

7, abstract

Package com.yz4 

abstract  class Animal { // may be used to abstract or non-abstract methods have abstract fields / non-abstract field
 // Abstract field (not the initial value) 
  Val COUNT: Int
   // non-abstract field 
  Val NUM = 100 // Abstract method   DEF AAA: String
   // non-abstract methods 
  DEF BBB: Int = {
     10 
  } 
}


  

Package com.yz4 

class Cat the extends Animal {
   // override abstract method 
  override def aaa: String = { "Hello" } 

  the override Val COUNT: Int = 666 
}
package com.yz4

object test extends App {
 val cat = new Cat
   val aaa: String = cat.aaa
   val bbb: Int = cat.bbb
   println(aaa+bbb)

}

8, trait (java interfaces)

Package com.yz5 

trait the Log { 
  // an error occurs when the non-abstract methods call super abstract 
  DEF Showa: String = {
 "original" 
  } 

}
Package com.yz5 

trait ALOG the extends the Log { 
  the override Showa DEF: String = {
     Super .showa 
    the println ( "ALOG override method" )

     "ALOG" 
  } 

}
Package com.yz5 

trait BLog the extends the Log { 
  the override Showa DEF: String = { 
    the println ( "BLog override method" )
     Super .showa

     "BLog" 
  } 

}
Package Penalty for com.yz5 

class CCC the extends the alog with BLog { // inherits two qualities 

  // Showa () // default method should be called the same name as the characteristics of the rightmost occurrence of the same name [to] the right of the main 
  override def showa: String = {
     Super .showa 
  } 

}
Package Penalty for com.yz5 
Object the Test the extends App { 
   Val ccc = new new CCC
             // conditions: ALog and BLog rewrite method called super.showa 
  ccc.showa // default all the run out (ALog and BLog the run a) no way to run just to the left of 

}

 

Guess you like

Origin www.cnblogs.com/qfdy123/p/11431772.html