abstract class Book{
int bookPage;
float discount;
double price;
abstract void show();//抽象方法
abstract double getPrice(int bookPage,float discount);
public Book(int bookPage,float discount){
this.bookPage=bookPage;
this.discount=discount;
}
public void show_price(){System.out.println("this book's price is "+price);
}
}class Science_book extends Book{
public Science_book(int bookPage,float discount){
super(bookPage,discount);
}
public void show(){
System.out.println("the book's kind is discount");
}
public double getPrice(int bookPage,float discout){
return bookPage*0.1*discount;
}
}public class Booksell{
public static void main(String[] arg){
Science_book bb=new Science_book(530,0.7f);
bb.price=(int)bb.getPrice(530,0.7f);
bb.show();
bb.show_price();
Literature_book ll=new Literature_book(530,0.7f);
ll.price=(int)ll.getPrice(530,0.7f);
ll.show();
ll.show_price();
}
}问题是程序中有没有"abstract void show();//抽象方法"都是一样的结果,不明白抽象方法拿来干什么用??????????
int bookPage;
float discount;
double price;
abstract void show();//抽象方法
abstract double getPrice(int bookPage,float discount);
public Book(int bookPage,float discount){
this.bookPage=bookPage;
this.discount=discount;
}
public void show_price(){System.out.println("this book's price is "+price);
}
}class Science_book extends Book{
public Science_book(int bookPage,float discount){
super(bookPage,discount);
}
public void show(){
System.out.println("the book's kind is discount");
}
public double getPrice(int bookPage,float discout){
return bookPage*0.1*discount;
}
}public class Booksell{
public static void main(String[] arg){
Science_book bb=new Science_book(530,0.7f);
bb.price=(int)bb.getPrice(530,0.7f);
bb.show();
bb.show_price();
Literature_book ll=new Literature_book(530,0.7f);
ll.price=(int)ll.getPrice(530,0.7f);
ll.show();
ll.show_price();
}
}问题是程序中有没有"abstract void show();//抽象方法"都是一样的结果,不明白抽象方法拿来干什么用??????????
解决方案 »
- inieditor-r2.jar的使用方法
- JAVA中的泛型集合问题
- 解析XML出错,大家来看看,顶者有分
- [绝对高分]如何抓取网页内的数据?
- 求助:为什么我在eclipse中启动tomcat时会跳出错误对话框???
- 高分求解:eclipse下采用swt+hibernate开发的项目打包后运行不能连库的问题
- 如何检测到用户已经打开"关闭计算机"的窗口了呢??
- 为什么是Integer.arseInt(),而不是Integer.valueOf()
- 找不到错误呀,快来帮忙,谢谢大家!
- 如何更新JBulider的JDK呢?
- 心力憔悴中,跪求高人指点:图像如何显示在屏幕上?
- 用 Java 怎样求两个日期之间的差
ll.price=(int)ll.getPrice(530,0.7f);
ll.show();
ll.show_price();
这部分多余的,可以不要了!
Test(Book a){
a.show();
}
这样的话你如果没有abstract void show();的话,就无法编译通过.
而上面的那个方法就是多态,假设你有Book有几个派生类,而你如果想调用show()的话,只需要写这一个方法就可以了!
作一个有点规模的项目时,初期设计总要为某个模块些几个基类,定义几个将来实现的方法,这时候抽象类就有用了,你可以只写函数名,返回值和参数以及要抛出的Exception,只后规定手下的程序员,所有的类必须继承这个类!OK,你的手下就只好规规矩矩的按你设计的那几个方法了,呵呵。