当前位置:网站首页>Scala-day01- companion objects and HelloWorld
Scala-day01- companion objects and HelloWorld
2022-06-26 12:09:00 【There will always be daylight】
object-oriented programming , But static variables school Is actually called by the class name
initial scala
package chapter01
/*
object: keyword , Declare a singleton object ( Companion )
*/
object HelloWorld {
/*
main Method , From the outside, you can directly call the executed method
def Method name ( Parameter name : Parameter type ): Method return value type ={ Method body }
*/
def main(args: Array[String]): Unit = {
println("hello world")
System.out.println("hello world")
}
}
java:
public class Student {
private String name;
private Integer age;
private static String school = "atguigu";
public Student(String name,Integer age){
this.name = name;
this.age = age;
}
public void printInfo(){
System.out.println(this.name + " " + this.age + " " + Student.school);
}
public static void main(String[] args) {
Student alice = new Student("alice", 20);
Student bob = new Student("bob", 23);
alice.printInfo();
bob.printInfo();
}
}
Introducing companion objects , With the same name class Accompany each other , Remove java Medium static,school Attribute from student Object call
class Student Is equivalent to a constructor ,def Customize printInfo Refer to the above structure for function structure
main Function defined in object Student Inside
scala:
package chapter01
class Student(name:String,age:Int) {
def printInfo():Unit={
println(name + " " + age + " " + Student.school);
}
}
// Introducing companion objects
object Student{
val school:String = "atguigu"
def main(args: Array[String]): Unit = {
val alice = new Student("alice",20)
val bob = new Student("bob",20)
alice.printInfo()
bob.printInfo()
}
}
边栏推荐
- 深度学习中的FLOPs和Params如何计算
- Loggie encoding and newline character test
- 中国十大证券app排名 开户安全吗
- express在nodejs中的基本使用
- leetcode 715. Range module (hard)
- HUST network attack and defense practice | 6_ IOT device firmware security experiment | Experiment 2 MPU based IOT device attack mitigation technology
- NFS共享存储服务安装
- Ad - update the modified PCB package to the current PCB
- I want to know how the top ten securities firms open accounts? Is online account opening safe?
- Common problems and Thoughts on member operation management
猜你喜欢

HUST网络攻防实践|6_物联网设备固件安全实验|实验三 FreeRTOS-MPU 保护绕过
![In depth understanding of STM32 serial port experiment (register) [nanny level tutorial]](/img/b2/f09e220918a85b14a1993aa85f7720.png)
In depth understanding of STM32 serial port experiment (register) [nanny level tutorial]

MOS管基本原理,单片机重要知识点

科技兴关,荣联与天津海关共建基因组数据库及分析平台

19: Chapter 3: develop pass service: 2: get through Alibaba cloud SMS service in the program; (it only connects with Alibaba cloud SMS server, and does not involve specific business development)

4. N queen problem

再获认可!知道创宇入选“业务安全推进计划”首批成员单位

Jmeter响应时间和tps监听器使用教程

How to calculate flops and params in deep learning

FastRCNN
随机推荐
Quantitative elementary -- akshare obtains stock code, the simplest strategy
19: Chapter 3: develop pass service: 2: get through Alibaba cloud SMS service in the program; (it only connects with Alibaba cloud SMS server, and does not involve specific business development)
Is it safe to open a securities account
请指教同花顺开户选选择哪家券商比较好?手机开户安全么?
Change calico network mode to host GW
【Redis 系列】redis 学习十六,redis 字典(map) 及其核心编码结构
Is it safe to open a stock account by mobile phone
基于slate构建文档编辑器
Rookie practical UML - activity diagram
. Net, the usage of log components NLog, seriallog, log4net
Five trends of member marketing of consumer goods enterprises in the future
MOS管基本原理,单片机重要知识点
Lintcode 130 · stacking
杜比全景音效简介
FasterRCNN
Redis cannot connect to the server through port 6379
webgame开发中的文件解密
Assembly language (7) operation instruction
Prospering customs through science and technology, Ronglian and Tianjin Customs jointly build a genomic database and analysis platform
Re recognized! Know that Chuangyu has been selected as one of the first member units of the "business security promotion plan"