스칼라 언어 대상 프로 그래 밍
19243 단어 빅 데이터
、 : , ( class)
:
(1)
(2)
(3)
、 : class, : public、protected、private
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)
}
}
、Object : static
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 : new
:val 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
1、
2、
3、
4、 : ,
:
//
/*
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
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
spark 의 2: 원리 소개Google Map/Reduce 를 바탕 으로 이 루어 진 Hadoop 은 개발 자 에 게 map, reduce 원 어 를 제공 하여 병렬 일괄 처리 프로그램 을 매우 간단 하고 아름 답 게 만 들 었 습 니 다.S...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.