我正在尝试编写一个使用Factory模式计算出形状区域的程序。
我尝试使用调试器,但是我真的是编程新手,所以不要一头雾水。我没有错误消息或红线。 MainActivity:
package com.example.shape;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ShapeFactory myShapeFactory;
Shape myShape;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myShapeFactory = new ShapeFactory();
}
public void convert(View view) {
//This tell it to collect the height variable from user input
EditText heightEditText = findViewById(R.id.heightEditText);
String getHeight = heightEditText.getText().toString();
myShape.setHeight(Double.parseDouble(getHeight));
//This tell it to collect the height variable from user input pass as String, then accept into shape as double for width variable
EditText widthEditText = findViewById(R.id.widthEditText);
String getWidth = widthEditText.getText().toString();
myShape.setWidth(Double.parseDouble(getWidth));
//This tell is to allow the user to choose if the area is for a triangle or square
EditText shapeTypeEditText = findViewById(R.id.shapeTypeEditText);
String ShapeType = shapeTypeEditText.getText().toString();
//This states if T is entered to display the area of a Triangle
myShape = myShapeFactory.getShape("T");
Toast.makeText(this, Double.toString(myShape.Area()), Toast.LENGTH_LONG).show();
//This states if S is entered to display the area of a Square
myShape = myShapeFactory.getShape("S");
Toast.makeText(this, Double.toString(myShape.Area()), Toast.LENGTH_LONG).show();
}
}
类形状:
package com.example.shape;
public abstract class Shape {
//set variable types to double
private double width;
private double height;
//tells it to get the variable width from input
public double getWidth () {
return width;
}
//tells it to set the variable width from input
public void setWidth ( double _width) {
width = _width;
}
public double getHeight () {
return height;
}
public void setHeight(double _height){
height = _height;
}
public abstract double Area();
}
package com.example.shape;
public class ShapeFactory {
public Shape getShape(String ShapeType) {
if (ShapeType==null) {
return null;
}
if (ShapeType=="T") {
return new Triangle();
}
if (ShapeType=="S") {
return new Square();
}
return null;
}
}
----------
package com.example.shape;
public class Square extends Shape {
@Override
public double getArea() {
return super.getHeight() * super.getWidth();
}
public double getTinsOfPaint() {
return super.getHeight()*super.getWidth()/24;
}
}
----------
package com.example.shape;
public class Triangle extends Shape {
@Override
public double getArea() {
return super.getHeight()*super.getWidth()/2;
}
public double getTinsOfPaint() {
return super.getHeight()*super.getWidth()/2/24;
}
}
用户应该能够输入宽度和高度,并将其传递给超类Shape,该类具有三角形和正方形的子类,它们可以计算出面积并将其显示在烤面包中。
答案 0 :(得分:0)
您遇到的问题是,用一个没有设置值的新Shape对象覆盖已经用值设置的myShape对象。
有几种方法可以解决此问题。以下是一些示例:
您可以设置宽度,高度和其他值,以便可以正确计算面积:
class AuthCallToActionScreen extends Component {
static navigationOptions = {
header: null,
}
render() {
return <View><Text>Auth Call To Action - No Header required</Text></View>
}
}
export default withNavigation(AuthCallToActionScreen)
您可以修改工厂以接受父形状,以便它可以继承所有值(宽度,高度等)。
myShape = myShapeFactory.getShape("T");
myShape.setWidth(Double.parseDouble(getWidth)); // You need to setup the width, etc.
Toast.makeText(this, Double.toString(myShape.Area()), Toast.LENGTH_LONG).show();
为此,我建议您阅读有关Clonable接口和clone方法的信息。 另外,您可以检查序列化和复制构造函数。
您可以在Shape中添加一个方法,以便它可以继承另一个图形的值:
myShape = myShapeFactory.getShape("T", myShape);
Toast.makeText(this, Double.toString(myShape.Area()), Toast.LENGTH_LONG).show();
此InheritedProperties方法只会将myShape的属性复制到myNewShape中。
让我知道这是否对您有意义。