/**
* ① 图书馆接口(Library)
成员方法:
borrow(),借阅图书
revert(),归还图书
② 图书类(Book)
成员变量:
图书名称(name)String类型
出版社(publisher)String类型
构造方法:
通过形参初始化图书名称(name)和出版社(publisher)
普通方法:
(1)设置getter和setter方法用于获取和设置类中的name变量的值;
(2)重写Equals方法,当且仅当书名(name)和出版社(publisher)均相等时,即为同一本书。
(3)重写toString方法,返回书名(name)和出版社(publisher)的信息,样式如下:
“书名:Java程序设计,出版社:清华大学出版社”
③ 馆藏图书类(CollectionBook),继承自Book类,实现Library接口
成员变量:
图书编号(bNo)String类型
所在书库(stacks)String类型
是否借阅(isBorrow)boolean类型 图书状态为已借阅时,值为true
构造方法:
调用父类构造方法初始化书名(name)和出版社(publisher)信息,然后初始化图书编号(bNo)和线路图书书库(stacks)
普通方法:
(1)实现接口中的borrow方法
如果图书状态为已借阅,则输出“对不起,该图书已借阅”,否则,修改该图书状态为已借阅,输出“借阅成功”
(2)实现接口中的revert方法
如果图书状态是可借阅状态,输出“该图书已归还”,否则,修改图书借阅状态为未借阅,并输出“还书成功”
④ 通过main函数测试
(1)创建两个CollectionBook对象book1和book2,分别输出book1和book2,并调用其equals方法判断两个对象是否相等
(2)通过键盘输入整数,输入0,则对book1进行借阅,输入1,则对book1进行归还操作
*/
using System;
namespace 考核题目三
{
public class Test3
{
public void ThirdTest()
{
CollectionBook book1 = new CollectionBook("1", "2", "3", "4");
CollectionBook book2 = new CollectionBook("1", "2", "3", "4");
Console.WriteLine(book1.Equals(book2));
int a = Convert.ToInt32(Console.ReadLine());
if (a == 0)
{
book1.borrow();
}
if (a == 1)
{
book1.revert();
}
}
}
interface Library
{
void borrow();
void revert();
}
class Book
{
protected string name;
protected string publisher;
public Book(string name, string publisher)
{
this.name = name;
this.publisher = publisher;
}
public string Name
{
get => name;
set => name = value;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}
Book book = (Book)obj;
if (ReferenceEquals(this, book))
{
return true;
}
if (string.Equals(name, book.name, StringComparison.InvariantCulture) &&
string.Equals(publisher, book.publisher, StringComparison.InvariantCulture))
{
return true;
}
return base.Equals(obj);
}
public override string ToString()
{
return "书名:" + name + ",出版社:" + publisher;
}
}
class CollectionBook : Book, Library
{
private string bNo;
private string stacks;
private bool isborrow;
public CollectionBook(string name, string publisher, string bNo, string stacks) : base(name, publisher)
{
this.bNo = bNo;
this.stacks = stacks;
}
public void borrow()
{
if (isborrow)
{
Console.WriteLine("该图书已被借阅");
}
else
{
isborrow = true;
Console.WriteLine("借阅成功");
}
}
public void revert()
{
if (!isborrow)
{
Console.WriteLine("该图书已归还");
}
else
{
isborrow = true;
Console.WriteLine("还书成功");
}
}
}
}
namespace 考核题目三
{
class Program
{
static void Main(string[] args)
{
Test3 test3 = new Test3();
test3.ThirdTest();
}
}
}
重载索引器
using System;
namespace IndexerApplication
{
class IndexedNames
{
private string[] namelist = new string[size];
static public int size = 10;
public IndexedNames()
{
for (int i = 0; i < size; i++)
{
namelist[i] = "N. A.";
}
}
public string this[int index]
{
get
{
string tmp;
if( index >= 0 && index <= size-1 )
{
tmp = namelist[index];
}
else
{
tmp = "";
}
return ( tmp );
}
set
{
if( index >= 0 && index <= size-1 )
{
namelist[index] = value;
}
}
}
public int this[string name]
{
get
{
int index = 0;
while(index < size)
{
if (namelist[index] == name)
{
return index;
}
index++;
}
return index;
}
}
static void Main(string[] args)
{
IndexedNames names = new IndexedNames();
names[0] = "Zara";
names[1] = "Riz";
names[2] = "Nuha";
names[3] = "Asif";
names[4] = "Davinder";
names[5] = "Sunil";
names[6] = "Rubic";
for (int i = 0; i < IndexedNames.size; i++)
{
Console.WriteLine(names[i]);
}
Console.WriteLine(names["Nuha"]);
Console.ReadKey();
}
}
}
抽象属性
using System;
namespace runoob
{
public abstract class Person
{
public abstract string Name
{
get;
set;
}
public abstract int Age
{
get;
set;
}
}
class Student : Person
{
private string code = "N.A";
private string name = "N.A";
private int age = 0;
public string Code
{
get
{
return code;
}
set
{
code = value;
}
}
public override string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public override int Age
{
get
{
return age;
}
set
{
age = value;
}
}
public override string ToString()
{
return "Code = " + Code +", Name = " + Name + ", Age = " + Age;
}
}
class ExampleDemo
{
public static void Main()
{
Student s = new Student();
s.Code = "001";
s.Name = "Zara";
s.Age = 9;
Console.WriteLine("Student Info:- {0}", s)
s.Age += 1;
Console.WriteLine("Student Info:- {0}", s);
Console.ReadKey();
}
}
}