如何将Groovy的课程理解为一流的公民

编程入门 行业动态 更新时间:2024-10-24 06:25:06
本文介绍了如何将Groovy的课程理解为一流的公民的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是一名经验丰富的Java开发人员,但是一名新手Groovy程序员,目前我正在学习(迄今为止这非常棒)。作为参考,我正在阅读这份文件:

groovy.codehaus/Groovy+style+and+language+feature+guidelines+for+Java+developers

这很好,除了我不完全明白的一件事。文档中说类是一等公民,Groovy中不需要 .class 后缀。但是,如果是这种情况,我怎么才能在Groovy中引用对象的类型(即类)?

请考虑以下示例:

def o = new Object() println($ o,$ {o.class})

> java.lang.Object@da479dd,类java.lang.Object

预期输出感。但是当Groovy说他们说不需要 .class 后缀时,它指的是什么?

解决方案在Groovy和其他许多动态语言中,一切都是一个对象,包括类本身。

假设你在java中有一个Circle类。你需要调用Circle.getClass()来处理类对象。在许多动态语言中,类本身不需要指定。假设你有一堂课

class Miu {}

,并且以后每次引用Miu时都会引用类对象本身

Miu.class Miu

都会评估为同一个对象

换句话说,C ++中的Java和早期版本没有eval(),所以类定义本身不能直接作为类对象。 OO模型更像是面向类的编程,而不是真正的面向对象,因为类是微妙的不是对象。在最近解释的动态语言中,类本身就是直接的对象。

I am an experienced Java developer, but a novice Groovy programmer, which I am learning at the moment (and it's great so far). As a reference I am reading this document:

groovy.codehaus/Groovy+style+and+language+feature+guidelines+for+Java+developers

That is all fine, except one thing I do not fully understand. The documentation says that classes are first-class citizens and the .class suffix is not needed in Groovy. But if that is the case how can I then refer to an object's type (i.e. class) in Groovy?

Consider the following example:

def o = new Object() println("$o, ${o.class}")

Which gives me the following output:

java.lang.Object@da479dd, class java.lang.Object

This output is expected and makes sense. But what is the Groovy documentation than referring to when they say that the .class suffix is not needed?

解决方案

In Groovy and many other dynamic languages everything is an object, including class itself.

Say you have a class Circle in java. You need to call Circle.getClass() for an class object do deal with. In many dynamic languages, class itself does not need to be specified. Say you have a class

class Miu {}

and each later reference to Miu will be referencing to the class object itself

Miu.class Miu

will both evaluate to the very same object

In other words, Java and earlier in C++ had no eval(), so class definition itself cannot be made into class object directly. OO model in them is more like class oriented programming rather than true object oriented, as classes are subtly not objects. In more recent interpreted dynamic languages classes themselves are directly objects.

更多推荐

如何将Groovy的课程理解为一流的公民

本文发布于:2023-11-06 22:39:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564871.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   公民   课程   Groovy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!