21.编写一个Java应用程序,该程序包括3个类:Monkey类、People类和主类
E。要求:
(1) Monkey类中有个构造方法:Monkey (String s),并且有个public void speak()
方法,在speak方法中输出“咿咿呀呀......”的信息。
(2)People类是Monkey类的子类,在People类中重写方法speak(),在speak方法
中输出“小样的,不错嘛!会说话了!”的信息。
(3)在People类中新增方法void think(),在think方法中输出“别说话!认真思考!”
的信息。
(4)在主类E的main方法中创建Monkey与People类的对象类测试这2个类的功
能。、
复制代码
package zhongqiuzuoye
public class Monkey {
Monkey(String s) //构造
{}
public void speak()
{
System.out.println("咿咿呀呀......")
}
}
完整代码如下:
编译器版本:VS2012
#include<string>#include<time.h>
#include<iostream>
using namespace std
class Date
{
public:
Date(int y, int m, int d)
{
tm_year = y
tm_month = m
tm_mday = d
}
Date(const Date& D)
{
tm_year = D.tm_year
tm_month = D.tm_month
tm_mday = D.tm_mday
}
int tm_year
int tm_month
int tm_mday
}
class Book
{
public:
Book(string Book_name, string writer):title(Book_name),author(writer){}
Book(const Book& Books)
{
title = Books.title
author = Books.author
}
~Book(){}
Book operator=(Book Books)
{
title = Books.title
author = Books.author
}
friend ostream& operator<<(ostream&, const Book&)
protected:
string title
string author
}
ostream& operator<<(ostream& os, const Book& Book_a)
{
os << "title:" << Book_a.title << endl
<< "author:" << Book_a.author << endl
return os
}
class AudioBook:public Book
{
public:
AudioBook(string Book_name, string writer, string reader, Date lent_date):Book(Book_name, writer),narrator(reader),borrow_date(lent_date){}
AudioBook(const AudioBook&)//没有必要实现
AudioBook operator=(AudioBook ab)//没有必要实现
friend ostream& operator<<(ostream&, const AudioBook&)
~AudioBook(){}//没有必要实现
private:
string narrator
Date borrow_date
}
ostream& operator<<(ostream& os, const AudioBook& audio_Book_a)
{
os << "title:" << audio_Book_a.title << endl
<< "author:" << audio_Book_a.author << endl
<< "narrator:" << audio_Book_a.narrator << endl
<< "borrow_date:" << audio_Book_a.borrow_date.tm_year
<< "-" << audio_Book_a.borrow_date.tm_month
<< "-" << audio_Book_a.borrow_date.tm_year << endl
return os
}
class PaperBook:public Book
{
public:
PaperBook(string Book_name, string writer, string id, Date lent_date):Book(Book_name, writer),Book_id(id),borrow_date(lent_date){}
PaperBook(const PaperBook&)
PaperBook operator=(PaperBook pb)
friend ostream& operator<<(ostream&, const PaperBook&)
~PaperBook(){}
private:
string Book_id
Date borrow_date
}
ostream& operator<<(ostream& os, const PaperBook& paper_Book_a)
{
os << "title:" << paper_Book_a.title << endl
<< "author:" << paper_Book_a.author << endl
<< "narrator:" << paper_Book_a.Book_id << endl
<< "borrow_date:" << paper_Book_a.borrow_date.tm_year
<< "-" << paper_Book_a.borrow_date.tm_month
<< "-" << paper_Book_a.borrow_date.tm_year << endl
return os
}
int main()
{
time_t tmNow = time(NULL)
//solution method.
//选择项目->属性->C/C++预处理器->编辑预处理器定义->添加"_CRT_SECURE_NO_WARNINGS"
//上述处理后可正常运行程序
tm* pt = gmtime(&tmNow)//Warning3 at VS2012
Date d(1900+pt->tm_yday,1+pt->tm_mon,pt->tm_mday)
AudioBook ab1("Hello","Andrew","ABC",d)
AudioBook ab2("Yesterday","Karpenter","ABC",d)
PaperBook pb1("C","Tan","ABC",d)
PaperBook pb2("C++","Andrew","1001",d)
Book bk[4]={ab1,ab2,pb1,pb2}
for(int i=0i<4i++)
cout << bk[i] << endl
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)