本文介绍了当父类似乎没有改变任何属性时,两次声明一个方法背后的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在查看这些各自的类(和子类)...

So I am looking at these respective classes (and subclasses)...

public class Control{
    public Control(){}
    public Control(String name, String type, ContainerControl owner){
        //do stuff here
    }
}


public class ContainerControl extends Control{
    public ContainerControl() { super(); }
    public ContainerControl(String name, String type, ContainerControl owner)
    {
        super(name, type, owner);
        controls = new Controls(); //exists somewhere else 
    }
}

public class SSTab extends ContainerControl{
    public SSTab(){ super(); }
    public SSTab(String name, String value, ContainerControl owner)
    {
        super(name, value, owner);
    }
}

我对使用 super () 的没有参数的方法声明背后的目的感到困惑.根据我所做的研究,我倾向于相信它们对于使用来自各自父类的属性重载方法或覆盖方法是必要的.但是,当他们调用 super() 时,据我所知,它会在超类中找到无参数方法(它再次做同样的事情),我看不出实际上会发生什么变化.

I am confused about the purpose behind the method declarations with no parameters which use super (). From the research I have done, I would be inclined to believe that they were necessary for either overloading the methods with properties from respective parent classes, or overriding. However, as they are calling super() which to my understanding would find the parameter-less method in the super class (which once again does the same thing), I don't see what could actually be getting changed.

作为包含大量子类的大型项目的一部分,我不能只从每个类中删除第一个方法声明并运行以查看会发生什么(以确定是否有目的),而且我可以似乎没有找到解释这些使用 super 的初始方法声明实际上在做什么的参考.

As a result of being part of a huge project with massive amounts of subclasses I can't just remove the first method declarations from each class and run to see what happens (to identify whether there is a purpose), and I can't seem to find a reference to explain what these initial method declarations which use super are actually doing.

任何帮助或链接将不胜感激!

Any help or links would be incredibly appreciated!

后续问题:

为什么SSTab不能只用CControl的参数初始化,而CControl可以用参数初始化?当存在带有调用 super 的参数的构造函数变量时,我不明白无参数构造函数变量是如何初始化对象种类的必要条件.

Why can't SSTab just be initialized with parameters with CControl which can be initialized with parameters with Control? I don't see how the parameterless constructor variables are necessary to initialize the kinds of objects, when there are constructor variables with parameters which call super anyways.

推荐答案

这是构造函数重载;构造函数不是方法.

This is Constructor Overloading; a constructor is not a method.

我不确定控件是在哪里定义的,但我很确定这是一个错字.在任何情况下,Super 都用于调用方法/构造函数的父版本.子类的构造函数中始终需要它,例如您提供的示例.可以这样想:

I'm not sure where controls is defined, but i'm pretty sure that's a typo. In any case, Super is used to call the parent version of a method/constructor. It's always needed in a constructor for subclasses such as the example you provided. Think of it like this:

SSTab 是一个 ContainerControl 对象;所以为了制作一个 SSTab,你首先必须制作一个 ContainerControl.但是,CControl 是一个 Control 对象.因此,您必须首先制作一个控制对象.有点像,你出生是因为你的父母出生.他们出生是因为他们的父母出生.

SSTab IS A ContainerControl object; so in order to make an SSTab you first have to make a ContainerControl. However, CControl IS A Control object. Therefore, you must first make a Control Object. Kind of like, you were born because your parents were born. They were born because THIER parents were born.

后续回答:

我可以说(根据我对您问题的理解)这有点像要遵循某些法律.例如,在现实生活中,如果您没有律师,则会为您提供一名律师.如果您调用 SSTab(param x, y z),并且不调用 super 函数(使用 params x y z),则将向您提供默认的函数(这将创建一个空的 CControl 和一个空的控件).因为你没有在 SSTab 中定义使用另一个,它会使用默认的,这并不总是你想要做的

What I can say (based on my understanding of your question) is that this is kind of like how there are certain laws to follow. For example, in real life, if you don't have a lawyer one will be provided to you. If you call SSTab(param x, y z), and you don't call the super function (with params x y z), then the default one will be provided to you (which will create an empty CControl, and an empty control). Because you did not define inside SSTab to use the other one, it will use the default one and this is not always what you want to do

这篇关于当父类似乎没有改变任何属性时,两次声明一个方法背后的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 08:39