Class news
Here is a basic outline for a News
class in Python:
class News:
def __init__(self, title, content, date, author):
self.title = title
self.content = content
self.date = date
self.author = author
def display(self):
print(f"Title: {self.title}")
print(f"Content: {self.content}")
print(f"Date: {self.date}")
print(f"Author: {self.author}")
def __str__(self):
return f"{self.title} - {self.date} by {self.author}"
This class has the following attributes:
title
: The title of the news article.content
: The content of the news article.date
: The date the news article was published.author
: The author of the news article.
The class has two methods:
display()
: This method prints out the title, content, date, and author of the news article.__str__()
: This method returns a string representation of the news article, which can be used for printing or other purposes.
Here's an example of how you could use this class:
news1 = News("New Study Reveals...", "A new study has revealed...", "2022-01-01", "John Doe")
news1.display()
print(news1)
This would output:
Title: New Study Reveals...
Content: A new study has revealed...
Date: 2022-01-01
Author: John Doe
New Study Reveals... - 2022-01-01 by John Doe