一 回顾
1.继承中访问成员方法的特点:
重名:优先访问子类 不重名:优先方法子类 没有则向上查找
方法重载: 同一一个类 方法名相同 参数不同 与返回值类型无关
方法重写: 在继承中 方法名相同 参数相同 与返回值与访问修饰符有关
2.继承好处:
高扩展性 高可复用性 避免大量的冗余的代码
耦合度比较高 低耦合 高内聚
3.多态
概念:一个事物或者一个对象 多种形态
访问成员变量特点: 编译看左边 运行看左边
访问成员方法特点:编译看左边 运行看右边
多态向上转型:
语法:父类的类名 对象名 = new 子类的类名()
问题:不能调用子类独有方法
多态向下转型:
语法:子类的类名 对象名 =(需要转换的数据类型)父类对象的引用
问题:强制类型转换错误
多态两种体现: 以父类作为方法参数 以父类作为方法的返回值
多态好处:与继承类似
4.包:
作用:用于管理与分类源文件 文件夹
组成:
A.域名+公司的名称+项目的名称+包的含义
B.全部小写字母
C.不同的包名使用.分割
声明包: package 声明包 idea 自动声明
导包 import 包名+类名
A.使用不同包下的类就需要进行导包
B.使用同包下的类不需要进行导包 使用java.lang 包下的类都不需要进行导包
5.final 最终 最后
修饰成员
变量:修饰变量是常量 只能赋值一次
方法:不能被重写
类:不能被继承
6.访问修饰符: 需要对外提供访问使用public 不需要提供对外方法使用private
如果不想其它类实例化对象 使用私有的构造
7.代码块:
局部代码 构造代码块 静态代码块(重点)
二 作业1
step01 需求
step02 分析
step03 -父类
package com.qf.work01;
public class Animal {
public void eat(){
}
public void sleep(){
}
}
step04 子类
package com.qf.work01;
public class Rabbit extends Animal {
@Override
public void eat() {
System.out.println("吃草");
}
}
step05 子类
package com.qf.work01;
public class Tiger extends Animal {
@Override
public void eat() {
System.out.println("吃肉");
}
}
step06 测试类
package com.qf.work01;
/*父类: Animal 类 eat sleep
子类: Tiger Rabbit 重写这个方法
测试类进行测试*/
public class Test {
public static void main(String[] args) {
//使用多态实例化对象
Animal a = new Rabbit();
a.eat();
Animal a1 = new Tiger();
a1.eat();
}
}
三 作业2
step01 需求
step02 分析
step03 父类-代码
package com.qf.work02;
public class Pet {
private String name;
private int love;
private int health;
public Pet() {
}
public Pet(String name, int love, int health) {
this.name = name;
this.love = love;
this.health = health;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLove() {
return love;
}
public void setLove(int love) {
this.love = love;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public void print(){
System.out.println(name+"\t"+love +"\t"+health);
}
}
step04 子类
package com.qf.work02;
public class Dog extends Pet {
private String kind;
public Dog() {
}
public Dog(String name, int love, int health, String kind) {
super(name, love, health);//给父类的成员变量进行赋值
this.kind = kind;
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
@Override
public void print() {
super.print(); //调用父类的方法
System.out.println(kind);
}
}
step05 子类
package com.qf.work02;
public class Penguin extends Pet {
private String sex;
public Penguin() {
}
public Penguin(String name, int love, int health, String sex) {
super(name, love, health);
this.sex = sex;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public void print() {
super.print();
System.out.println(sex);
}
}
step06 测试类
package com.qf.work02;
public class Test {
public static void main(String[] args) {
//实例化狗类
Pet p = new Dog("欧欧",100,80,"泰迪");
p.print();
//实例化企鹅类
Pet p1 = new Penguin("qq",100,70,"q妹");
p1.print();
}
}
四 作业3
step01 需求
step02 分析
step03 父类-代码
package com.qf.work03;
public class Poultry {
private String name;
private String symptom;
private int age;
private String illness;
public Poultry() {
}
public Poultry(String name, String symptom, int age, String illness) {
this.name = name;
this.symptom = symptom;
this.age = age;
this.illness = illness;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSymptom() {
return symptom;
}
public void setSymptom(String symptom) {
this.symptom = symptom;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getIllness() {
return illness;
}
public void setIllness(String illness) {
this.illness = illness;
}
public void showSymptom(){
}
}
step04 子类
package com.qf.work03;
public class Duck extends Poultry{
public Duck() {
}
public Duck(String name, String symptom, int age, String illness) {
super(name, symptom, age, illness);
}
@Override
public void showSymptom() {
System.out.println("压力大"+super.getName() +"\t"+super.getSymptom()+"\t"+super.getAge()+"\t"+super.getIllness());
}
}
step05 测试类
package com.qf.work03;
public class Test {
public static void main(String[] args) {
//使用多态实例化对象
Poultry p = new Duck("小黄鸭","肾虚",1,"六味地黄丸");
p.showSymptom();
}
}
五 作业4
step01 需求
step02 分析
step03 代码
package com.qf.work04;
public class Auto {
private String brand;
private double length;
private double price;
public Auto() {
}
public Auto(String brand, double length, double price) {
this.brand = brand;
this.length = length;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String pay(){
return "";
}
}
step04 代码
package com.qf.work04;
public class SUV extends Auto {
public SUV() {
}
public SUV(String brand, double length, double price) {
super(brand, length, price);
}
public String pay(){
double length = super.getLength();
if (length >4295 && length <=5070){
return "SUV中型车";
}
return "";
}
}
step05 代码
package com.qf.work04;
public class Test {
public static void main(String[] args) {
Auto [] autos = new Auto[3];
autos[0]=new SUV("宝马",3,20000);
autos[1]=new SUV("奔驰",5000,20000);
autos[2]=new SUV("奥迪",4040,20000);
for (int i=0;i<autos.length;i++){
System.out.println(autos[i].pay());
}
}
}
六 抽象方法
1.概念:只有方法的声明 没有方法的实现 使用abstract来修饰
2.语法:
访问修饰符 abstract 返回类型 方法的名称(参数列表);
例子: public abstract void showInfo();
3.作用: 简化方法的编写 不用编写方法体
4.注意点:抽象方法必须在抽象类中
七 抽象类
1.概念: 抽象类也是一个普通类 使用abstract来修饰
2.语法:
访问修饰符 abstract class 类名 {}
例子:public abstract class Student{}
3.成员:
A.成员变量
B.构造方法
C.方法: 成员方法 抽象方法
4.抽象类与抽象方法关系:
抽象方法必须在抽象类 但是抽象类可以有抽象方法 也可以有普通的成员方法
5.注意点:
A.抽象类是不能实例化对象
B.抽象类的构造方法一般用给子类进行调用
C.子类必须实现抽象类中所有的抽象方法 除非子类也是抽象类
6.使用场景:
一般抽闲类作为父类 需要约束子类的一些规范的时候
八案例
step01 需求
step02 代码
package com.qf.demo02;
public abstract class Girl {
public void goShopping(){
System.out.println("买神仙水..........");
}
public abstract void findBoy();
}
step03 代码
package com.qf.demo02;
public class WuGirl extends Girl {
@Override
public void findBoy() {
System.out.println("发际线高的男朋友");
}
}
step04 代码
package com.qf.demo02;
public class ShenGirl extends Girl {
@Override
public void findBoy() {
System.out.println("找有钱的人");
}
}
step05测试类
package com.qf.demo02;
public class Test {
public static void main(String[] args) {
Girl g = new ShenGirl();
g.goShopping();
g.findBoy();
Girl g1 = new WuGirl();
g1.goShopping();
g1.findBoy();
}
}
九 案例
step01 需求
step02 分析
step03 代码
package com.qf.demo03;
public abstract class Emp {
private int eid;
private String name;
private double sal;
public Emp() {
}
public Emp(int eid, String name, double sal) {
this.eid = eid;
this.name = name;
this.sal = sal;
}
public int getEid() {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public abstract void work();
}
step04 代码
package com.qf.demo03;
public class Programmer extends Emp {
private double meoney;
public Programmer() {
}
public Programmer(int eid, String name, double sal, double meoney) {
super(eid, name, sal);
this.meoney = meoney;
}
public double getMeoney() {
return meoney;
}
public void setMeoney(double meoney) {
this.meoney = meoney;
}
@Override
public void work() {
System.out.println("撸代码......");
}
}
step05 代码
package com.qf.demo03;
public class Manager extends Emp {
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Manager() {
}
public Manager(int eid, String name, double sal, double money) {
super(eid, name, sal);
this.money = money;
}
@Override
public void work() {
System.out.println("怼程序员......");
}
}
step06 代码
package com.qf.demo03;
public class Tester extends Emp {
public Tester() {
}
public Tester(int eid, String name, double sal) {
super(eid, name, sal);
}
@Override
public void work() {
System.out.println("怼项目经理..........");
}
}
step07代码
package com.qf.demo03;
public class Test {
public static void main(String[] args) {
//实例化对象
Emp e = new Manager(1001,"圣源",20000,2000);
e.work();
System.out.println(e.getEid()+"\t"+e.getName()+"\t"+e.getSal());
//向下转型
Manager m = (Manager) e;
System.out.println(m.getMoney());
Emp e1 = new Programmer(1002,"叶总",18000,2000);
e1.work();
Emp e2 = new Tester(1003,"启龙",20000);
e2.work();
}
}
十 接口
1.概念:
生活中接口 usb 水龙头接口 花洒 .....
开发中的接口:提供了一些规范以及约束 规范:就是定义了一些抽象方法
2.接口使用的关键字:interface
3.定义的语法:
访问修饰符 interface 接口名称{}
例子:public interface inner{}
注意点:
A.将类的关键字换成interface B.接口编译之后也是字节码文件
4.成员:
变量:接口的变量都是静态的常量 默认都使用 public static final来进行修饰
构造方法:没有构造方法
方法: 在jdk1.8之前 只能由抽象方法 方法默认都会加上 public abstract
在jdk1.8之后 可以有私有的方法 以及静态方法 默认的方法
5.注意点:
A.接口不能实例化对象
B.接口中的实现类必须实现接口中所有的抽象方法
C.接口可以有多个实现类
6.接口的实现类:
概念:实现接口的普通的java类
关键字: implements
分类:单实现 多实现
7.单实现:
概念:实现类实现一个接口
语法: 访问修饰符 class 类名 implements 接口 {}
例子:public class Impl implements inner{}
原则:实现接口中所有的抽象方法
8.多实现:
概念:实现类实现多个接口 解决java单继承的问题
语法:访问修饰符 class 类名 implements 接口1,接口2 {}
例子:public class Impl implements inner,Inner{}
原则:实现接口中所有的抽象方法
注意点:实现多个接口 多个接口有相同名抽象方法 实现类只需要实现一个抽象类方法啊
代码-接口
package com.qf.demo04;
public interface Inner {
int num =100;
void showInfo();
}
代码-实现类
package com.qf.demo04;
public class Impl implements Inner {
@Override
public void showInfo() {
}
}
代码-测试类
package com.qf.demo04;
public class Test {
public static void main(String[] args) {
// Inner in = new Inner();
}
}
多实现的代码
package com.qf.demo05;
public interface Inner {
void show();
void pay();
}
package com.qf.demo05;
public interface Inner01 {
void showInfo();
void pay();
}
package com.qf.demo05;
public class Impl implements Inner,Inner01 {
@Override
public void show() {
System.out.println("show");
}
@Override
public void pay() {
System.out.println("支付");
}
@Override
public void showInfo() {
System.out.println("showInfo");
}
}
十一案例
step01 组装一台打印机
step02 分析
step03 代码
package com.qf.demo06;
public interface Pager {
String getPager();
}
step04 代码
package com.qf.demo06;
public class A3Pager implements Pager {
@Override
public String getPager() {
return "A3纸张";
}
}
step05 代码
package com.qf.demo06;
public class A4Pager implements Pager {
@Override
public String getPager() {
return "A4纸张";
}
}
step06 代码
package com.qf.demo06;
public interface Link {
String getLink();
}
step07代码
package com.qf.demo06;
public class BlackLink implements Link{
@Override
public String getLink() {
return "黑色墨盒";
}
}
step08 代码
package com.qf.demo06;
public class ColorLink implements Link {
@Override
public String getLink() {
return "彩色墨盒";
}
}
step09代码
package com.qf.demo06;
public class Printer {
private Pager pager;
private Link link;
public void setPager(Pager pager) {
this.pager = pager;
}
public void setLink(Link link) {
this.link = link;
}
public void print(){
System.out.println(pager.getPager()+"\t"+link.getLink());
}
}
step10代码
package com.qf.demo06;
public class Test {
public static void main(String[] args) {
//实例化打印机
Pager pa = new A3Pager();
Link l = new BlackLink();
Printer p = new Printer();
p.setPager(pa);
p.setLink(l);
p.print();
}
}
十二 类与接口 接口与接口之间的关系
1.类与类:只能单继承 多层继承 不支持多继承
2.类与接口:类可以实现单接口 也可以实现多接口
3.接口与接口:接口与接口可以单继承 也可以多继承
例子:public interface Inner02 extends Inner,Inner01 {}
十三案例
step01 需求
step02 分析
接口 USB 接口 开启 工作 关闭
实现类: 鼠标类 键盘类
笔记本:开机 使用usb功能 关机
step03 -定义接口
package com.qf.demo08;
public interface USBInterface {
void open();
void work();
void close();
}
step04-键盘
package com.qf.demo08;
public class KeyBoard implements USBInterface {
@Override
public void open() {
System.out.println("打开键盘......");
}
@Override
public void work() {
System.out.println("正在撸键盘.....");
}
@Override
public void close() {
System.out.println("正在关闭键盘......");
}
}
step05-鼠标
package com.qf.demo08;
public class Mouse implements USBInterface {
@Override
public void open() {
System.out.println("正在开启.......");
}
@Override
public void work() {
System.out.println("正在瞎点......");
}
@Override
public void close() {
System.out.println("正在关闭........");
}
}
step06-笔记本
package com.qf.demo08;
public class NoteBook {
public void open(){
System.out.println("笔记本开启.....");
}
public void useUsb(USBInterface usb){
usb.open();
usb.work();
usb.close();
}
public void close(){
System.out.println("关机.......");
}
}
step07-测试类
package com.qf.demo08;
public class Test {
public static void main(String[] args) {
//实例化笔记本对象
NoteBook note = new NoteBook();
note.open();
//实例化键盘的对象
USBInterface usb = new KeyBoard();
note.useUsb(usb);
//使用鼠标
USBInterface usb1= new Mouse();
note.useUsb(usb1);
note.close();
}
}
十四 内部类
1.概念:定义在类中的类 或者是定义在方法的类 就是内部类
2.分类:
成员内部类:普通成员内部类 私有内部类 静态内部类
局部内部类:定义在方法中的类
匿名内部类 没有名称的内部类
3.普通的成员内部类
位置:定义在类中 方法外
语法:
访问修饰符 class 外部类名 {
访问修饰符 class 内部类名{
}
}
说明:
A.内部类是可以访问外部类的私有的属性以及方法
B.外部类访问内部类的私有 必须实例化内部类对象才能访问
C.外部类以外的类访问内部类的资源 需要同外部类来实例化内部类对象
语法: 外部类的类名.内部类 对象名 = new 外部类的类名().new 内部类();
4.私有内部类
位置:定义在类中 方法外
语法:
访问修饰符 class 外部类名 {
private class 内部类名{
}
}
说明:
A.内部类是可以访问外部类的私有的属性以及方法
B.外部类访问内部类的私有 必须实例化内部类对象才能访问
C.外部类以外的类不能直接实例化内部类对象 但是可以通过外部类来调用其方法获取属性
5.静态内部类:
位置:定义在类中 方法外
语法:
访问修饰符 class 外部类名 {
static class 内部类类名{
}
}
说明:
A.静态内部类中方法只能获取外部类的静态资源
B.外部类访问静态内部类的资源 只能通过实例化对象来获取
C.外部类以外的类访问静态内部类的语法:
外部类.内部类 对象名 = new 外部类.内部类()
例子: OutClass.IntClass in = new OutClass.IntClass();
6.局部内部类
位置:定义在方法中
语法:
访问修饰符 返回值类型 方法名(){
class 类名 {
}
}
说明:
A.局部内部类只能间接进行调用内部类的资源
7.匿名内部类
位置:一般是定义在方法在方法的参数中
注意点:一般是与接口或者抽象类搭配的使用
代码-成员内部类
package com.qf.demo09;
public class OutClass {
private int num=20;
public void showInfo(){
System.out.println("哈哈哈!我是外部类");
InClass in = new InClass();
in.show();
System.out.println(in.inNum);
}
public class InClass{
private int inNum=400;
public void show(){
System.out.println(num);
showInfo();
}
}
}
package com.qf.demo09;
public class Test {
public static void main(String[] args) {
// OutClass.InClass in = new OutClass().new InClass();
// in.show();
/* OutClass outClass = new OutClass();*/
}
}
代码-私有内部类
package com.qf.demo10;
public class OutClass {
private int numOut=10;
public void showInfo(){
IntClass in = new IntClass();
in.showInfo();
}
private class IntClass{
public int numInt =20;
public void showInfo(){
System.out.println(numOut);
}
}
}
package com.qf.demo10;
public class Test {
public static void main(String[] args) {
OutClass out = new OutClass();
out.showInfo();
}
}
代码- 局部内部类
package com.qf.demo12;
public class OutClass {
public void showInfo(){
class InnerClass{
private int num=20;
public void show(){
System.out.println("哈哈");
}
/* public void show(){
InnerClass in = new InnerClass();
in.showInfo();
}
*/
}
InnerClass in = new InnerClass();
in.show();
}
}
package com.qf.demo12;
public class Test {
public static void main(String[] args) {
OutClass outClass = new OutClass();
outClass.showInfo();
}
}
代码-匿名内部类
package com.qf.demo13;
public class Test {
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
}
});
showInfo(new Inner() {
@Override
public void show() {
}
});
show(new Inner01() {
@Override
public void showInfoInner01() {
System.out.println("dddd");
}
});
}
public static void showInfo(Inner inner){
inner.show();
}
public static void show(Inner01 inner01){
inner01.showInfoInner01();
}
}
十五 综合的案例
step01 需求
step02 分析
step03 代码
package com.qf.demo14;
/**
* 车类
*/
public abstract class Vehicles {
private String brand;
private String noId;
private double rent;
public Vehicles() {
}
public Vehicles(String brand, String noId, double rent) {
this.brand = brand;
this.noId = noId;
this.rent = rent;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getNoId() {
return noId;
}
public void setNoId(String noId) {
this.noId = noId;
}
public double getRent() {
return rent;
}
public void setRent(double rent) {
this.rent = rent;
}
public abstract double pay(int days);
}
step04 代码
package com.qf.demo14;
/**
* 客车类
*/
public class Bus extends Vehicles{
private int seatCount;
public Bus() {
}
public Bus(String brand, String noId, double rent, int seatCount) {
super(brand, noId, rent);
this.seatCount = seatCount;
}
public int getSeatCount() {
return seatCount;
}
public void setSeatCount(int seatCount) {
this.seatCount = seatCount;
}
@Override
public double pay(int days) {
if (days >=150){
return days*0.6;
}else if(days >= 30){
return days *0.7;
}else if(days >=7){
return days *0.8;
}else if(days >=3){
return days*0.9;
}
return days;
}
}
step05 代码
package com.qf.demo14;
/**
* 轿车类
*/
public class Car extends Vehicles{
private String type;
public Car() {
}
public Car(String brand, String noId, double rent, String type) {
super(brand, noId, rent);
this.type = type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public double pay(int days) {
if (days >=150){
return days*0.7;
}else if(days >30){
return days*0.8;
}else if(days >7){
return days*0.9;
}
return days;
}
}
step06 代码
package com.qf.demo14;
public class Player {
private Vehicles [] vehicles = new Vehicles[8];
//定义方法存储车
public void init(){
// String brand, String noId, double rent, String type
vehicles[0]= new Car("宝马","京88888",800,"X6");
vehicles[1]= new Car("宝马","京99999",600,"550i");
vehicles[2]= new Car("别克","京77777",300,"林荫大道");
vehicles[3]= new Car("别克","京66666",600,"GL8");
//String brand, String noId, double rent, int seatCount
vehicles[4] = new Bus("京杯","55555",800,16);
vehicles[5] = new Bus("京杯","44444",1500,34);
vehicles[6] = new Bus("京龙","33333",800,16);
vehicles[7] = new Bus("京龙","22222",1500,34);
}
//定义方法找车
public Vehicles findVehicles(String brand,String type,int seatCount){
if (vehicles !=null && vehicles.length >0) {
for (int i=0;i<vehicles.length;i++) {
Vehicles v = vehicles[i];
if( v instanceof Car){
Car c = (Car) v;
if (brand.equals(c.getBrand()) && type.equals(c.getType())) {
return c;
}
}else if(v instanceof Bus){
Bus b = (Bus) v;
if (brand.equals(b.getBrand()) && seatCount ==b.getSeatCount()) {
return b;
}
}
}
}
return null;
}
}
step07 代码
package com.qf.demo14;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//实例化操作类的对象
Player p = new Player();
p.init();
Scanner input = new Scanner(System.in);
System.out.println("欢迎来到********租车系统*******");
System.out.println("1.轿车\t2.客车");
System.out.println("请输入租车的类型");
String brand="";
String type="";
int seatCount=0;
int id =input.nextInt();
if (id ==1){
System.out.println("请输入品牌:1.别克\t2.宝马");
int brandId = input.nextInt();
if (brandId ==1){
brand="别克";
System.out.println("请输入型号1.林荫大道\t2.GL8");
int typeId =input.nextInt();
if (typeId ==1){
type="林荫大道";
}
}
}
Vehicles ve = p.findVehicles(brand,type,seatCount);
System.out.println("请输入租赁天数");
int days =input.nextInt();
System.out.println("车牌号:"+ve.getNoId());
System.out.println("需要支付金额:"+ve.getRent() * ve.pay(days));
}
}