LoginSignup
0
0

More than 1 year has passed since last update.

Javaを基本からまとめてみた【オブジェクトの使い方】

Last updated at Posted at 2023-03-07

オブジェクトの使い方

①オブジェクトを生成する

クラス名 オブジェクト名 = new クラス名();
例)
Student stu1 = new Student();
//newを使うとStudentクラスから作ったオブジェクトのメモリ領域を確保してくれる
Student
name : String
engScore : int
mathScore : int
display() : void
setScore(int, int) : void
setAvg() : double
プログラムにおいてオブジェクトを作ることを『インスタンス化』という
プログラムの中で実際にできたオブジェクトを『インスタンス』という

②変数・メソッドを利用する

オブジェクト名.変数名
オブジェクト名.メソッド名(引数)
例)
stu1.name = "Michael ";
stu1.setScore = (80,92);
Sample.java
public class StuSample {
	public static void main(String[] args) {
		Student stu1 = new Student();
		stu1.name = "Michael ";
		stu1.setScore = (80,92);

	   stu1.display ();
		System.out.println("平均" + stu1.getAvg() + "点");
	}
}

参考サイト

インスタンス化の方法とメモリ内部の動きを解説!【Java入門講座】3-3 オブジェクトの使い方

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0