Ingredient::getIngredientId报错,错误信息:Non-static method cannot be referenced from a static context
时间: 2024-06-16 10:08:31 浏览: 5
Ingredient::getIngredientId报错的原因是非静方法不能从静态上下文中引用。这意味着你在一个静态方法中尝试调用了一个非静态方法。
在面向对象编程中,静态方法是属于类的,而非静态方法是属于类的实例的。静态方法可以直接通过类名调用,而非静态方法需要通过类的实例来调用。
解决这个问题的方法是,要么将调用该方法的方法也设置为非静态方法,要么将被调用的方法设置为静态方法。具体取决于你的需求和设计。
相关问题
如何打印使"Dinner with ingredient:" + ingredient.getName() + "-" + ingredient.getPrice() + "; Total bill" + totalBill中"Dinner with ingredient:"只有一遍
你可以使用一个标志变量,例如 `printHeader`,来判断是否需要打印 "Dinner with ingredient:"。在第一次打印时将其设置为 `True`,之后就不再打印。以下是伪代码示例:
```
printHeader = True
for ingredient in ingredients:
if printHeader:
print("Dinner with ingredient:")
printHeader = False
print(ingredient.getName() + "-" + ingredient.getPrice())
print("Total bill " + totalBill)
```
python爬虫第三关:爬取菜谱网站/爬取豆瓣top250
你好!对于爬取菜谱网站,你可以使用 Python 的 requests 和 BeautifulSoup 库来实现。首先,你需要发送请求获取网页的 HTML 内容,然后使用 BeautifulSoup 解析该内容,提取出你所需的菜谱信息。
以下是一个简单的示例代码,以爬取美食天下网站(www.meishij.net)为例:
```python
import requests
from bs4 import BeautifulSoup
def get_recipe(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 在这里根据网页结构提取菜谱信息
# 例如,获取菜名和食材
recipe_name = soup.find('h1', class_='recipe-title').text.strip()
ingredients = soup.find('div', class_='particulars').ul.find_all('li')
ingredient_list = []
for ingredient in ingredients:
ingredient_list.append(ingredient.text.strip())
# 输出菜谱名称和食材列表
print("菜谱名称:", recipe_name)
print("食材列表:", ingredient_list)
# 菜谱详情页的URL
recipe_url = 'https://www.meishij.net/1'
get_recipe(recipe_url)
```
对于爬取豆瓣 Top250 电影榜单,同样可以使用 requests 和 BeautifulSoup 库来实现。以下是一个简单的示例代码:
```python
import requests
from bs4 import BeautifulSoup
def get_top250():
url = 'https://movie.douban.com/top250'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
movie_list = soup.find('ol', class_='grid_view').find_all('li')
for movie in movie_list:
# 获取电影排名、名称和评分
rank = movie.find('em').text
title = movie.find('span', class_='title').text
rating = movie.find('span', class_='rating_num').text
# 输出电影信息
print("排名:", rank)
print("名称:", title)
print("评分:", rating)
print()
get_top250()
```
以上代码只是一个简单示例,你可以根据具体的网页结构和需求进行相应的修改和扩展。希望能对你有所帮助!