python如何编写类_如何使用不同的对象在python中编写自定义类?

我正在尝试学习如何用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驱动程序尝试

^{pr2}$

我的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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值