我是Python新手,我想弄清楚是否有办法在参数定义中指定变量类型.例如:
def function(int(integer))
Run Code Online (Sandbox Code Playgroud)
而不是:
def function(integer)
int(integer)
Run Code Online (Sandbox Code Playgroud)
我知道这不是一个主要的区别,但我试图在这里使用良好的编程实践,如果我定义一个具有大量参数的函数,它可能会变得混乱.
我已经覆盖了类Object的toString方法,但我的方法不起作用,我无法弄清楚原因.这是我的方法的代码(在一个名为的类中ShoppingBag):
public String toString(){
String str = "";
Item temp = record;
str += "\n\nThe bag contains:\n";
str += String.format("%-18s%-13s%-12s\n", "Name of the Items", "Quantity", "Subtotal");
while(temp != null){
str += String.format("%-18s%-13s%-12s\n", temp.getItemName(), temp.getQuantity(),
"$"+(temp.getRetailPrice()*temp.getQuantity()));
}
str += String.format("%-18s%-13s%-12s\n", "", "Total:", "$"+this.totalCost());
str += String.format("%-18s%-13s%-12s\n", "", "Tax(5%):", "$"+(this.totalCost()
* taxRate));
str += String.format("%-18s%-13s%-12s\n", "", "Grand Total:", "$"+this.totalCost()
+(this.totalCost()*taxRate));
String test = "test1";
return test;
}
Run Code Online (Sandbox Code Playgroud)
我知道那里有很多带有Item和String.format的垃圾.编译或运行时没有例外,它只是不打印任何东西.
在我的应用程序中,我试试这个:
ShoppingBag bag = new ShoppingBag(parameters);
System.out.println(bag.toString());
Run Code Online (Sandbox Code Playgroud)
并没有打印.当我注释掉除了我的方法(String test …