彼岸微蓝
2016-08-10 12:49
//封装数据 package com.pictice; public class Book { String bookName; int number; public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } @Override public String toString() { return "Book [bookName=" + bookName + ", number=" + number + "]"; } } //写入数据 package com.pictice; public class IBook extends Book { String bookName; int number; public IBook(int number, String bookName) { // TODO Auto-generated constructor stub Book book = new Book(); book.setBookName(bookName); book.setNumber(number); this.number = book.getNumber(); this.bookName = book.getBookName(); } @Override public String toString() { return "IBook [bookName=" + bookName + ", number=" + number + "]"; } } //获取数据(无法获取) package com.pictice; import com.pictice.Book; public class GetBook extends Book{ int number; String nameBook; public GetBook() { // TODO Auto-generated constructor stub //Book book=new Book(); this.number=super.getNumber(); this.nameBook=super.getBookName(); } @Override public String toString() { return "GetBook [number=" + number + ", nameBook=" + nameBook + "]"; } }
首先 没有调用的main方法,其次,在写入数据的时候不能把值赋给父类的Book,只能赋值在写入的类的Book对象上,最后,建议调用的时候直接调用第二个类的toString()方法,不要再第三个类当中调用,因为Book 中没有实际的值。
你可以试试引入抽象,
Java入门第二季 升级版
530649 学习 · 6091 问题
相似问题