While reading the introductory text, I thought, "Isn't it convenient if I can return two or more return values?", So I tried writing while checking. Note so that you don't forget it. (There seems to be a method called Beans and list, but I will study ...)
I took the method of returning all the values in one array variable or class type variable. In the end, I thought, "Isn't it better to increase the number of methods and return them one by one?"
--When multiple return values are of the same type -** How to return as an array of basic data type ** --When multiple return values are atypical -** How to return as an array of Object type ** -** How to return by class type **
Given the access restrictions, I think the methods are actually more limited.
The idea is that if there are multiple return values that you want to return and they are all of the same type, you can put them in the basic data type. You can forcibly insert an int type into a double type, but you don't want to do that, right? I thought, so I'm not thinking about it in that case.
Sub.java
public class Sub {
private int A = 100;
private int B = 200;
public int[] getAB(){
int[] array = new int[2];
array[0] = A;
array[1] = B;
return array;
}
}
Main.java
public class Main {
public static void main(String[] args){
Sub sub = new Sub();
int[] array = sub.getAB(); //Here, the int array is returned from the Sub class.
System.out.println(array[0]); //output
System.out.println(array[1]);
}
}
Execution result.
100
200
--How to return as Object type with the same idea as above --How to call a field by returning it as a class type
The part that was written as ʻint [] above became ʻObject []
. The idea is that even atypical values can be stored together if it is an Object type array.
Sub.java
public class Sub {
private int A = 100;
private double B = 200.0;
public Object[] getAB() {
Object[] array = new Object[2];
array[0] = A;
array[1] = B;
return array;
}
}
Main.java
public class Main {
public static void main(String[] args) {
Sub sub = new Sub();
Object[] array = sub.getAB();
System.out.println(array[0]);
System.out.println(array[1]);
}
}
Execution result.
100
200.0
Is it the best method I've written in this article?
The idea is that after returning an object, you can call the field of that object with the main method. It can be done, but the drawback is that access cannot be restricted using access modifiers.
Sub.java
public class Sub {
int circleAreaInt;
double circleAreaDouble;
public Sub calcCircleArea(int r) {
Sub sub = new Sub();
sub.circleAreaInt = 3 * r * r ;
sub.circleAreaDouble = 3.14 * r * r;
return sub;
}
}
Main.java
public class Main {
public static void main(String[] args) {
Sub sub = new Sub();
sub = sub.calcCircleArea(2);
System.out.println(sub.circleAreaInt);
System.out.println(sub.circleAreaDouble);
}
}
Execution result.
12
12.56
If you pass the radius r
of the circle as an argument (int), the method will calculate and return the area of the circle with both the pi (3) of the free generation and the pi (3.14) of the non-clear generation. ..... But I couldn't think of a good example. You don't have to write it this way.
[Easy Java 7th Edition] (https://www.amazon.co.jp/%E3%82%84%E3%81%95%E3%81%97%E3%81%84Java-%E7%AC%AC7%E7%89%88-%E3%80%8C%E3%82%84%E3%81%95%E3%81%97%E3%81%84%E3%80%8D%E3%82%B7%E3%83%AA%E3%83%BC%E3%82%BA-%E9%AB%98%E6%A9%8B-%E9%BA%BB%E5%A5%88/dp/4815600848) Pp.251
Recommended Posts