2014-12-06 45 views
-3

好吧,所以我必须创建一个名为book的类,它必须具有作者,标题和日期,并且必须在末尾添加一个名为display的方法)输出标题和作者。我认为我有正确的代码,但它一直给我一个错误的日期?我在创建c类书籍时遇到了问题#

namespace lab7_feador 
    { 
    class Program 
    { 
     static void Book(string[] args); 

      public class Book 
{ 
    public string Nick { get; set; } 
    public string James { get; set; } 
    public string Radar { get; set; } 
    public int 1989 { get; set; } 



       } 
     } 
    } 

这是它应该如何?

或像不像这个,我只需要添加日期

命名空间lab7_feador {

class Program 
{ 
    static void Main(string[] args) 
    { 
     book; 

     book = title book ("Radars adventure"); 
     Console.WriteLine(book.title()); 

     book = Author ("Nick James"); 
     Console.WriteLine(author.Describe()); 

     Console.ReadLine(); 

    } 
} 

    class book 
    { 
     private string title; 

     public book(string title) 
     { 
      this.title = title; 
     } 

     public string Describe() 
     { 
      return "The name of this book is " + title; 
     } 

     public string Color 
     { 
      get { return title; } 
      set { title = value; } 
     } 
    } 
} 
+0

这是什么日期,错误信息是什么? – 2014-12-06 20:08:42

+3

'public int 1989 {get;组; }'?哦,男孩...... – LarsTech 2014-12-06 20:09:49

+1

将来,请始终在您的问题中包含错误消息......简单地告诉我们存在错误没有帮助。 – 2014-12-06 20:10:22

回答

0

如果你想有时间,那么你应该使用DateTime类型,你也可以不显示直接约会。 做这样的事情,当你创建你的对象的instannce分配,

你的类将是:

public class Book 
    { 
    public string Property1 { get; set; } 
    public string Property2 { get; set; } 
    public string Property3 { get; set; } 
    public DateTime SomeDate { get; set; } 
    } 

这样的分配值,

Book bookObj = new Book(); 
    bookObj.Property1 = "Nick"; 
    bookObj.Property2 = "James"; 
    bookObj.Property3 = "Radar"; 
    bookObj.SomeDate = new DateTime(1989, 01, 01); 
+0

这就是我认为应该如何做,如果有更多的知识渊博的方式我想知道我不希望它是怪异的哈哈,是的,很抱歉,我应该包括错误 – 2014-12-06 20:14:45

+0

@ coleenfeador你的意思是你想要设置默认值? – Sajeetharan 2014-12-06 20:19:26

+0

是啊,就像我想要说的作者和标题和日期,如果这是有道理的,当我试着它的日期下有红色的错误行,也不知道我使用了错误的代码? – 2014-12-06 20:22:34

1

1)您的类被称为程序。你想要一个名为Book:

class Book {} 

2)你有属性尼克,詹姆斯,雷达和“1989”。您需要属性标题,作者和日期。

public string Author {get;set;} 
public string Title {get;set;} 
public DateTime Date {get;set;} 

3)您的日期有误。没有日期,只有一个称为1989的整数属性(这是一个无效的属性名称)。 1989年更可能是该日期的年份的价值比该财产的名称。

我不会在这里提供一个完整的解决方案,因为这看起来很家常 - 但看看本教程;它应该可以帮助你解决这个问题:http://csharp.net-tutorials.com/classes/introduction/