package com.gsau.util.Chapter5
import java.io.IOException
/**
* 方法的默认权限是public
* private[this] 对当前实例可见
* private 对当前类的所有实例可见
* protected 对当前类及其子类的实例可见
* private[packageName] 对packageName包下的所有子类可见
* public 公开的
* @param username
* @param password
*/
class MethodTest(var username: String, var password: String) {
/**
* 【作用域1】对象私有:是这个类的每个对象特有的,不同对象不能通用
* 返回值类型不是必须的
* 当没有参数的时候圆括号也不是必须
* 方法体简单的时候花括号也不是必须的
* @return
*/
private[this] def isMethodTest = true
/**
* 【作用域2】类私有:这个法方法对当前实例和当前类的其他实例都是可见的
* 类的私有方法对其子类,是不可见的
*
* @return
*/
private def isMethodTest1 = true
/**
* 【作用域3】保护作用域:类和他的子类的实例是可见的
*
* @return
*/
def isMethodTest2 = {
import TemplatePackage._
val dog = new Dog
var info = dog.breath
}
/**
* 【作用域3】包作用域:该包下的类是可见的
*
* @return
*/
private[Chapter5] def isMethodTest3 = {
println("private[Chapter5]")
}
/**
* 方法的默认参数值指定
*
* @param timeOut 500
* @param protocol http
*/
def makeConnection(timeOut: Int = 500, protocol: String = "http") {
println("timeout =%d,protocol=%s".format(timeOut, protocol))
}
/**
* 使用tuples从方法中返回多个值
*
* @return
*/
def getPeopleInfo = {
("苍井空", 23, "女", "日本", "东京")
}
/**
* 接受可变参数的方法
*
* @param strs
*/
def printAll(strs: String*) {
strs.foreach(print)
}
/**
* throws 注解:声明可能抛出的异常
*/
@throws(classOf[IOException])
def play {
printAll("x", "y")
}
/**
* scala 链式编程风格的写法
*/
def method1(firstName: String): this.type = {
username = firstName
this
}
def method2(password: String): this.type = {
this.password = password
this
}
/**
* scala定义一个方法的方式1
*
* @param x
* @param y
* @return
*/
def doSomeThing(x: String, y: Int): String = {
"其实我什么事都没有干"
}
/**
* scala 定义方法的方式2
* 返回值类型不是必须的,方法体很短的时候花括号也可以不写
*
* @param i
* @return
*/
def method1(i: Int) = i + 1
}
/**
*
*/
package TemplatePackage {
trait Human {
def hello = "Human特质"
}
trait Monther extends Human {
override def hello: String = "Monther特质"
}
trait Father extends Human {
override def hello: String = "father特质"
}
class Animal {
private[TemplatePackage] val message = "这是一个包级别的常量"
protected def breath: Unit = {
val name = this.getClass.getSimpleName
println(s"$name 中的 breath方法")
}
/**
* 这个是一个包级别的方法,同一个包下的其它类都可见
*/
private[TemplatePackage] def breath1 = {
}
}
class Dog extends Animal {
val name = this.getClass.getSimpleName
override def breath: Unit = {
println(s"$name 继承来的breath方法")
println(message)
}
}
/**
* scala 继承多个特质
*/
class Child extends Human with Monther with Father {
def printSuper = super.hello
def printMonther = super[Monther].hello
def printFather = super[Father].hello
def printHuman = super[Human].hello
/**
* 参数的复制荀勖默认是从左往右
* @param timeout
* @param protocol
*/
private[TemplatePackage] def getConnection(timeout: Int = 500, protocol: String = "https") {
println(s"timeout= $timeout protocol= $protocol")
}
}
object test {
def main(args: Array[String]): Unit = {
val methodTest = new MethodTest("方法", "测试类")
val child = new Child
println(child.hello)
println(child.printFather)
println(child.printHuman)
println(child.printSuper)
child.getConnection()
child.getConnection(500000)
child.getConnection(protocol = "udp")
child.getConnection(1000, "http")
val info = methodTest.getPeopleInfo
val tuple = info.copy()
println(tuple._1, tuple._2, tuple._3, tuple._4, tuple._5)
println(info._1, info._2, info._3, info._4, info._5)
methodTest.printAll("苍井空\n")
methodTest.printAll("苍井空", "158岁\n")
val list = List("苍井空", "158岁", "还是女的")
methodTest.printAll(list: _*) //把一个list集合当作一个变参传递给方法
methodTest.play
methodTest.method1("\n苍井空")
.method2("123")
println(methodTest.username)
println(methodTest.password)
}
}
}
import java.io.IOException
/**
* 方法的默认权限是public
* private[this] 对当前实例可见
* private 对当前类的所有实例可见
* protected 对当前类及其子类的实例可见
* private[packageName] 对packageName包下的所有子类可见
* public 公开的
* @param username
* @param password
*/
class MethodTest(var username: String, var password: String) {
/**
* 【作用域1】对象私有:是这个类的每个对象特有的,不同对象不能通用
* 返回值类型不是必须的
* 当没有参数的时候圆括号也不是必须
* 方法体简单的时候花括号也不是必须的
* @return
*/
private[this] def isMethodTest = true
/**
* 【作用域2】类私有:这个法方法对当前实例和当前类的其他实例都是可见的
* 类的私有方法对其子类,是不可见的
*
* @return
*/
private def isMethodTest1 = true
/**
* 【作用域3】保护作用域:类和他的子类的实例是可见的
*
* @return
*/
def isMethodTest2 = {
import TemplatePackage._
val dog = new Dog
var info = dog.breath
}
/**
* 【作用域3】包作用域:该包下的类是可见的
*
* @return
*/
private[Chapter5] def isMethodTest3 = {
println("private[Chapter5]")
}
/**
* 方法的默认参数值指定
*
* @param timeOut 500
* @param protocol http
*/
def makeConnection(timeOut: Int = 500, protocol: String = "http") {
println("timeout =%d,protocol=%s".format(timeOut, protocol))
}
/**
* 使用tuples从方法中返回多个值
*
* @return
*/
def getPeopleInfo = {
("苍井空", 23, "女", "日本", "东京")
}
/**
* 接受可变参数的方法
*
* @param strs
*/
def printAll(strs: String*) {
strs.foreach(print)
}
/**
* throws 注解:声明可能抛出的异常
*/
@throws(classOf[IOException])
def play {
printAll("x", "y")
}
/**
* scala 链式编程风格的写法
*/
def method1(firstName: String): this.type = {
username = firstName
this
}
def method2(password: String): this.type = {
this.password = password
this
}
/**
* scala定义一个方法的方式1
*
* @param x
* @param y
* @return
*/
def doSomeThing(x: String, y: Int): String = {
"其实我什么事都没有干"
}
/**
* scala 定义方法的方式2
* 返回值类型不是必须的,方法体很短的时候花括号也可以不写
*
* @param i
* @return
*/
def method1(i: Int) = i + 1
}
/**
*
*/
package TemplatePackage {
trait Human {
def hello = "Human特质"
}
trait Monther extends Human {
override def hello: String = "Monther特质"
}
trait Father extends Human {
override def hello: String = "father特质"
}
class Animal {
private[TemplatePackage] val message = "这是一个包级别的常量"
protected def breath: Unit = {
val name = this.getClass.getSimpleName
println(s"$name 中的 breath方法")
}
/**
* 这个是一个包级别的方法,同一个包下的其它类都可见
*/
private[TemplatePackage] def breath1 = {
}
}
class Dog extends Animal {
val name = this.getClass.getSimpleName
override def breath: Unit = {
println(s"$name 继承来的breath方法")
println(message)
}
}
/**
* scala 继承多个特质
*/
class Child extends Human with Monther with Father {
def printSuper = super.hello
def printMonther = super[Monther].hello
def printFather = super[Father].hello
def printHuman = super[Human].hello
/**
* 参数的复制荀勖默认是从左往右
* @param timeout
* @param protocol
*/
private[TemplatePackage] def getConnection(timeout: Int = 500, protocol: String = "https") {
println(s"timeout= $timeout protocol= $protocol")
}
}
object test {
def main(args: Array[String]): Unit = {
val methodTest = new MethodTest("方法", "测试类")
val child = new Child
println(child.hello)
println(child.printFather)
println(child.printHuman)
println(child.printSuper)
child.getConnection()
child.getConnection(500000)
child.getConnection(protocol = "udp")
child.getConnection(1000, "http")
val info = methodTest.getPeopleInfo
val tuple = info.copy()
println(tuple._1, tuple._2, tuple._3, tuple._4, tuple._5)
println(info._1, info._2, info._3, info._4, info._5)
methodTest.printAll("苍井空\n")
methodTest.printAll("苍井空", "158岁\n")
val list = List("苍井空", "158岁", "还是女的")
methodTest.printAll(list: _*) //把一个list集合当作一个变参传递给方法
methodTest.play
methodTest.method1("\n苍井空")
.method2("123")
println(methodTest.username)
println(methodTest.password)
}
}
}