How to use a nested Java Builder Class to construct an object?

조회 수: 6 (최근 30일)
Riley Gibbs
Riley Gibbs 2015년 6월 29일
댓글: Andrew Janke 2017년 7월 28일
I'm currently trying to import and use a class that uses the Java Builder pattern and I can't seem to figure out how to instantiate an instance of the object. Below is my class setup and how I'd build it in Java for reference.
package testJavaBuilder;
public class NutritionalFacts {
private int sodium;
private int fat;
private int carbo;
public static class Builder {
private int sodium;
private int fat;
private int carbo;
public Builder(int s) {
this.sodium = s;
}
public Builder fat(int f) {
this.fat = f;
return this;
}
public Builder carbo(int c) {
this.carbo = c;
return this;
}
public NutritionalFacts build() {
return new NutritionalFacts(this);
}
}
private NutritionalFacts(Builder b) {
this.sodium = b.sodium;
this.fat = b.fat;
this.carbo = b.carbo;
}
}
NutritionalFacts facts = new NutritionalFacts.Builder(10).carbo(23).fat(1).build();
  댓글 수: 1
Andrew Janke
Andrew Janke 2017년 7월 28일
See https://www.mathworks.com/matlabcentral/answers/98620-how-do-i-create-an-instance-of-a-java-public-static-nested-class-from-inside-matlab-7-13-r2011b. You have to work around this by using Java Reflection calls or writing a custom Java adapter class.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Call Java from MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by