首页 > 代码库 > java之继承
java之继承
封装继承(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); }}
java之继承
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。