final
被final修饰的变量不可被改变,又被称为常量
![](https://img-blog.csdnimg.cn/img_convert/18353362e094cfc4671a009924363ce8.png)
这里就报错了,证明不可改变
被final修饰的方法不可被重写
这里可以看到父类用final定义后,子类出现红色波浪线
![](https://img-blog.csdnimg.cn/img_convert/dba78c3637e7843325f4f2c5219d1d58.png)
![](https://img-blog.csdnimg.cn/img_convert/7185f6761a19128b29bad71692afbb68.png)
被final修饰的类不可被继承
如图可见,出错
![](https://img-blog.csdnimg.cn/img_convert/b1fd96c774174708de8afcc2bbb56bf9.png)
![](https://img-blog.csdnimg.cn/img_convert/3da79e3c7d6957e5022063fce86fe287.png)
final总结就是不可变
抽象
抽象:现实不存在的东西
在java中只声明不实现
抽象方法:用abstract修饰的方法,不可以有方法体,直接用分号结束即可
抽象类:如果一个类中有抽象方法,那这个类一定是抽象类
抽象类中类名前和方法前都要写abstract
![](https://img-blog.csdnimg.cn/img_convert/33f11646b9195588555c8f6e13332528.png)
子类重写了父类中所有定义了abstract类型的
![](https://img-blog.csdnimg.cn/img_convert/88d427f12161bf8dc801c45ff5635460.png)
main
![](https://img-blog.csdnimg.cn/img_convert/e3e81b89dadbb96174e60cbd468aa7a3.png)
![](https://img-blog.csdnimg.cn/img_convert/399e5d28fe6bfbaeb358a5bac5e3baf0.png)
注:可不能这样写哦,Anmial是抽象类,不可以创建对象
![](https://img-blog.csdnimg.cn/img_convert/c050c829049b90e031853b10269dc6a2.png)
特点:
抽象类不能创建对象
抽象类的子类必须重写父类中的抽象方法,否则子类也是抽象类
通过抽象类可以强制要求子类必须有哪些方法
接口
接口其实是一种特殊的抽象类
接口中所有的方法都是抽象方法
接口用interface来声明
![](https://img-blog.csdnimg.cn/img_convert/43ff26181e801259c0b5bb3a148a394c.png)
![](https://img-blog.csdnimg.cn/img_convert/4690b3a6cc30c7b5ad685485f19fd9c5.png)
![](https://img-blog.csdnimg.cn/img_convert/c56676abf4973eea09c974aab6dd1441.png)
总结:接口和继承类似,但是继承只能有一个,接口可以多实现,这就是弊端
类只能单继承,接口可以多实现
前两张图无变化
![](https://img-blog.csdnimg.cn/img_convert/99f1b73880011e5f508fdf94b015cb3f.png)
![](https://img-blog.csdnimg.cn/img_convert/aece0b556cf6ddee3e35089ded6f12d3.png)
Protet接口
![](https://img-blog.csdnimg.cn/img_convert/f8f37b17ac8eaaaeba62b2e33030cf82.png)
Gold中已经实现Interface,所以继承给Panda时不需要重写他们中的方法了
![](https://img-blog.csdnimg.cn/img_convert/d2eb68c44d306cfc61403a6482f723bc.png)
注意,虽然接口有多态性,但是使用时应注意 对象调用的方法在该类中是否拥有,如果没有就不行,如20行
![](https://img-blog.csdnimg.cn/img_convert/850ea8c9b98652ec047a90d5a2d8a2f8.png)
总结:接口同样具有多态性,接口把许多不相干的内容结合在一起
在上述接口Interface中定义一个变量
![](https://img-blog.csdnimg.cn/img_convert/bdd5ed1618427534282d930e2c7e43e4.png)
在main中修改他的值发现会出错,但是可以打印出他是多少
![](https://img-blog.csdnimg.cn/img_convert/4c05610d6e42f1d13c764a0c9b982b73.png)
特点:
接口的所有方法都是抽象方法,都是公开的
接口中所有的变量都是全局静态常量
面对对象练习3
现在有两种数据库,一种是mysql,一种是oracle,请通过程序设计,让用户自己选择数据库并进行增删查改
在main里,根据用户输入的编号自动创建一个数据库,并进行增删查改
两种思路
IDAO一般来讲是接口
![](https://img-blog.csdnimg.cn/img_convert/7c65b17c9cb2da0f58b60d569856cfb7.png)
对于第一种方法,我们会发现,有重复的方法,这样非常浪费时间
![](https://img-blog.csdnimg.cn/img_convert/3c5bedfc31d3cd1d783554f89c31dd54.png)
![](https://img-blog.csdnimg.cn/img_convert/c84a6b4f8caeb020698a429e1e94c2a8.png)
写过的内容需需要重复去写,不利
![](https://img-blog.csdnimg.cn/img_convert/49056e2093ad64547ac7ea8992623b0a.png)
所以我们用第二种方法
![](https://img-blog.csdnimg.cn/img_convert/683cb343cb36b33509c1d9caf27596ca.png)
![](https://img-blog.csdnimg.cn/img_convert/61b5265ad0a98f7579c23a1726874f4b.png)
![](https://img-blog.csdnimg.cn/img_convert/0a18be154da513106f69ebdc97ed3669.png)
![](https://img-blog.csdnimg.cn/img_convert/fd6352acf899820d3de738d2e72d7ecf.png)
![](https://img-blog.csdnimg.cn/img_convert/03bc16084f27f5652185825b0298796b.png)