0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

privateで修飾されたフィールド、メソッドへのアクセス

Posted at

アクセス方法
publicで修飾されたメソッド経由でアクセス

Test.java
package test;

public class Test {
	
	private int num;

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}
	
	private int calc(int x,int y) {
		
		int f = getNum();
		int result = x * y + f;
		
		return result;
		
	}
	
	public int calc2(int w,int z) { //calcメソッドアクセス用
		
		calc(w,z); 
  //privateには同じクラス内からのみアクセスできる
  //引数w,zに入れられた値はcalcメソッドのx,yに入る
		
		return calc(w,z);
	}

}


Main.java
Test test = new Test();
test.setNum(100); 
System.out.println(test.calc2(3,4));//calcメソッドが実行され112と表示
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?