gpt4 book ai didi

java - 您将如何使用不同的对象在 Python 中编写自定义类?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:43:21 24 4
gpt4 key购买 nike

我正在尝试学习如何使用 Python 编写代码,但在寻找在线创建自定义类的方法时遇到了麻烦。我用 java 写了一个程序,我试图用 python 转换它。我想我已经关闭了自定义类(我不确定),而且我肯定遇到了驱动程序问题。

我的自定义类(python):

class CostCalculator:
__item = ""
__costOfItem = 0.0
__tax = 0.0
__tip = 0.0

def set_item(self, item):
self.__item = item
def get_name(self):
return self.__item

def set_costOfItem(self, costOfItem):
self.__costOfItem = costOfItem
def get_costOfItem(self):
return self.__costOfItem

def get_tax(self):
__tax = self.__costOfItem * .0875
return self.__tax

def get_tip(self):
__tip = self.__costOfItem * .15
return self.__tip

我的 python 驱动程序尝试

import sys
from CostCalculator import CostCalculator

item = ""
cost = 0.0
totalTip = 0.0
totalTax = 0.0
overallTotal = 0.0
subtotal = 0.0

print("Enter the name of 3 items and their respective costs to get the total value of your meal")
print ("\n Enter the name of your first item: ")
item = sys.stdin.readline()
print("How much is " + item + "?")
cost = sys.stdin.readLine()

我的 java 自定义类和驱动程序:

public class TotalCost
{
String item = " ";
double costOfItem = 0;
double tax = 0;
double tip = 0;

public void setItem ( String i )
{
item = i;
}

public String getItem()
{
return item;
}

public void setCostOfItem ( double c )
{
costOfItem = c;
}

public double getCostOfItem ()
{
return costOfItem;
}

public double getTax ()
{
double tax = costOfItem * .0875;
return tax;
}

public double getTip()
{
double tip = costOfItem * .15;
return tip;
}

public String toString()
{
String str;
str = "\nMeal: " + getItem() +
"\nCost of " + getItem() + ": " + getCostOfItem() +
"\nTax of " + getItem() + ": " + getTax() +
"\nTip of " + getItem() + ": " + getTip();
return str;

}

}

import java.util.Scanner;
public class Driver
{
public static void main (String args[])
{
Scanner input = new Scanner (System.in);

String item ;
double cost ;
double totalTip = 0;
double totalTax = 0;
double OverallTotal = 0;
double subtotal;
TotalCost a = new TotalCost ();
TotalCost b = new TotalCost ();
TotalCost c = new TotalCost ();

System.out.println("Enter the name of 3 items and their respective costs to get the total value of your meal");
System.out.println("Enter the name of your first item: ");
item = input.nextLine();
a.setItem ( item );
System.out.println("How much is " + a.getItem() + "?" );
cost = input.nextDouble();
a.setCostOfItem (cost);

input.nextLine();

System.out.println("Enter the name of your second item: ");
item = input.nextLine();
b.setItem (item);
System.out.println("How much is a " + b.getItem() + "?");
cost = input.nextDouble();
b.setCostOfItem (cost);

input.nextLine();

System.out.println("Enter the name of your third item: ");
item = input.nextLine();
c.setItem (item);
System.out.println("How much is a " +c.getItem() + "?" );
cost = input.nextDouble();
c.setCostOfItem(cost);

System.out.println(a + "\n" + b + "\n" + c);
subtotal = a.getCostOfItem() + b.getCostOfItem() + c.getCostOfItem();
totalTip = a.getTip() + b.getTip() + c.getTip();
totalTax = a.getTax() + b.getTax() + c.getTax();
OverallTotal = subtotal + totalTip + totalTax;

System.out.println("\n\tSubtotal: $" + subtotal);
System.out.println("\tTax: $" + totalTax);
System.out.println("\tTip: $" + totalTip);
System.out.println("\tMeal Total: $" + OverallTotal);
}
}

最佳答案

在 Python 中,没有 publicprivate 的概念,一切都是 public 所以你不需要 setter 或 getter。

您需要的是 __init__ 函数,它类似于构造函数。您可以在此处初始化成员变量,这样它们就不是静态的并且在您的类的所有实例之间共享。您还可以添加默认参数,这样您可以在实例化时将任何参数、全部参数或不传递任何参数传递给类。

class CostCalculator:
def __init__(self, item = "", cost = 0.0):
self.item = item
self.cost = cost

def __str__(self):
return 'Meal: {item}\nCost of {item}: {cost}\nTax of {item}: {tax}\nTip of {item}: {tip}'.format(item = self.item, cost = self.cost, tax = self.calc_tax(), tip = self.calc_tip())

def calc_tax(self):
return self.cost * 0.0875

def calc_tip(self):
return self.cost * 0.15

def calc_total(self):
return self.cost + self.calc_tax() + self.calc_tip()

然后你可以创建这个类的一个实例。再次注意,无论好坏,您都可以直接访问成员而无需 setter 或 getter ;)

>>> c = CostCalculator('cheese', 1.0)
>>> c.item
'cheese'
>>> c.calc_tip()
0.15

现在你可以在你的对象上调用print

>>> c = CostCalculator('cheese', 1.0)
>>> print(c)
Meal: cheese
Cost of cheese: 1.0
Tax of cheese: 0.085
Tip of cheese: 0.15

最后,您接受用户输入的方式通常是通过 input(尽管乱用 stdin 不一定是错误的)

>>> tax = input('how much does this thing cost? ')
how much does this thing cost? 15.0
>>> tax
'15.0'

关于java - 您将如何使用不同的对象在 Python 中编写自定义类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30618927/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com