current position:Home>Java class related problems

Java class related problems

2022-02-03 01:13:29 CSDN Q & A

What is the meaning of the class name as the return value

img


The output of this is that students learn

img


It is the same as this result
I can't figure out what the meaning of class name as return value is
He is direct new Student Isn't class more labor-saving
Class names are easy to understand as methods , As a return value, I always feel strange
Words , The process of creating objects is just opening up space , Why don't you instantiate when you return a value




Refer to the answer 1:

You this getStudent The overall meaning of the method is

//  Create a return Student Object method public StudentgetStudent(){        //  Create a Student object s        Student s=new Student()        //  Return to new object s        return s}       



Refer to the answer 2:



Refer to the answer 3:

It depends on the specific scene
This really doesn't make any more sense in your code
If you write like this , You can make sure you get the same... Every time Student

public Teacher{    Student student = null;    public Student getStudent(){        if (student == null){            student = new Student();        }        return student;    }}



Refer to the answer 4:



Refer to the answer 5:

Take a scene , Generally, this return type is an interface ,Teacher What is returned is the implementation class of this interface , As for what is written in this implementation class, you don't have to worry about , You just call it according to the method provided by the interface , polymorphic .
Programming is a multi-party project , When you call a third party provided sdk Or interface , You don't need to focus on how others achieve , Just bring it here .




Refer to the answer 6:



Refer to the answer 7:

This is an exercise , I'm teaching you how to treat a class as a parameter or return value
Just like you learn math in primary school , It must be from 1+1 Start learning
You can't feel like learning 1+1 It's no use




Refer to the answer 8:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202030113267951.html

Random recommended