Kotlin构造函数:主要和次要 [英] Kotlin constructors: primary and secondary

查看:202
本文介绍了Kotlin构造函数:主要和次要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Kotlin 开始,您可以在其中拥有 主要 构造函数和 第二 .这个问题听起来很简单,但是我找不到答案(我已经阅读了文档中的构造函数"部分 )-为什么?

Just have started with Kotlin, where you can have a primary constructor and secondary ones. The question may sound simple, but I couldn't find an answer to it (I've read the "constructors" section in the documentation ) - why?

基本上,我试图了解成为主要和次要的背后的想法是什么.以及如何使用它们有什么区别(好像没有,所以为什么要分开)?

Basically, I'm trying to understand what is the idea behind being primary and secondary. And what is the difference in how they are used (seems like there's not, so why the separation)?

推荐答案

很明显,这是各种语法差异.但主要的概念差异是所有辅助构造函数最终都委托给主要构造函数.

The are various syntactic differences, clearly. But a major conceptual difference is that all secondary constructors ultimately delegate to the primary constructor.

的思考方式是,主要构造函数是用于创建对象的规范接口,而次要构造函数就像静态助手,用于转换其他参数集以使其符合该接口. > *

The way I think about this is that the primary constructor is the canonical interface for creating an object, and secondary constructors are like static helpers for transforming other argument sets to comply with this interface.*

*请注意,这是个人解释,不以任何方式得到官方文档的支持!

这篇关于Kotlin构造函数:主要和次要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆