Created
July 28, 2010 09:58
-
-
Save johobemax/493909 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* カプセル化のサンプル | |
* クラスLadyのカプセル化 | |
*/ | |
class Lady | |
{ | |
private int age; //年齢 | |
Lady(int _age) | |
{ | |
age = _age; | |
} | |
int ansAge() //年齢を答える | |
{ | |
if(age >= 30){ | |
return age - 5; //5歳さばよんで答える | |
}else{ | |
return age; //そのまま答える | |
} | |
} | |
} | |
public class LadyTest | |
{ | |
public static void main(String[] args) | |
{ | |
Lady tomiko = new Lady(40); | |
Lady masako = new Lady(18); | |
System.out.println( tomiko.ansAge() ); //tomikoが年齢を答える | |
System.out.println( masako.ansAge() ); //masakoが年齢を答える | |
//System.out.println( tomiko.age ); //Ladyクラスの private int age; にはアクセスできない | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment