This is the engineer t-77. I personally summarized what I thought was important in studying Java. This time, it's about class instance methods.
--One of the objects that are things or things that have not yet been materialized (blueprints for things). --Java code requires at least one class. --If you think of a class as one "thing", the contents (members) of the class are roughly divided into the following two. --A "field" that handles the state and properties of things --"Methods" that handle the functions of things --The class is basically described as "sample.java" below.
sample.java
//Class declaration
class Animal
{
//Describe the "field" that handles the state and properties of things in the class
//"Type name Field name;Described as
String name;
int num;
//Describe "method" that handles the function of the class
//"Return type method name(Argument list){Sentence;}Described as
void show() {
System.out.println("The name of the animal is" + name + "is.");
System.out.println("The registration number is" + num + "is.");
}
}
――One of the objects that are things or things, and is materialized (the real thing made from the blueprint). --You need to create an instance to use the declared class. --To create an instance, follow the steps below.
sample.java
//Class declaration
class Animal
{
String name;
int num;
void show() {
System.out.println("The name of the animal is" + name + "is.");
System.out.println("The registration number is" + num + "is.");
}
}
class Sample
{
public static void main(String[] args)
{
//Declare instance variables.
Animal animal1;
//Create an instance.
//When creating an instance, it becomes a "thing that can actually be handled".
animal1 = new Animal();
//You can declare and create an instance at the same time as follows.
// Animal animal1 = new Animal();
//Instance name.You can access the field by field name.
animal1.name = "dog";
animal1.num = "1";
//Instance name.You can access the method by method name.
animal1.show();
}
}
console
The name of the animal is dog.
The registration number is 1.
--Methods are the parts that handle (process) the functions of things. --When calling a method from outside the class, use "instance name.method name". --When calling a method in a class, use "method name" or "this.method name".
sample.java
//Class declaration
class Animal {
String name;
int num;
void show() {
System.out.println("The name of the animal is" + name + "is.");
System.out.println("The registration number is" + num + "is.");
}
void showAnimal() {
System.out.println("I will introduce animals.");
//You are calling your own method (calling from within the class).
// show()May be.
this.show();
}
}
class Sample {
public static void main(String[] args) {
Animal animal1;
animal1 = new Animal();
animal1.name = "dog";
animal1.num = "1";
//The method is called with the information of animal1 (calling from outside the class).
animal1.showAnimal();
}
}
console
I will introduce animals.
The name of the animal is dog.
The registration number is 1.
--You can pass arguments (values) to the method. --There are the following two types of arguments.
(1) Variable that receives the value in the method definition => Formal argument (2) Value to be passed when calling the method => Actual argument --Return value (information) can be passed from the method body to the method caller. --For methods with no return value, use void type. --There are two types of return values as follows.
(1) When the caller uses the return value
Return type Variable name = Object name. Method name (argument list);
(2) When the caller does not use the return value
Object name. Method name (argument list);
sample.java
//Class declaration
class Animal {
String name;
int num;
//Prepare String type and int type formal parameters.
//A method with no return value.
void show(String str, int n) {
//Make the argument value available in the method.
name = str;
num = n;
System.out.println("The name of the animal is" + str + "is.");
System.out.println("The registration number is" + n + "is.");
}
//A method whose return value is a String type.
String getAnimal() {
System.out.println("I will introduce animals.");
//Pass the return value to the caller.
return name;
}
}
class Sample {
public static void main(String[] args) {
Animal animal1 = new Animal();
String name = "Tiger";
//Pass the actual argument to the method with the information of animal1 (variables are also available).
//A pattern that does not use the return value at the caller.
animal1.show(name, 2);
//A pattern that uses the return value at the caller.
int number = animal1.getAnimal();
//Use the return value.
System.out.println(name + "Registration number is" + number + "is.");
}
}
console
The name of the animal is tiger.
The registration number is 2.
I will introduce animals.
The tiger registration number is 2.
--Access restrictions can be set for members. --The "private" member cannot be accessed from outside the class. --The "public" member can be accessed from outside the class.
--A function to set access restrictions by grouping data (fields) and functions (methods) in a class and adding "private" to the members you want to protect.
sample.java
class class name{
//Not accessible from outside the class.
private type name field name;
//Accessable from outside the class.
public void method name{
formula;
}
}
--Define a method with the same method name but different argument types and numbers in the same class. --If you overload only by the difference in the return type, you will not know which one the program should access. Therefore, when overloading, be sure to "have different argument types and numbers".
sample.java
class Animal {
String name;
int num;
//A method with one argument.
void show(String str) {
name = str;
System.out.println("The name of the animal is" + str + "is.");
}
//A method with two arguments.
void show(String str, int n) {
name = str;
num = n;
System.out.println("The name of the animal is" + str + "is.");
System.out.println("The registration number is" + n + "is.");
}
}
class Sample {
public static void main(String[] args) {
Animal animal1 = new Animal();
String name = "Tiger";
int num = 2;
//Call a method with one argument.
animal1.show(name);
//Call a method with two arguments.
animal1.show(name, num);
}
}
console
The name of the animal is tiger.
The name of the animal is tiger.
The registration number is 2.
This time is over.
Recommended Posts