2014-12-06 46 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/