一.抽象类和方法
1.抽象关键字:abstract
2.抽象方法的特点:没有方法的实现,必须出现在抽象类中
3.抽象类的特点:不一定要有抽象方法,可以有普通方法,不能被实例化,子类继承抽象父类必须重写抽象方法
4.抽象类的用途:在程序的设计过程中使用,作为类的模板或者框架
二.接口
1.定义:对行为的一种规范。在java中接口和类是同一级别
2.语法:
定义接口:interface
实现接口:implements
3.特点:
a.在接口中只能定义静态常量和抽象方法
b.接口不能实例化,必须由实现类实现再被调用
c.接口可以多实现,解决了java中单一继承的局限
d.实现类一旦实现接口就必须实现接口的全部抽象方法
三.例子
开发一个租车系统
车型以及每天的租金
小车 客车
别克 途观 宝马 宾利 12> 20> 32>
300 600 800 1500 800 1200 1800
要求:客户选择车型,再输入天数,计算出租金?
思路:
一:发现类
车类(Vehicle):父类
小车(Car):子类
客车(Bus):子类
二:发现属性和方法
车类属性:每天租金
车类的方法:计算租金(抽象方法)
小车属性:品牌
方法:重写父类计算租金方法,属性的get/set方法,在set或者get方法中完成品牌的判断从而得到每天的租金
客车属性:座位数
方法:同上
业务升级1:加入了新的车种,货车。货车是按吨数来计算日租金,每吨每天50块。把该功能加入到原有系统中
业务升级2:可以选择多次租车,最后完成总金额的计算
二。接口练习
1.设计笔记本电脑的usb接口,接入不同的产品
移动硬盘
电风扇
完成相应的工作。
思路:
发现类:
笔记本电脑
属性:usb接口
方法:笔记本运行--在这个方法中就会去调用usb接口接入进来的方法
USB接口:
工作方法(抽象)
移动硬盘:实现USB接口
电风扇:实现USB接口
测试类:创建笔记本,创建移动硬盘或者是电风扇接入笔记本开始工作
2.开发一个打印机:
打印机可以使用不同的纸张和墨盒。
现在有A4,B5两个厂商生产纸张,有黑白和彩色两个厂商生产墨盒。
用户可以选择不同纸张和墨盒打印不同效果。
采用面向接口的方式。
/*开发一个租车系统
车型以及每天的租金
小车 客车
别克 途观 宝马 宾利 12> 20> 32>
300 600 800 1500 800 1200 1800
要求:客户选择车型,再输入天数,计算出租金?
思路:
一:发现类
车类(Vehicle):父类
小车(Car):子类
客车(Bus):子类
二:发现属性和方法
车类属性:每天租金
车类的方法:计算租金(抽象方法)
小车属性:品牌
方法:重写父类计算租金方法,属性的get/set方法,在set或者get方法中完成品牌的判断从而得到每天的租金
客车属性:座位数
方法:同上
业务升级1:加入了新的车种,货车。货车是按吨数来计算日租金,每吨每天50块。把该功能加入到原有系统中
业务升级2:可以选择多次租车,最后完成总金额的计算
*/
package com.class1107;
import java.util.Scanner;
public class Class01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int cr=0;
int sum=0;
do{
System.out.println("请输入要租赁的车型:小车;客车;货车");
String sign=sc.next();
switch(sign){
case "小车":
Car c=new Car();
System.out.println("请输入要租赁的车型:别克;途观;宝马;宾利");
c.setBrand(sc.next());
System.out.println("请输入要租赁的天数");
cr=c.countrent(sc.nextInt());
break;
case "客车":
Bus b=new Bus();
System.out.println("请输入要租赁的客车座位数");
b.setSeat(sc.nextInt());
System.out.println("请输入要租赁的天数");
cr=b.countrent(sc.nextInt());
break;
case "货车":
Trucks t=new Trucks();
System.out.println("请输入要租赁的客车吨位");
t.setTon(sc.nextInt());
System.out.println("请输入要租赁的天数");
cr=t.countrent(sc.nextInt());
break;
default:
System.out.println("输入有误,请重新输入");
break;
}
System.out.println("您本次租车的租金为:"+cr+"元");
sum+=cr;
System.out.println("是否继续租车:y/n");
if(sc.next().equals("n"))
{
break;
}
}while(true);
System.out.println("您的总租金为:"+sum+"元");
}
}
abstract class Vehicle{
int dayrent;
public abstract int countrent(int day);
}
class Car extends Vehicle{
private String brand;
public void setBrand(String brand)
{
this.brand=brand;
switch(brand){
case "别克":
dayrent=300;
break;
case "途观":
dayrent=600;
break;
case "宝马":
dayrent=800;
break;
case "宾利":
dayrent=1500;
break;
}
}
public String getBrand()
{
return brand;
}
@Override
public int countrent(int day)
{
return day*dayrent;
}
}
class Bus extends Vehicle{
private int seat;
public void setSeat(int seat)
{
this.seat=seat;
if(seat>0&&seat<=12){
dayrent=800;
}else if(seat>12&&seat<=20){
dayrent=1200;
}else if(seat>20&&seat<=32){
dayrent=1800;
}
}
public int getSeat()
{
return seat;
}
@Override
public int countrent(int day)
{
return day*dayrent;
}
}
class Trucks extends Vehicle{
private int ton;
public void setTon(int ton){
this.ton=ton;
dayrent=ton*50;
}
public int getTon(){
return ton;
}
public int countrent(int day)
{
return day*dayrent;
}
}
package com.demo1107;
/**
* 笔记本usb案例
*/
public class USBDemo {
public static void main(String[] args) {
IUSB usb = new Disk();
Computer c = new Computer(usb);
c.start();
}
}
class Computer{
IUSB usb;
public Computer(IUSB usb) {
this.usb = usb;
}
public void start(){
System.out.println("笔记本正在启动,启动完毕,准备调用usb接口功能");
usb.work();
}
}
interface IUSB{
public void work();
}
class Disk implements IUSB{
@Override
public void work() {
System.out.println("我是1Tb的移动硬盘,正在读取数据");
}
}
class Feng implements IUSB{
@Override
public void work() {
System.out.println("我是电风扇,正在吹吹风");
}
}
package com.demo1107;
/**
* 打印机案例
*/
public class Printer {
public static void main(String[] args) {
IPaper p = new B5Paper();
IBox b = new BlackBox();
Print print = new Print(p, b);
print.println("你好,海文");
}
}
class Print{
IPaper paper;
IBox box;
public Print(IPaper paper,IBox box) {
this.paper = paper;
this.box = box;
}
public void println(String content){
paper.paper();
box.box();
System.out.println("正在打印:"+content);
}
}
interface IPaper{
public void paper();
}
interface IBox{
public void box();
}
class A4Paper implements IPaper{
public void paper() {
System.out.println("正在使用a4的纸张");
}
}
class B5Paper implements IPaper{
public void paper() {
System.out.println("正在使用b5的纸张");
}
}
class ColorBox implements IBox{
public void box() {
System.out.println("正在使用彩色的墨盒");
}
}
class BlackBox implements IBox{
public void box() {
System.out.println("正在使用黑白的墨盒");
}
}
package com.demo1107;
public class Demo2 {
public static void main(String[] args) {
IWasher w = new HaierWasher();//接口的类型指向实现类的对象
System.out.println("品牌是:"+w.brand);
w.wash();
}
}
interface IWasher{
static final String brand ="海尔";
public abstract void wash();
}
class HaierWasher implements IWasher{
@Override
public void wash() {
System.out.println("洗衣服咯");
}
}