복제본과 함께 프로토 타입을 사용하는 대신 클래스 개체를 사용할 수 있다고 읽었습니다. 하지만 그 대신 클래스 객체를 사용한다는 것이 무슨 뜻인지 이해하지 못합니까? 어떤 사람이 프로토 타입 패턴 대신 클래스 객체를 사용한다는 것을 이해한다면 누구나 예제를 줄 수 있습니까?프로토 타입 대신 Java 클래스 객체가 사용됩니까?
-1
A
답변
1
Java 5부터는 java.lang.Class
이 자체 유형에 대해 일반적입니다. 이것에 의해, 클래스는 형태 보증 된 방법으로 인스턴스를 생성 할 수 있습니다.
interface Calculator {
void setA(int a);
void setB(int b);
int compute();
Calculator copy();
}
class Adder implements Calculator {
private int a,b;
public void setA(int a) {this.a=a;}
public void setB(int b) {this.b=b;}
public int compute() {return a+b;}
public Calculator copy() {
Adder res = new Adder();
res.a = a;
res.b = b;
return res;
}
}
class Multiplier implements Calculator {
private int a,b;
public void setA(int a) {this.a=a;}
public void setB(int b) {this.b=b;}
public int compute() {return a*b;}
public Calculator copy() {
Multiplier res = new Multiplier();
res.a = a;
res.b = b;
return res;
}
}
class Test {
static void computeWithPrototype(Calculator proto) {
Calculator calc = proto.copy();
calc.setA(123);
calc.setB(321);
System.out.println(calc.compute());
}
public static void main(String[] args) throws Exception {
computeWithPrototype(new Adder());
computeWithPrototype(new Multiplier());
}
}
Demo of the above approach on ideone : 당신이 복제와 프로토 타입을 사용하는 경우
, 당신은 같은 것을 할.
당신은이 같은 대신 copy
방법의 Class<T>
로 다시 쓸 수 있습니다 :
interface Calculator {
void setA(int a);
void setB(int b);
int compute();
}
class Adder implements Calculator {
private int a,b;
public void setA(int a) {this.a=a;}
public void setB(int b) {this.b=b;}
public int compute() {return a+b;}
}
class Multiplier implements Calculator {
private int a,b;
public void setA(int a) {this.a=a;}
public void setB(int b) {this.b=b;}
public int compute() {return a*b;}
}
class Test {
static <T extends Calculator> void computeWithClass(Class<T> calcClass)
throws Exception {
Calculator calc = calcClass.newInstance();
calc.setA(123);
calc.setB(321);
System.out.println(calc.compute());
}
public static void main(String[] args) throws Exception {
computeWithClass(Adder.class);
computeWithClass(Multiplier.class);
}
}
0
자바에서는 객체를 생성 할 때 참조 메모리가 있습니다. 따라서 변수에 객체를 할당하려고하면 참조 메모리를 전달합니다.
예 :
당신이 모두 수정이 메모리 참조에 속하는 속성을 수정하여 이러한 이유로Person a = new Person(); a.setName("Person abc");
Person b = a; b.setName("Person yzw");
System.out.print(a.getName());
System.out.print(b.getName());
. 인쇄 예정 : "yzw yzw";
그래서 당신은 그것이 발생하는 것을 원하므로 Cloneable를 인터페이스를 사용하지 않는 경우 :
public class Person implements Cloneable{
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
그래서 당신은 클론() 메서드를 호출 할 때이 두 개의 서로 다른 목적이있을 것이다. 예 :
Person a = new Person();
a.setName("Person abc");
Person b = (Person)a.clone();
System.out.print(a.getName());
System.out.print(b.getName());
인쇄 할 항목 : "abc yzw";
관련 문제
- 1. 프로토 타입 대 클래스
- 2. JavaScript 객체가 프로토 타입 체인에서 상속받지 않습니다.
- 3. 프로토 타입 : 클래스 명
- 4. 프로토 타입 상속 및 프로토 타입 속성
- 5. 클래스 V 프로토 타입 속성
- 6. C++ 클래스 프로토 타입 충돌
- 7. 프로토 타입 클래스 클리어 입력
- 8. 프로토 타입 대신 클래스 구조가있는 Backbone.js가 뒤로 단계입니까? 아니요.
- 9. 프로토 타입
- 10. 프로토 타입
- 11. 프로토 타입
- 12. 프로토 타입 함수가 결과 대신 자체를 인쇄합니다.
- 13. iPhone 메소드 프로토 타입
- 14. 프로토 타입 및 프로토 타입 정보
- 15. 프로토 타입
- 16. 프로토 타입
- 17. 프로토 타입
- 18. 프로토 타입
- 19. 프로토 타입
- 20. 프로토 타입
- 21. 프로토 타입
- 22. 프로토 타입
- 23. 프로토 타입
- 24. 프로토 타입
- 25. 프로토 타입
- 26. 프로토 타입 기반 상속
- 27. 프로토 타입 세포 uitableview
- 28. 배열 프로토 타입
- 29. setInterval 이벤트로 클래스 프로토 타입 메서드 호출
- 30. JS에서 클래스 정의하기 (ES5 대 프로토 타입)
우리는 여기에 자바 나 자바 스크립트에 대해 말을하는거야? –
질문이 명확하지 않습니다. 명확히하십시오. –
읽은 기사에 대한 링크가 있습니까? 프로토 타이핑은 객체 계층 구조를 생성 할 때 솔루션이며 관련된 연산은 비싸거나 실현 가능하지 않습니다. 게임 상태의 예를 들어 봅시다 - 2 시간 동안 게임을하고 상태를 저장했다고합시다. 다시 시작할 때 2 시간 정도의 모든 작업을 실행하는 것보다 이전에 저장 한 상태를 복제하는 것이 더 쉽습니다. 따라서 읽은 내용을 참고하면 더 나은 대답을 얻을 수 있습니다. –