Python Help:编写WeatherForecast类的定义

时间:2015-05-14 02:11:17

标签: python

  

写一个提供的WeatherForecast类的定义   以下行为(方法):

A method called set_skies that has one parameter, a String.
A method called set_high that has one parameter, an int.
A method called set_low that has one parameter, an int.
A method called get_skies that has no parameters and that returns the value that was last used as an argument in set_skies .
A method called get_high that has no parameters and that returns the value that was last used as an argument in set_high .
A method called get_low that has no parameters and that returns the value that was last used as an argument in set_low .
     

不需要定义构造函数。一定要定义实例变量   根据您的需要"得到" /"设置"方法

class WeatherForecast(object):
    def __init__ (self, skies, value):
        self.skies = ""
        value = 0
    def get_skies():
        return self.set_skies
    def set_skies(self, value)
        self.skies = value
    def get_high():
        return self.set_high
    def set_high(self, value):
        self.high = value
    def get_low():
        return self.set_low
    def set_low(self, value):
        self.low = value

class WeatherForecast():

skies = "Clear"
high = 80
low = 20 

def set_skies(self, skies)
    self.skies = skies

def get_skies(self):
    return self.skies

def set_high(self, high):
    self.high = high

def get_high(self):
    return self.high

def set_low(self, value):
    self.low = value

def get_low(self):
    return self.low

4 个答案:

答案 0 :(得分:0)

好像你缺少了一些实例变量。

规范要求你有一个用于描述天空的字符串(即阴天,晴天)和两个表示当天低温和高温的整数(即低23和高45)

规范说您不必在构造函数中处理它们,但如果您愿意,也可以。

因为这是我作业的作业,我会把你带到正确的路线上;但是我不想给你答案。

变量:

  1. 天空的字符串
  2. int for the low temp
  3. int for high temp
  4. 功能:

    1. 获取/设置天空。 get应该返回self.skies,set应该为天空设置实例变量。

    2. 获取/设置低温。 get应返回self.low,set应设置低温的实例变量。

    3. 获取/设置为高温。 get应返回self.high,set应设置高温的实例变量。

    4. 祝你好运。

      如果你想查找一些语法,这里是python类的链接。 https://docs.python.org/2/tutorial/classes.html

      编辑:我没有给op这个代码,他们写了它(减去一个缺少的冒号)

      class WeatherForecast():
          skies = "Clear"
          high = 80
          low = 20 
          def set_skies(self, skies):
              self.skies = skies
          def get_skies(self):
              return self.skies
          def set_high(self, high):
              self.high = high
          def get_high(self):
              return self.high
          def set_low(self, value):
              self.low = value
          def get_low(self):
              return self.low
      

      您可以通过创建类类型的对象并在其上运行这些类方法来运行此代码。

      IE)

      w = WeatherForecast()
      w.set_skies("clear")
      w.set_low(20)
      w.set_high(30)
      
      w.get_skies()
      w.get_low()
      w.get_high()
      

答案 1 :(得分:0)

class WeatherForecast():
      skies = ""
      high = 0
      low = 0
      def set_skies(self, skies):
          self.skies = skies
      def get_skies(self):
          return self.skies
      def set_high(self, high):
          self.high = high
      def get_high(self):
          return self.high
      def set_low(self, low):
          self.low = low
      def get_low(self):
          return self.low

答案 2 :(得分:0)

为myProgrammingLab输入就像这样。 (包括'class WeatherForecast:“part。

class WeatherForecast:

    def __init__(self):
    self.skies=""
    self.low=0
    self.high=0

def set_skies(self,skies):
    self.skies = skies

def set_high(self, high):
    self.high = high

def set_low(self, low):
    self.low = low

def get_skies(self):
    return self.skies

def get_high(self):
    return self.high

def get_low(self):
    return self.low

答案 3 :(得分:0)

class WeatherForecast:

   def  __init__(self):
       self.skies=""
       self.low=0
       self.high=0

    def set_skies(self,skies):
         self.skies = skies

    def set_high(self,high):
         self.high = high

     def set_low(self,low):
         self.low = low

     def get_skies(self):
         return self.skies

     def get_high(self):
         return self.high
     def get_low(self):
         return self.low

dan=WeatherForecast()

您必须致电WeatherForecast才能让它在Myprogramming lab上运行