스칼라 언어 대상 프로 그래 밍

19243 단어 빅 데이터
 、         :               ,      (  class)
           :
    (1)  
    (2)  
    (3)  

 、   : class,       : publicprotectedprivate
class Student1 {
  //    
  private var stuName:String = "Tom"
  private var stuAge:Int = 20

  //    (  )
  def getStuName():String = stuName
  def setStuName(newName:String) = this.stuName = newName

  def getStuAge():Int = stuAge
  def setStuAge(newAge:Int) = this.stuAge = newAge
}

//        ,      
// Java: public static void main()******
//  :objec          ,    ,  object           
object Student1{
  //    
  def main(args: Array[String]): Unit = {
    //      
    var s1 = new Student1

    //     
    println(s1.getStuName()+"\t" + s1.getStuAge())

    //  set  
    s1.setStuName("Mary")
    s1.setStuAge(25)

    //     
    println(s1.getStuName()+"\t" + s1.getStuAge())

    //     :           
    //  :               ??? ---->   :   set get  
    println(s1.stuName + "\t" + s1.stuAge)

  }
}
get set  
/*
   get set  
1、        ,   private,Scala        set get  
     private var stuName:String = "Tom"
     (1) get  :stuName
     (2) set  : stuName_

2、    get  ,  set  ? --->       :    val
      private val money:Int = 1000

3、     get set  : private[this]
              
 */

class Student2 {
  //    
  private var stuName:String = "Tom"

  //  get  
  private val money:Int = 1000
}

object Student2{
  def main(args: Array[String]): Unit = {
    var s2 = new Student2

    println(s2.stuName)

    //  money  ===> error   
    //s2.money = 2000
  }
}

 、   (   ):  :Student    :  Course
import scala.collection.mutable.ArrayBuffer

class Student3 {

  //       (   ):        
  //      
  class Course(val courseName:String,val credit:Int){
    //     
  }

  //  
  private var stuName:String = "Tom"
  private var stuAge:Int = 20

  //                 
  private var courseList = new ArrayBuffer[Course]()

  //    :            
  def addNewCourse(cname:String,credit:Int): Unit ={
    //      
    var c = new Course(cname,credit)

    //  list
    courseList += c
  }
}

object Student3{
  def main(args: Array[String]): Unit = {
    //    
    var s3 = new Student3

    //       
    s3.addNewCourse("Chinese",3)
    s3.addNewCourse("English",3)
    s3.addNewCourse("Math",3)

    //  
    println(s3.stuName+"\t"+s3.stuAge)
    for(s "\t" + s.credit)
  }

}
 、   :(1)      (2)     
/*
   :(1)      :         ,         
       (2)     :         ,     :this
 */

class Student4(val stuName:String,val stuAge:Int) {
  //         
  def this(age:Int){
    //      
    this("No Name",age)
  }
}

object Student4{
  def main(args: Array[String]): Unit = {
      //            
    var s4 = new Student4("Tom",24)
    println(s4.stuName + "\t" + s4.stuAge)

    //             
    var s5 = new Student4(25)
    println(s5.stuName + "\t" + s5.stuAge)
  }
}
Objectstatic   
    1、    :         
                  :        
object CreditCard {

  //    :        
  //         
  private[this] var creditCardNumbe:Long = 0

  //    
  def generateNewCCNumber():Long = {
    creditCardNumbe += 1
    creditCardNumbe
  }

  //    
  def main(args: Array[String]): Unit = {
    //      
    println(CreditCard.generateNewCCNumber())
    println(CreditCard.generateNewCCNumber())
    println(CreditCard.generateNewCCNumber())
    println(CreditCard.generateNewCCNumber())
  }
}
    2、      : App  ----->     main  
object MainAppObject extends App {
  //    main  
//  def main(args: Array[String]): Unit = {
//    println("Hello World")
//  }

  println("Hello World")
}

 、  apply  :  newval s1 =  new Student
          val s2 = Student  --->      Student  apply  

class Student5(val stuName:String){
}

object Student5 {
  //    apply  
  def apply(stuName:String) = {
    println("    apply  ")

    //      
    new Student5(stuName)
  }

  def main(args: Array[String]): Unit = {
    //      
    var s1 = new Student5("Tom")
    println(s1.stuName)

    //      ,  new   
    var s2 = Student5("Mary")
    println(s2.stuName)
  }
}
extends
    1234、   :         ,         
           :             
//  
/*
1、     
2、        
3、      
 */

//    
class Person(val name:String,val age:Int){
  //  (  )
  def sayHello():String = "Hello " + name + " and the age is " + age;
}

//    
class Employee(override val name:String,override val age:Int,salary:Int) extends Person(name,age){
  //      sayHello  
  override def sayHello(): String = "    sayHello  "
}

object Demo1 {
  def main(args: Array[String]): Unit = {
    //    Person  
    var p1 = new Person("Tom",20)
    println(p1.sayHello())

    //      
    var p2:Person = new Employee("Mike",25,1000)
    println(p2.sayHello())

    //        , Person  
    var p3:Person = new Person("Jerry",26){
      //      ,       
      override def sayHello(): String = "      sayHello  "
    }
    println(p3.sayHello())
  }
}
//    

//  :        
abstract class Vehicle{
  //        
  def checkType():String
}

//  
class Car extends Vehicle{
  //      
  def checkType():String = {"I am a Car"}
}

class Bike extends Vehicle{
  //      
  def checkType():String = {"I am a Bike"}
}

object Demo2 {
  def main(args: Array[String]): Unit = {
    var v1:Vehicle = new Car
    var v2:Vehicle = new Bike

    println(v1.checkType())
    println(v2.checkType())
  }
}
package p1

//  :  
abstract class Person{
  //        
  var id:Int
  var name:String
}

//    
abstract class Employee1 extends  Person{
  //var id:Int = 1
  var name:String = "No Name"
}

//     :         ,      ,      
class Employee2(var id:Int) extends  Person{

  //   name
  var name:String = "No Name"
}


class Demo3 {

}
trait:     、       
                     
package p2

//  :trait
//     trait
trait Human{
  //      
  var id:Int
  var name:String

  //  :              
  def sayHello():String = "Hello " + name
}

//     trait
trait Action{
  //        
  def getActionName():String
}

//      :      trait  
//   :  extends ... with
class Student6(var id:Int,var name:String) extends Human with Action{

  //  Action  getActionName
  def getActionName():String = "Action is Running"
}

object Demo4 {

  def main(args: Array[String]): Unit = {
    //        
    var s6 = new Student6(1,"Tom")
    println(s6.sayHello())
    println(s6.getActionName())
  }
}
 、     
        :package  ****{
               class ****
               def   
               val   

                 
            }

좋은 웹페이지 즐겨찾기