Skip to content

Instantly share code, notes, and snippets.

@gakuzzzz
Created September 13, 2012 05:11
Show Gist options
  • Save gakuzzzz/3712015 to your computer and use it in GitHub Desktop.
Save gakuzzzz/3712015 to your computer and use it in GitHub Desktop.
オブジェクト指向エクササイズのアクセサ禁止
// これはNG
public class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {return name;}
public int getAge() {return age;}
}
public class UserFinder {
public static void main(String[] args) {
User user = UserRepository.findByName(args[0]);
System.out.format("User(name=%s, age=%d)\n", user.getName(), user.getAge());
}
}
// こういう風にしろ
public class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public void display(PrintStream out) {
out.format("User(name=%s, age=%d)\n", user.getName(), user.getAge());
}
}
public class UserFinder {
public static void main(String[] args) {
User user = UserRepository.findByName(args[0]);
user.display(System.out)
}
}
@gakuzzzz
Copy link
Author

てst

@gakuzzzz
Copy link
Author

test

@gakuzzzz
Copy link
Author

testtest

@gakuzzzz
Copy link
Author

test

@gakuzzzz
Copy link
Author

test

@gakuzzzz
Copy link
Author

もっかいテスト

@gakuzzzz
Copy link
Author

test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment