OOP 术语:类、属性、属性、字段、数据成员 [英] OOP Terminology: class, attribute, property, field, data member

查看:11
本文介绍了OOP 术语:类、属性、属性、字段、数据成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习 OOP,我想了解什么是类.我对某些核心元素的使用松散程度感到有些困惑,从而增加了我的困惑.

I am starting studying OOP and I want to learn what constitutes a class. I am a little confused at how loosely some core elements are being used and thus adding to my confusion.

我看过C++类,java类,想知道的足够写自己的伪类来帮助理解.

I have looked at the C++ class, the java class and I want to know enough to write my own pseudo class to help me understand.

例如在 这篇文章中我读到了这个(.. 类属性(或类属性,字段或数据成员)

For instance in this article I read this (.. class attribute (or class property, field, or data member)

我已经看到了一些很好的问题,这些问题表明类属性和类字段之间存在差异,例如 C#中的字段和属性有什么区别?

I have seen rather well cut out questions that show that there is a difference between class property and class field for instance What is the difference between a Field and a Property in C#?

根据我所学的语言,定义是什么

Depending on what language I am studying, is the definition of

  • 房产
  • 字段
  • 类变量
  • 属性

因语言而异?

推荐答案

字段"、类变量"和属性"是否或多或少相同 - 附加到对象的低级存储槽.每种语言的文档可能始终使用不同的术语,但大多数实际程序员可以互换使用它们.(然而,这也意味着一些术语可能是模棱两可的,例如类变量"——可以解释为给定类的实例的变量"或类对象本身的变量".在一种可以直接操作类对象的语言中.)

"Fields", "class variables", and "attributes" are more-or-less the same - a low-level storage slot attached to an object. Each language's documentation might use a different term consistently, but most actual programmers use them interchangeably. (However, this also means some of the terms can be ambiguous, like "class variable" - which can be interpreted as "a variable of an instance of a given class", or "a variable of the class object itself" in a language where class objects are something you can manipulate directly.)

属性"在我使用的大多数语言中,完全是另外一回事——它们是一种将自定义行为附加到读/写字段的方法.(或替换它.)

"Properties" are, in most languages I use, something else entirely - they're a way to attach custom behaviour to reading / writing a field. (Or to replace it.)

所以在 Java 中,典型的例子是:

So in Java, the canonical example would be:

class Circle {

    // The radius field
    private double radius;
    public Circle(double radius) {
        this.radius = radius;
    }

    // The radius property
    public double getRadius() {
        return radius;
    }
    public void setRadius(double radius) {
        // We're doing something else besides setting the field value in the 
        // property setter
        System.out.println("Setting radius to " + radius);
        this.radius = radius;
    }

    // The circumference property, which is read-only
    public double getCircumference() {
        // We're not even reading a field here.
        return 2 * Math.PI * radius;
    }

}

(请注意,在 Java 中,属性 foo 是一对称为 getFoo()setFoo() 的访问器方法 - 或者只是如果属性是只读的,则为 getter.)

(Note that in Java, a property foo is a pair of accessor methods called getFoo() and setFoo() - or just the getter if the property is read-only.)

另一种看待这一点的方式是属性";是一个抽象 - 对象承诺允许调用者获取或设置一段数据.而领域"等是这种抽象的一种可能的实现.上例中getRadius()getCircumference()的值可以直接存储,也可以计算出来,调用者无所谓;setter 可能有也可能没有副作用;调用者无所谓.

Another way of looking at this is that "properties" are an abstraction - a promise by an object to allow callers to get or set a piece of data. While "fields" etc. are one possible implementation of this abstraction. The values for getRadius() or getCircumference() in the above example could be stored directly, or they could be calculated, it doesn't matter to the caller; the setters might or might not have side effects; it doesn't matter to the caller.

这篇关于OOP 术语:类、属性、属性、字段、数据成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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