第6章
一.类的定义
如图,进入软件,右击新建scala project
在src下右击新建 scala class,并命名为Person
在Person中:
class Person {
//申明一个变量,并对其进行初始化
var name:String=null
}
编写测试类PersonTest,即在src下,右击new一个scala app
object PersonTest extends App {
var p = new Person()
println(p)
}
右击,Run As,看到结果:输出的是一串地址
Object Person Test extends App{
Var p=new Person()
Println(p)
}
一.类和成员的访问
接着上面的Person类,可对其赋值,并输出
object PersonTest extends App {
//隐式的调用
var p1 = new Person()
p1.name = "John"
println(p1.name)
//显示的调用
var p2 = new Person
p2.name_=("Vicky")
println(p2.name)
}
可看到结果:
6.2:
object section6_2 extends App {
//6.2.1
class Person(val name:String,val age:Int){
println("constructing Person......")
override def toString():String={s"name=$name,age=$age"}
}
val p=new Person("john",29)
println(p.toString())
//6.2.2
class Person1(val name:String="",val age:Int=18){
override def toString()="name="+name+",age="+age
}
val p1=new Person1
println(p1.toString())
//6.2.3
class Person2 private(val name:String="1",val age:Int=18){}
//val p2=new Person2
}
效果如下:
6.4:
object section6_4 extends App {
//6.4.1
class Person(var name:String,var age:Int){
override def toString:String="name="+name+",age="+age
}
class Student(name:String,age:Int,var studentNo:String) extends Person(name,age){
override def toString:String=super.toString()+",studentNo="+studentNo
}
println(new Person("john",18))
println(new Student("nancy",19,"140116"))
//6.4.2
class Person1(var name:String,var age:Int){
println("zhi xing Person")
}
class Student1(name:String,age:Int,var studentNo:String) extends Person1(name,age){
println("zhi xing Student")
}
new Student1("nancy",19,"140116")
//6.4.3
class Person2(var name:String,var age:Int){
override def toString=s"Person($name,$age)"
}
class Student2(name:String,age:Int,var studentNo:String) extends Person2(name,age){
override def toString=s"Student($name,$age,$studentNo)"
}
println(new Student("nancy",19,"140116"))
//6.4.4
class Person3(var name:String,var age:Int){
def walk():Unit=println("walk() method in Person")
def talkTo(p:Person3):Unit=println("talkTo() method in Person")
}
class Student3(name:String,age:Int) extends Person3(name,age){
private var studentNo:Int=0
override def walk()=println("walk like an elegant swan")
override def talkTo(p:Person3)={
println("talkTo() method in Student")
println(this.name+" is talking to "+p.name)
}
}
class Teacher(name:String,age:Int) extends Person3(name,age){
private var teacherNo:Int=0
override def walk()=println("walk like an elegant swan")
override def talkTo(p:Person3)={
println("talkTo() method in Teacher")
println(this.name+" is talking to "+p.name)
}
}
val p1:Person3=new Teacher("albert",38)
val p2:Person3=new Student3("john",38)
p1.walk()
p1.talkTo(p2)
println("//////////")
p2.walk()
p2.talkTo(p1)
}
效果如下:
6.6:
object section6_6 extends App {
abstract class Person{
var age:Int=0
var name:String
def walk()
override def toString=name
}
class Student extends Person{
override var name:String =_
override def walk():Unit=println("Walk like a Student")
}
val p=new Student
p.walk()
}
效果如下:
第7章
一:trait基本使用
使用trait关键字定义一个名为Closable特质(相当于Java中的接口)
object section7_1 extends App {
trait Closable{
def close()
}
class File(var name:String) extends Closable{
override def close():Unit=println(s"File $name has been closed")
}
new File("config.txt").close()
}
可看到结果:
二:trait的使用
1. 右击src —>new project 取名为org.traits
定义第一个trait特质
其内容如下:即申明了4个函数方法
package org.traits
import org.model.Person
trait PersonDao {
def add(p:Person):Unit
def update(p:Person)
def delete(id:Int)
def findById(id:Int):Person
}
接下来写特质的实现类PersonDaoImp (类似Java中接口的实现),即它继承了PersonDao,要PersonDao中的所有方法
package org.traits
import org.model.Person
class PersonDaoImp extends PersonDao{
override def add(p:Person):Unit={
println("调用了add方法,增加了一个Person"+p)
}
def update(p:Person)={
println("调用了update方法,更新了一个Person"+p.toString)
}
def findById(id:Int):Person={
var result:Person = new Person(1,"John",18)//这里的Person可不写,可自动识别
println("调用了findById方法,id= "+result)
result // 相当于java中的return result
}
def delete(id:Int)={
println("调用了delete方法,删除一个Person,id="+id)
}
}
新建一个包名为org.model
定义一个类Person用来返回规定返回数据,避免发生返回参数地址的情况
package org.model
class Person(var id:Int,var name:String,var age:Int) {
override def toString():String={
s"Person's id =$id, name=$name, age=$age "
}
}
新建一个包,名为org.action
新建测试对象PersonManager
package org.action
import org.traits._
import org.model._
import org.action._
object PersonManager extends App {
var personDao:PersonDao = new PersonDaoImp()
var person:Person= new Person(2,"Vicky",23)
personDao.add(person)
personDao.update(person)
personDao.findById(1)
personDao.delete(2)
}
运行后,可看到结果:
2.混入trait的类对象的构造:
定义一个特质Logger:
package org.traits
trait Logger {
println("Logger")
def log(msg:String):Unit
}
定义特质FileLogger且继承Logger:
package org.traits
import java.io._
trait FileLogger extends Logger{
println("FileLogger")
var fileOutput = new PrintWriter("file.log")
fileOutput.println("#")
def log(msg:String):Unit={
fileOutput.print(msg)
fileOutput.flush() //存盘
}
}
在上面的PersonManager中添加一行代码:
new FileLogger{}.log("trait") //在scala中特质(接口)的实例化,必须用{}
运行后可看到效果:
找到文件位置,可看到在src同级目录下,可看到生成的file.log文件,打开后如下:
3. 提前定义与懒加载 (提前定义代码繁琐,建议使用懒加载)
特质Logger:
package org.traits
trait Logger {
def log(msg:String):Unit
}
新建FileLogger1继承Logger
package org.traits
import java.io._
import org.traits._
trait FileLogger1 extends Logger{
val fileName:String
println("FileLogger")
lazy val fileOutput=new PrintWriter(fileName:String)
override def log(msg:String)={
fileOutput.print(msg)
fileOutput.flush()
}
}
在student.class中添加:
val fileName="file.log"
在PersonManager中添加:
val p1=new Student(2,"e",777)
p1.log("#")
p1.log("123456哈哈")
运行后可看到结果: