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();//抽象方法"都是一样的结果,不明白抽象方法拿来干什么用??????????

解决方案 »

  1.   

    Literature_book  ll=new  Literature_book(530,0.7f);  
                           ll.price=(int)ll.getPrice(530,0.7f);  
                           ll.show();  
                           ll.show_price();  
    这部分多余的,可以不要了!
      

  2.   

    假设有一个方法是: 
    Test(Book a){
        a.show();
    }
    这样的话你如果没有abstract void show();的话,就无法编译通过.
    而上面的那个方法就是多态,假设你有Book有几个派生类,而你如果想调用show()的话,只需要写这一个方法就可以了!
      

  3.   

    其实抽象的类就是用来限定其继承类必须要包含那几个方法来用的,抽象类本身不能被new成对象,而只能被其他类继承后才能new成对象。
       作一个有点规模的项目时,初期设计总要为某个模块些几个基类,定义几个将来实现的方法,这时候抽象类就有用了,你可以只写函数名,返回值和参数以及要抛出的Exception,只后规定手下的程序员,所有的类必须继承这个类!OK,你的手下就只好规规矩矩的按你设计的那几个方法了,呵呵。