Book abstract class Book int book Page float
//定义抽象类Book abstract class Book { int book. Page; float discount; double price; //图书页码 //图书折扣 //图书价格 public Book(int book. Page, float discount) { this. book. Page=book. Page; this. discount=discount; } abstract void show_kind(); //抽象方法,显示图书种类 //抽象方法,计算价格 abstract double get. Price(int book. Page, float discount); public void show_price() { // 非抽象方法:显示价格 System. out. println("This book's price is "+price); } }
//定义科技书子类 class Science_book extends Book { public Science_book(int book. Page, float discount) { super(book. Page, discount); //引用父类的构造方法 } public void show_kind() { //实现抽象方法 System. out. println("The book's kind is science"); } //实现抽象类方法 public double get. Price(int book. Page, float discount) { return book. Page*0. 1*discount; } }
//定义文艺书子类 class Literature_book extends Book { public Literature_book(int book. Page, float discount) { super(book. Page, discount); } public void show_kind() { System. out. println("The book's kind is literature"); } public double get. Price(int book. Page, float discount) { return book. Page*0. 08*discount; } }
//定义教材子类 class Teaching_book extends Book { public Teaching_book(int book. Page, float discount) { super(book. Page, discount); } public void show_kind() { System. out. println("The book's kind is teaching book"); } public double get. Price(int book. Page, float discount) { return book. Page*0. 05*discount; } }
//创建三类图书的类对象、调用其方法: public class Booksell { public static void main(String args[]) { //创建文艺书类对象 Science_book sb=new Science_book(530, 0. 7 f); sb. price=sb. get. Price(530, 0. 7 f); sb. show_kind(); sb. show_price(); //创建文艺书类对象 Literature_book lb=new Literature_book(530, 0. 7 f); lb. price=lb. get. Price(530, 0. 7 f); lb. show_kind(); lb. show_price(); 程序运行结果: //创建教材类对象 } } The book's kind is science Teaching_book tb=new Teaching_book(530, 0. 7 f); This book's price is 37. 0 tb. price=tb. get. Price(530, 0. 7 f); The book's kind is literature tb. show_kind(); This book's price is 29. 0 tb. show_price(); The book's kind is teaching book This book's price is 18. 0
Example 以下是一个接口声明: public interface Shape 1 { public static final PI=3. 14159; // 成员变量,常量 // 抽象成员方法 public abstract double area(); public abstract double volume(); public abstract void show(); }
//例8 -2——实现接口 interface Shape 1 { public static final double PI=3. 14159; public abstract double area(); //计算图形面积 public abstract double volume( ); //计算图形体积 public abstract void show_height(); //显示图形高度 } class Circle 1 implements Shape 1 { double radius; public Circle 1(double r) { radius =r; } public double area() { return PI*radius; } public double volume( ) // 类中不用的抽象方法 { return 0; } public void show_height() { } // 空方法 }
class Cylinder 1 extends Circle 1 { //实现接口中所有的抽象方法 double radius, height; 圆 Area=28. 27 public Cylinder 1(double r, double h) 圆柱 高度是: 8. 0 圆柱底 Area=78. 54, volum=628. 32 { super(r); height=h; } public double volume() { return (int)(height*area()*100+0. 5)/100. 0; } public void show_height() { System. out. println("圆柱 高度是: "+height); } } public class ch 82 { public static void main(String[] args) { Circle 1 circle=new Circle 1(3); System. out. println( "圆 Area="+circle. area()); Cylinder 1 cl=new Cylinder 1(3, 5); cl. show_height(); System. out. println( “圆柱侧 Area="+cl. area()+", volum="+cl. volume()); } }
//实现多接口 interface Shape 2 { String get. Name(); } public class Square implements Shape 1, Shape 2 { double a; Square(double a){this. a=a; } public double area() { return a*a; } public double volume() { return a*a*a; } public void show_height() { } // 空方法 public String get. Name() { return "正方形: "; } public static void main(String[] args) { Square s=new Square(3); System. out. println(s. get. Name()+"Area="+s. area()+", volume="+s. volume()); } } 正方形: Area=9. 0, volume=27. 0
DEMO——包的声明 package ppp; //声明包ppp,以下的类均在此包中 public class New. Point { double x, y; public void set. Point(double a, double b) { x=a; y=b; } public double get. X() { return x; } public double get. Y() { return y; } public String to. String() { return "["+x+", "+y+"]"; } }
import ppp. *; import ppp_son. *; public class New. Circle extends New. Point implements newshow // public class New. Circle extends ppp. New. Point implements ppp_son. newshow { double r; New. Circle(double x, double y, double r){ set. Point(x, y); this. r=r; } double area(){return Math. PI*r*r; } public String newshow(String s){return s; } public static void main(String[] args) { New. Circle c 1=new New. Circle(3, 4, 5); System. out. println(c 1. newshow("圆形: ")+"area="+c 1. area()); } }
- Slides: 27