如何在单个构造函数中的 if/else 语句中传递 “type” 参数?

huangapple go评论71阅读模式
英文:

How should I pass the "type" parameter of in a single constructor in an if/else statement?

问题

以下是翻译好的部分:

如何在 if/else 语句中传递构造函数的 "type" 参数例如 - cal(2,2,0,rectangle)所以如果 type=rectangle则计算矩形的面积如果 type=circle则计算圆的面积

我正在使用单个构造函数我的问题是我知道逻辑但无法以语法形式编写它我正在使用 Java 或 Apex

我想使用 if-else 语句在代码中如何传递 type 参数

我的程序如下 -

1. 如果 "type"=square则编译器将调用计算正方形的面积

2. 如果 "type"=circle则编译器将调用计算圆的面积

public class Area {

    private String type;
    private Integer length;
    private Integer breadth;
    private Integer height;
    private Integer area;

    public void setType(String t){
        type=t;    
    }
    public void setLength(Integer l){
        length=l;    
    }
    public void setbreadth(Integer b){
        breadth=b;    
    }
    public void setheight(Integer h){
        height=h;    
    }
    /* public void setArea(Integer a){
        area=a;    
    } */

    public Integer getLength(){
        return length;  
    }
    public Integer getbreadth(){
        return breadth;  
    }
    public Integer getheight(){
        return height;  
    }
    public string gettype(){
        return type;  
    }

    public Integer AreaRectangle(){
        return area=length*breadth;
    }

    public Integer AreaSquare(){
        return area=length*length;
    }

    public integer AreaTriangle(){
        return area=1/2 *(breadth*height);
    }

    public Area(){ // 默认构造函数
        length=9;
        breadth=2;
        height=7;       
    }
    public Area(String t,Integer l ,Integer b,Integer h ){         // 带参数的构造函数
        type=t;
        length=l;
        breadth=b;
        height=h;          
    }  
}
英文:

How should I pass the "type" parameter of a constructor in an if/else statement? For eg - cal(2,2,0,rectangle). So if the type=rectangle then calculate area of a rectangle. If type=circle, calculate the area of a circle.

I am using a single constructor. My issue is that I know the logic but I can't write it in syntax. I am using Java or Apex.

I want to use if-else statement. How should I pass the type parameter in the code?

My program is like this -

  1. if "type"=square, the compiler will call calculate area of the square.

  2. if "type"=circle, the compiler will call calculate area of the circle.


public class Area {
private String type;
private Integer length;
private Integer breadth;
private Integer height;
private Integer area;
public void setType(String t){
type=t;    
}
public void setLength(Integer l){
length=l;    
}
public void setbreadth(Integer b){
breadth=b;    
}
public void setheight(Integer h){
height=h;    
}
/* public void setArea(Integer a){
area=a;    
} */
public Integer getLength(){
return length;  
}
public Integer getbreadth(){
return breadth;  
}
public Integer getheight(){
return height;  
}
public string gettype(){
return type;  
}
public Integer AreaRectangle(){
return area=length*breadth;
}
public Integer AreaSquare(){
return area=length*length;
}
public integer AreaTriangle(){
return area=1/2 *(breadth*height);
}
public Area(){ // default constructor
length=9;
breadth=2;
height=7;       
}
public Area(String t,Integer l ,Integer b,Integer h ){         // parameterised constructor
type=t;
length=l;
breadth=b;
height=h;
}  
}

答案1

得分: 4

你不需要。你创建一个名为"Shape"的抽象类。

public abstract class Shape {
    abstract double area();  
}

然后创建另外两个类,它们分别扩展了Shape,并提供了适当的实现。

public class Square extends Shape {
    private int side;
    public Square(int side) {
        this.side = side;
    }
    public double area() {
        return (double) side * side;
    }
}

现在,在你想调用它的地方:

Shape shape = new Square(5);
double area = shape.area();

int radius = 4;
shape = new Circle(radius);
double circleArea = shape.area();
英文:

You don't. You create an abstract class called shape.

public abstract class Shape {
abstract double area();  
}

And then two other classes that extend Shape and each provides the proper implementation

public class Square extends Shape {
private int side;
public Square(int side) {
this.side = side;
}
public double area() {
return (double) side * side;
}
}

Now at the place you want to call it:

Shape shape = new Square(5);
double area = shape.area();
Int radius = 4;
shape = new Circle(radius);
double circle area = shape.area();

huangapple
  • 本文由 发表于 2020年9月5日 14:41:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63751174.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定