zoukankan      html  css  js  c++  java
  • scala 伴生对象与伴生类

     1 package cn.scala_base.oop.scalaobject
     2 
     3 import java.security.cert.Extension
     4 
     5 /**
     6  * object的构造器必须是无参的,且且构造器只会在第一次调用object时被调用
     7  * 同一个scala文件下同名的object与class成为伴生对象与伴生类可以互访各自的私有field
     8  */
     9 
    10 //伴生类
    11 class Person {
    12   private var age: Int = 20;
    13 }
    14 
    15 //定义一个抽象类
    16 
    17 abstract class abPerson(var message: String = "yeye") {
    18   //定义一个抽象方法
    19   def say(name: String): Unit;
    20 
    21 }
    22 
    23 //伴生对象
    24 object Person extends abPerson {
    25   private var name: String = "tele";
    26 
    27   println("object Person constuctor is used");
    28 
    29   def getName = name;
    30 
    31   //子类重写父类的抽象方法时,不需要使用override
    32   def say(name: String) = {
    33     println(message + "---hello--" + name);
    34   }
    35   def main(args: Array[String]): Unit = {
    36 
    37     /* println(Person.getName);
    38       println(Person.getName);*/
    39 
    40     //创建Person类的对象
    41     val p = new Person;
    42     //可以直接获取到
    43     println(p.age);
    44 
    45     //   println(Person.say("hhhh"));
    46 
    47   }
    48 }
  • 相关阅读:
    Postman提取接口返回值设置变量
    Python-浅拷贝与深拷贝
    Python列表
    typeorm查询两个没有关联关系的实体
    springboot去掉数据源自动加载
    docker搭建redis集群
    实习工作记录(一)大文件上传vue+WebUploader
    js重点之promise
    css重点
    git简单命令整理
  • 原文地址:https://www.cnblogs.com/tele-share/p/10039273.html
Copyright © 2011-2022 走看看