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();//抽象方法"都是一样的结果,不明白抽象方法拿来干什么用??????????
解决方案 »
- 数据集合计算
- java 画图
- 初学者-哪位高手能给我解答一下一个连接SQL server2000的问题?急!
- 浮点型的有效位数问题
- 在j2se中查看URL所指向的链接,如何安装和启动is服务
- 问一个鼠标定位的问题
- Vector中他的元素可以是Vector型的吗?
- 一道JAVA 小汽车位置移动的作业题 求高手解答
- 请看过《thinking in java 2nd〉的高手回答一个小问题。
- 看看这个东西代表什么意思double[][][] printData = new double[cityNum][tradeNum][24];
- 心力憔悴中,跪求高人指点:图像如何显示在屏幕上?
- 用 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,你的手下就只好规规矩矩的按你设计的那几个方法了,呵呵。