zoukankan      html  css  js  c++  java
  • Scala面向对象之创建对象,重载构造方法,继承抽象类实现接口

    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+"	"+_age
    }
    
    
    /**
      * 定义一个类,继承Person类,并且重写构造方法
      *
      */
    class Student2(name:String,age:Int,Clazz:String) extends Person(name,age){
      var _Clazz:String = Clazz
    
      override def toString: String = _name+"	"+_age+"	"+_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
    }
  • 相关阅读:
    Spring Mvc和Mybatis的多数据库访问配置过程
    Git下解决冲突
    安装Git
    数据库优化
    Ubuntu版 微信
    ssh框架简介
    写代码的习惯
    CentOS 7 安装 docker 并搭建私有仓库
    IPv4地址分类及特征
    Xcode 7.0 Could not find developer disk image
  • 原文地址:https://www.cnblogs.com/wyh-study/p/12217498.html
Copyright © 2011-2022 走看看