简体   繁体   English

关于 class 定义的混淆

[英]Confusion about class definitions

I am a little bit confused with the next terms.我对接下来的条款有点困惑。 I thing I got it right but programmers all the time say it wrong so I want to be sure.我觉得我做对了,但程序员总是说错,所以我想确定一下。

If we have class A and class B is inner class of class A. I hear a lot of programmers say "class A is parent of class B". If we have class A and class B is inner class of class A. I hear a lot of programmers say "class A is parent of class B". For me this is wrong.对我来说这是错误的。 Class A is outer class of class B. Class A would be parent class only if the class B inherits class A. Class A is outer class of class B. Class A would be parent class only if the class B inherits class A.

This is very confusing because (since I am Android developer) people also use this terms in defining layouts where they say:这非常令人困惑,因为(因为我是 Android 开发人员)人们也在定义布局时使用这些术语,他们说:

Imageview, TextView and Edittext are children of ConstraintLayout and ConstraintLayout is their parent. Imageview、TextView 和 Edittext 是 ConstraintLayout 的子级,而 ConstraintLayout 是它们的父级。 Even though I agree with this sentence it is contradictory to the previous example with classes.尽管我同意这句话,但它与前面的类示例相矛盾。 If we follow previous example we should say: ConstraintLayout is outer element and these 3 are its inner elements.如果我们按照前面的例子我们应该说:ConstraintLayout 是外部元素,这 3 个是它的内部元素。

Please can someone share opinion with me or confirm my doubts.请有人与我分享意见或确认我的疑问。 Thanks谢谢

Layout example in android: android中的布局示例:

<androidx.constraintlayout.widget.ConstraintLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".presentation.main.MainActivity">

     <ProgressBar
        android:id="@+id/progressBar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

  <TextView
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:ignore="RtlSymmetry" />

</androidx.constraintlayout.widget.ConstraintLayout>

When we have a class definition like this:当我们有这样的 class 定义时:

class A extends B {
    static class X {}
    class Y {}
}

Then, following standard terminology [ 1 ,2 ], you can say that:然后,按照标准术语 [ 1 ,2 ],您可以这样说:

  • Class A is a subclass of B Class AB子类
  • Class B is a superclass of A Class BA超类
  • Classes X and Y are nested classes of A XY类是A嵌套类
  • Class A is enclosing class of X and Y Class A封装XY的 class
  • Class X is static nested class of A Class XAstatic 嵌套 class
  • Class Y is inner class of A Class YA内class

According to Java Documentation , term "parent class" is synonymous to "superclass".根据Java 文档,术语“父类”是“超类”的同义词。 In practice though, it is used to mean many different things (eg "enclosing class", or "class of a parent field").但在实践中,它用于表示许多不同的事物(例如“封闭类”或“ parent字段的类”)。 Natural language is a messy thing.自然语言是一个混乱的东西。 People use different names to call the same thing, sometimes even when there is standard generally accepted terminology in place.人们使用不同的名字来称呼同一个东西,有时即使有标准的普遍接受的术语。 So the only thing you can do, is to be aware that "parent class", depending on a context can sometimes mean "superclass", sometimes "enclosing class", and sometimes something else.所以你唯一能做的就是意识到“父类”,取决于上下文,有时可能意味着“超类”,有时是“封闭类”,有时是其他东西。 If you want to be unambiguous always use term "superclass" instead of "parent class".如果您想明确,请始终使用术语“超类”而不是“父类”。

Some things need to be cleared up:有些事情需要澄清:

In the context of classes : suppose you have class A extending class B .的上下文中:假设您有 class A扩展 class B Class A is a superclass of class B and B is a subclass of A . Class A是 class B超类BA子类 Calling superclasses "parent classes" is much less used, but still accurate.将超类称为“父类”的使用要少得多,但仍然准确。

An inner class B of the outer class A is not a child class, as there is no hierarchical relation between those two.外部 class A的内部 class B不是子 class,因为这两者之间没有层次关系。

In the context of instances , however, the story is different.然而,在实例的上下文中,情况有所不同。 If you have a List<List<?>> is List a parent class of itself?如果您有List<List<?>>List本身的父 class 吗? No.不。

But suppose you have a但是假设你有一个

class Node {
    List<Node> children;

    void addChild(Node node) {
        children.add(node);
    }
}
Node a = new Node();
Node b = new Node();
a.addChild(b);

Now it makes sense to call a the parent node of b .现在调用a的父节点是有意义的b But this example is not in class context, it's instance context.但是这个例子不在 class 上下文中,它是实例上下文。

Those are two different contexts, and one should not mangle them.这是两种不同的背景,一个不应该破坏它们。

Imageview, TextView and Edittext are children of ConstraintLayout and ConstraintLayout is their parent. Imageview、TextView 和 Edittext 是 ConstraintLayout 的子级,而 ConstraintLayout 是它们的父级。 Even though I agree with this sentence it is contradictory to the previous example with classes.尽管我同意这句话,但它与前面的类示例相矛盾。 If we follow previous example we should say: ConstraintLayout is outer element and these 3 are its inner elements.如果我们按照前面的例子我们应该说:ConstraintLayout 是外部元素,这 3 个是它的内部元素。

You are thinking that parent/child is terminology exclusively used when talking about classes.您认为父/子是谈论课程时专门使用的术语。 That is incorrect.这是不正确的。 Many programming languages don't even have classes.许多编程语言甚至没有类。

What you're describing with the UI elements is more akin to a tree data structure.您使用 UI 元素描述的内容更类似于树数据结构。 In a tree, there are parent and child nodes.在树中,有父节点和子节点。 So ProgressBar is a child of ConstraintLayout in the tree.所以ProgressBar是树中ConstraintLayout的子级。 I don't know the history behind it, but I'd bet a tree data structure was thought of before classes even existed.我不知道它背后的历史,但我敢打赌,在类存在之前就已经想到了树数据结构。

In the context of Java classes, there is also a parent (superclass) or child (subclass) relationship.在 Java 类的上下文中,还存在父(超类)或子(子类)关系。 In this case: class B extends A , B is the child class, and A is the parent.在这种情况下: class B extends A , B 是子 class, A 是父。

Inner classes are a completely different thing.内部类是完全不同的东西。 An inner class is not a child of its enclosing class and doesn't seem relevant to this discussion.内部 class 不是其封闭 class 的子级,并且似乎与此讨论无关。 Anyone calling an inner class a child is using wrong terminology.任何将内部 class 称为孩子的人都在使用错误的术语。 I personally have never heard them referred to as child classes of their enclosing class.我个人从未听说过它们被称为封闭 class 的子类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM