package com.wyh.day01
object ScalaClass {
def main(args: Array[String]): Unit = {
val student = new Person("王友虎",21)
var result = student.toString
println(result)
val student1 = new Student2("赵以浩",22,"006")
val result1 = student1.toString
println(result1)
val inst = new Inst
println(inst.toInt("12"))
println(inst.toDouble("45"))
}
}
/**
*
* 定义一个类,包括参数
* 1、scala中的类参数写在类上
* 2、可以重写方法
*/
class Person(name:String,age:Int){
var _name: String = name
var _age:Int = age
override def toString: String = _name+"\t"+_age
}
/**
* 定义一个类,继承Person类,并且重写构造方法
*
*/
class Student2(name:String,age:Int,Clazz:String) extends Person(name,age){
var _Clazz:String = Clazz
override def toString: String = _name+"\t"+_age+"\t"+_Clazz
}
/**
* 定义抽象类
*
*/
abstract class abs(){
//定义抽象方法
def toInt(Str:String):Int
}
/**
*
* 定义接口
*/
trait Tr{
def toDouble(str:String):Double
}
/**
* 定义类继承抽象类,实现接口
*
* 注意,scala中的实现接口是with
*/
class Inst extends abs with Tr {
override def toInt(Str: String): Int = Integer.parseInt(Str)
override def toDouble(str: String): Double = str.toDouble
}