c# - 如何从用户输入创建对象 - C#
问题描述
我是 OOP 的初学者,最近开始使用 C# 进行编码。(在 Visual Studio 2017 控制台应用程序中工作。)搜索教程后,我会在这里试试运气。
我正在尝试制作一个简单的程序,允许用户通过询问他们的输入来制作一个新的对象书(或多个)。我知道如何手动创建类的对象,但不知道如何对用户输入做同样的事情。这是我到目前为止的代码:
public class book
{
//variables
private string title;
private string author;
private string genre;
private string series;
//constructor
public book(string _titel, string _author, string _genre, string _series)
{
this.titel = _titel;
this.author = _author;
this.genre = _genre;
this.series = _series;
}
//method to ask user for input to create book
public void createBook()
{
}
解决方案
Console.WriteLine("Input Title: ");
var title = Console.ReadLine();
Console.WriteLine("Input Author: "):
var author = Console.ReadLine();
etc..
var book = new Book(title,author etc...)
您将输入作为变量获取,然后从这些变量构造您的对象。
推荐阅读
- java - 在不知道其类型的情况下基于另一个抽象类的子类创建一个抽象类的子类
- amazon-web-services - URL 响应为 9422 毫秒,超过阈值(8000 毫秒):unitrl
- php - Laravel - 如何删除数组中的项目不使用循环
- java - 更新响应对象而不转换为 java 对象
- ruby-on-rails - 根据 Rails 中的另一个表更新数量
- c# - 如何使用 C# 在文件中用单个逗号替换多个选项卡
- java - 喜欢和不喜欢 Firebase 的系统不工作
- python - 使用 pandas 函数合并 2 行
- java - 两个 Comparables 的最小/最大函数
- url - Nginx 混淆 $fastcgi_script_name 和 $fastcgi_path_info