An orientation that exists so that programs can be easily assembled and modified. A form that makes each person introduce themselves as if they were in the program.
It can be divided into information and behavior. The types of information include name and age. The types of behavior include hello and walk.
Means the blueprint for an instance.
When creating an instance, it is basically assigned to a variable and used. "Class type variable name = new class name ();"
A variable that stores information. It is basically defined at the top of the class.
class Person{
public String name;
}
As a way to call
/*<Main.java>At*/
...
Person person1 = new Person();
person1.name = "Suzuki";
System.out.println(person1.name);
As a general definition method ↓
class Person {
public void hello(){ //Be careful because "static" is not used
System.out.println("Hello");
}
}
As a way to call
/*<Main.java>At*/
Person person1 = new Person();
Person person2 = new Person();
person1.hello();
person2.hello();
/*Note that it can only be called after the instance has been created!*/
By using this.variable name, it can be specified as a variable of this class. [Example]
class Person{
public String name;
public void hello(){
System.out.println ("Hello"+ this.name + "is");
/*Here, it will be replaced with the following "Suzuki" ↑*/
}
}
/*<Main.java>At*/
Person person = new Person();
person.name = "Suzuki";
person.hello(); //← Output here!
A method that is automatically called after an instance is created using new. Be careful because there are the following rules as definitions. <1> Constructor name has the same class name <2> Do not write the return value! (also void)
class Person{
public String name;
Person (String name){
/*↑ Constructor name*/
this.name = name;
}
}
How to call ↓
/*<Main.java>At*/
Person person = new Person("Suzuki");
System.out.println(person.name);
class Person{ /*First defined at the top of the class*/
public String firstName;
public String lastName;
public int age;
public double height;
public double weight;
/*Define multiple in the constructor and be careful not to make a mistake in the order!*/
Person(String firstName,String lastName,int age, double height, double weight){
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.height = height;
this.weight = weight;
}
public String fullName(){
return this.firstName + " " + this.lastName;
}
public double bmi(){
return this.weight/this.height/this.height;
}
/*Define printData for output! !! ↓*/
public void printData(){
System.out.println("my name is"+this.fullName()+"is");
System.out.println("Age is"+this.age+"I'm old");
System.out.println("BMI"+Math.round(this.bmi())+"is");
}/*After fullName and bmi()Don't forget! / Math.round is for rounding*/
}
Call ↓
/*<Main.java>At*/
...
Person person1=new Person("Kate","Jones",27,1.6,50.0);
person1.printData();
...
The overload here means When person1 has no middle name and person2 has a middle name It can be solved by defining two constructors. [Example]
class Person{
//...Abbreviation
public String middleName; //Plus where the instance field is defined.
Person(String firstName,String lastName, ...); //First pattern
//...* Omitted
Person(String firstName,String middleName,String lastName, ...); //Second pattern
//...* Omitted
The above "* omitted" is almost duplicated, so the second one should be abbreviated.
...
this(firstName,lastName,...weight); //Call other constructors here
this.middleName = middleName; //Write what you want to add here
・ Because it is difficult to decide whether to add () or not. I will write while checking carefully. ・ In the previous article, I received advice on how to use space and indentation. I wanted to get into the habit.
Recommended Posts