封装
继承(Inheritance)
继承的作用:提高代码的复用性和可维护性
软件中,1000个对话框,每个对话框单独编写需要100行代码,总共需要10万行代码
考虑每个对话框共同的部分占70行,独有的占30行。70行代码编写一次,重复使用。 总共需要:70+30*1000=3万行
--如何编写代码实现继承?
字体对话框
段落对话框
使用子类通过extends继承父类
父类:超类、基类,superClass
子类:派生类,扩展类
Java中的继承,比C++要简单的多
C++支持多重继承(一个子类同时多个父类),Java不支持
继承的几个性质:
(1)父类中的所有非私有成员都可以被子类继承(默认权限的成员,在包外不能被子类使用)
(2)一般情况下,将需要被继承的成员定义为protected的
(3)被正常继承的成员,可以在子类中当成自己的成员使用
继承的特殊情况:
(1)覆盖(重写override)
子类继承了父类的成员,万一子类中还定义了和父类相同类型和名称的成员,会不会有歧义?
子类中的成员会屏蔽掉父类成员,这叫做覆盖(Override)
注意:覆盖不允许使子类成员的访问权限比父类成员的访问权限严格
覆盖的作用?可保持子类的个性化
(2)父类和子类的关系
子类实例化时,系统会自动提前为该子类实例化父类对象
问题:父类构造函数万一有参数怎么办?在子类构造函数的第一句给父类的构造函数赋参数
继承:提高代码的复用性和可维护性
提高代码的复用性和可维护性除了继承之外,还有别的方法吗?
还有一种方法:组合继承是从父类直接将成员拿来为我所用
组合是使用另一个类的功能来完成本类的任务
某软件中,需要编写三个类:
第一个类:连接网络,将信息存到数据库
第二个类:连接网络,将网上信息存到文件
第三个类,接受用户输入,将信息存到数据库
class NetworkConn{}
class SaveToDatabase{}
class SaveToFile{}
class Input{}
class C1{
调用NetworkConn和SaveToDatabase
}
class C2{
调用NetworkConn和SaveToFile
}
class C3{
调用Input和SaveToDatabase
}
提高复用性方面,组合使用场合约为80%,继承为20%
组合分为四类
(1)Head实例化,Mouth自动实例化
生命周期组合,耦合性最强
class Mouth{}
class Head{
Mouth m;
Head(){m = new Mouth();}
}
(2)Car实例化,Engine没有实例化,可以后期使用set函数进行组装。后期组装
class Engine{}
class Car{
Engine e;
void setEngine(Engine e){
this.e = e;
}
}
(3)函数级组合,只在某个函数中能够使用
class Driver{}
class Car{
void drive(Driver d){
this.d = d;
}
}
(4)函数内部组合
class Driver{}
class Car{
void drive(){
Driver d = new Driver();
}
}
class Dialog{
protected String title;
void show(){ System.out.println("Dialog.show"); }
}
class FontDialog extends Dialog{//子类继承了父类
int size;
protected int show(){
System.out.println("FontDialog.show");
}
public static void main (String[] args) {
FontDialog fd = new FontDialog();
fd.show();
}
}
在重写当中呢,我们的方法的返回值不能更改,否者的话就不算是重写,我们在调用这个方法的时候呢我们就会出现错误。
class Dialog{
protected String title;
}
class FontDialog extends Dialog{//子类继承了父类
protected String title;
int size;
protected void show(){
title = "字体"; //特指子类中的title
super.title = "sadlfkjas;ldf";//特指父类中的title
}
public static void main (String[] args) {
FontDialog fd = new FontDialog();
fd.show();
}
}
//父类和子类的关系
class Dialog{
String title;
Dialog(){ System.out.println("Dialog构造函数"); }
}
class FontDialog extends Dialog{//子类继承了父类
FontDialog(){
System.out.println("FontDialog构造函数");
}
public static void main (String[] args) {
FontDialog fd1 = new FontDialog();
FontDialog fd2 = new FontDialog();
}
}
//父类和子类的关系
class Dialog{
String title;
Dialog(String title){
this.title = title;
System.out.println("Dialog构造函数");
}
}
class FontDialog extends Dialog{//子类继承了父类
FontDialog(){
super("字体"); //给父类构造函数赋参数,该句代码
//要放在子类构造函数的第一句
System.out.println("FontDialog构造函数");
}
public static void main (String[] args) {
FontDialog fd1 = new FontDialog();
System.out.println(fd1.title);
}
}
//父类和子类的关系
class Dialog{
String title;
Dialog(String title){
this.title = title;
System.out.println("Dialog构造函数");
}
}
class FontDialog extends Dialog{//子类继承了父类
FontDialog(){
System.out.println("FontDialog构造函数");
super("字体");
}
public static void main (String[] args) {
FontDialog fd1 = new FontDialog();
System.out.println(fd1.title);
}
}
来源:https://www.cnblogs.com/chang1203/p/5877927.html