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 }
  • 相关阅读:
    c#读取.config文件内容
    c# 读取配置文件方法
    C# Log4net详细说明
    C# 运算符集
    LeetCode 69_ x 的平方根
    LeetCode 172 _ 阶乘后的零
    LeetCode 171 _ Excel表列序号
    LeetCode 88 _ 合并两个有序数组
    LeetCode 581 _ 最短无序连续子数组
    LeetCode 283 _ 移动零
  • 原文地址:https://www.cnblogs.com/tele-share/p/10039273.html
Copyright © 2011-2022 走看看