c# BOOK类的实现(疑问有答案)

这是别人的程序,只能先学习coby一下:

using System;
class BOOK {
//定义字段
private readonly string isbn;
private string titile;
private string author;
private string press;
private int price;
public BOOK(string isbn,string titile,string author,string press,int price)//构造函数,用到了this关键字强调主体
{
this.isbn=isbn;
this.titile=titile;
this.author=author;
this.press=press;
this.price=price;
}
public BOOK(string isbn) : this(isbn, "未知", "未知", "未知", 0) { } //疑问1??????????用this干什么的 是构造函数

//解释:在当前类的构造函数后面通过:this来调用当前类自己的其他构造函数


//给字段加属性用属性访问器
public string ISBN{
get{
return isbn;
}
}
public string TITLE{
get{
return titile;
}
set{
titile=value;
}
}
public string AUTHOR{
get{
return author;}
set{
author=value;}
}
public string PRESS{
get{
return press;}
set{
press=value;}
}
public int PRICE{
get{
return price;}
set{
price=value;}
}
public void Show()//show函数
{
Console.WriteLine("书号:{0}",isbn);
Console.WriteLine("标题:{0}",titile);
Console.WriteLine("作者:{0}",author);
Console.WriteLine("出版社:{0}",press);
Console.WriteLine("价格:{0}",price);
}


}
//构造函数
class callbook{
static void Main()
{
BOOK book1 = new BOOK("123456789");//对象
book1.Show();//函数
Console.WriteLine();
book1.TITLE = "分";
book1.AUTHOR = "是";
book1.PRESS = "啊";
book1.PRICE = 12;

book1.Show();

book1 = new BOOK("1236547", "好", "山", "了", 14);//疑问2?????????此处不用BOOK

也能实现对象的初始化不是很懂

book1.Show();
Console.WriteLine();
}
}

原文地址:https://www.cnblogs.com/whyareyousocool/p/4332189.html