简体   繁体   English

返回类型 class 返回什么?

[英]What does a return type of class return?

Suppose I have a class called Practice.假设我有一个名为Practice.

public class Practice 
{
    public static void main(String[] args) 
    {
        test x= new test();
        System.out.println(x.testFunction());
    }

}   

And another class called Test另一个 class 叫Test

class Test 
{
    public Practice testFunction()
    {
        return xxxx;
    }
}

So since the return type is of class Practice, what could I replace xxxx with.所以既然返回类型是 class 实践,我可以用什么来代替 xxxx。 Like suppose the return type is int, so I am supposed to return a number, when the return type is string, I am supposed to return a sentence.比如假设返回类型是int,那么我应该返回一个数字,当返回类型是string时,我应该返回一个句子。 So when the return type is a class, what does it actually return.所以当返回类型是 class 时,它实际上返回了什么。

The return type would be an Object of type Practice.返回类型将是 Object,类型为 Practice。 Or in other words, an instance of the class Practice.或者换句话说,class 实践的一个实例。

Normally, you would create such an instance by using a constructor.通常,您会使用构造函数创建此类实例。

Eg using the default constructor: return new Practice();例如,使用默认构造函数: return new Practice();

Ideally an object of that specific class like this:理想情况下,特定 class 的 object 如下所示:

class Test 
{
    public Practice testFunction()
    {
        return new Practice();
    }
}

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

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