어쩌면 누군가가 코드를 작성하는 것이 더 낫다는 것을 알 수 있습니다 (CPU 또는 GPU에 대해 가장 적은 메모리, 가장 빠른 방법).JAVA 다중 생성자와 IF의 상속 비교
첫 번째 방법 : 여러 생성자가있는 모든 인스턴스에 대해 하나의 클래스를 작성하고 어떤 매개 변수가 있는지 "IF"검사를합니까? 예 :
class SomeClass
{
Parameter a;
Parameter b;
Parameter c;
SomeClass(Parameter a)
{
this.a=a;
}
SomeClass(Parameter a,Parameter b)
{
this(a);
this.b=b;
}
SomeClass(Parameter a,Parameter b,Parameter c)
{
this(a,b);
this.c=c;
}
public void method()
{
if(a!=null && b!=null && c!=null)
DO IN ONE WAY
if(a!=null && b!=null && c==null)
DO IN ANOTHER WAY
if(a!=null && b!=null && c==null)
DO IN ANOTHER WAY
if(a!=null && b==null && c==null)
DO IN ANOTHER WAY
...
}
}
두번째 방법 : 당신이 더 많은 매개 변수를 사용할 필요가있을 때 는 부모 클래스마다 inherid 상위 클래스를 확장합니다. 예 :
class SomeClass
{
protected Parameter a;
SomeClass(Parameter a)
{
this.a=a;
}
public void method()
{USE a parameter}
}
class SomeClass2 extends SomeClass
{
protected Parameter b;
SomeClass2(Parameter a,Parameter b)
{
super(a);
this.b=b;
}
public void method()
{USE a and b parameter}
}
어떤 방법이 더 좋고 왜 그런지 말해 주시겠습니까?
는
좋은 독서 책 로버트 마틴의 "클린 코드"가 될 것이라고는 ... http://codereview.stackexchange.com/](http://codereview [에 요청 .stackexchange.com /) – Jens
이것이 http://codereview.stackexchange.com에서 더 잘 논의되는지 아닌지는 잘 모르겠습니다. –
차이점은 매우 작으므로 성능을 기준으로 결정하지 않아야합니다. 어떤 코드가 더 읽기 쉽고 확장 가능하며 일반적으로 더 합리적인 지에 따라 결정해야합니다. – Eran