定义了4个类:出版社类(定义、显示出版社信息)、作者类(定义、显示作者信息)、图书类(定义、显示图书信息)、Demo类(最终在main方法中输出)
代码如下:
package demo05;
/*
* 定义一个出版社类
* 出版社的名称,地址
*/
public class Press {
String pressName;
String pressAdd;
public void showInfo() {
System.out.println("出版社名称:"+pressName);
System.out.println("出版社地址:"+pressAdd);
}
}
package demo05;
/*
* 定义一个作者类,包括:姓名,性别,专业
*/
public class Author {
String authorName;
int authorAge;
String authorMajor;
public void showInfo() {
System.out.println("作者姓名:"+authorName);
System.out.println("作者年龄:"+authorAge);
System.out.println("作者专业:"+authorMajor);
}
}
package demo05;
/*
* 定义图书类,包括:书名,书号,作者,出版社
*/
public class Book {
String bookName;
String bookID;
Author author;
Press press;
public void showInfo() {
System.out.println("书名:"+bookName);
System.out.println("书号:"+bookID);
//显示作者信息
author.showInfo();
//显示出版社信息
press.showInfo();
}
}
package demo05;
public class Demo {
public static void main(String[] args) {
Press tsinghuaPress=new Press();
tsinghuaPress.pressName="清华大学出版社";
tsinghuaPress.pressAdd="北京";
Author author=new Author();
author.authorName="张三";
author.authorAge=36;
author.authorMajor="计算机";
Book javaBook=new Book();
javaBook.bookName="java编程思想";
javaBook.bookID="1001001";
javaBook.press=tsinghuaPress;
javaBook.author=author;
javaBook.showInfo();
}
}