[Java入门笔记] 面向对象三大特征之:继承

理解什么是继承

首先我们知道,面对对象有三大特征:

封装:解决了数据的安全性问题
继承:解决了代码的重用问题
多态:解决了程序的扩展问题
上一篇博客中,我们了解了一下封装,现在我了再来看看什么是继承。
在现实生活中,我们可以把封装理解成儿子对父亲财产的继承。而在面向对象程序设计中的继承,是一个对象从另一个对象获取属性和方法的过程。

继承是利用现有的类创建新类的过程,现有的类称作基类(父类),创建的新类称为派生类(子类),派生类可以拥有所有基类的非私有的属性和方法。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAl8AAADcCAIAAADBZlMVAAAgAElEQVR4nO2dMYwbx/WHWbp0mdKlywBqUqp0Q8ClS1cCS6tLQcCd3BBwI5whwoCKE0wYCJQDBcSyYMLBBTDhw8HHO8Q5M4gi0Ypzt4EJ8aRIggL98f7F7L0dzs7Ozi6Hu4/L34cpYmq5u9yZ7HdvZt5MiwAAAACwTKvuGwAAAADEATsCAAAAJrAjAAAAYAI7AgAAACawIwCgKm61UFA2pcCOAICqqPt9h4LiX2BHAEBV1P2+Q0HxL7AjAKAq+NUDgFhgRwBA1cCOQD6wIwCgamBHIB/YEQBQNbAjkA/sCACoGtgRyAd2BABUDewI5AM7AgCqBnYE8oEdAQBVAzsC+cCOAICqaaQdJxOaTFb6+nRa/uutFrVa1O1SFJU/iYNej1ot6nRoNlv6fLGgxWItV6wd2BEAUDWNtONoRK0WtdvF/LRY0GAQu63VKi9IPsMqhqZLC6ZPoj5XRf+B6vNud6WLygR2BABUTSPtSETdrqmoxcIuy+l0yTd6cTObxYcZMZzn133odCyi5bvVr8s302pRrxfg0qKAHQEAVdNUO+q2UP2N/Emvl1iHS7tN/T6NxwXCTRWhpgPNgHaMouRsg0H8odWO7Xb84Xgc4LrSgB0BAFXTVDuSFj4admy3i1kw9/z9/tLnAe1IRJOJecK0HYdDexTbGGBHAEDVNNiO0yl1uzQaURRRFCV2HI3yv5g7ZLhYZFowrB3pUsM8oGjYkX+X4fsoas4kHd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oEdAQBVAzsC+cCOAICqgR2BfGBHAEDVwI5APrAjAKBqYEcgH9gRAFA1sCOQD+wIAKga2BHIB3YEAFQN7AjkAzsCAKqm9qXtUVC8C+wIAKiKut93KCj+BXYEAFRF3e87FBT/8v8kcR7u+jU3qgAAAABJRU5ErkJggg==" alt="" width="384" height="139" />

例如,我们可以有一个类“人”,而我们要创建一个新的类“学生”,学生也是属于“人”的一种,有“人”这个类的一些共通属性行为,我们就可以把“学生”这个类继承自“人”,而还有“小学生”、“中学生”、“大学生”,他们都属于学生,我们可以让他们继承自“学生”类。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjUAAAGWCAIAAAAPF+USAAAcUElEQVR4nO3dPa7jxtLGcYYO3x15BdyHQi9AwAmdaANXMPCGzOWYqZkMYKYKFRkEbqDEcd2A45429dWUmv1R9f+BiY9ndKiprnooiqIaAQCgPE3uHQAA4A7yCQBQIvIJAFAi8gkAUCLyCQBQIvIJEBGR/zRsqjbUjyoCIkI+qdtQP6oIiAj5pG5D/agiICLCXFOCOipCFQERYa5pQR0VoYqAiDDXtKCOilBFQESYa1pQR0WoIiAizDUtqKMiVBEQEeaaFtRREaoIiAhzTQvqqAhVBESEuaYFdVSEKgIiwlzTgjoqQhUBEWGuaUEdFaGKgIgon2uHw6FpmvP5nHtHtqe6jtZQRUBElM+1xjMMQ+7d2ZLqOlpDFQERUT7X/Hza7/e5d2dLqutoDVUERET5XPPzKfe+bEx1Ha2hioCIKJ9r5BNqRBUBEVE+1/x8ulwuuXdnS6rraA1VBERE+Vzz82kcx9y7syXVdbSGKgIionmuXS4XP5/6vs+9R1vSW0eDqCIgIprn2iKfDodD7j3akt46GkQVARHRPNfGcfTzqW3b3Hu0Jb11NIgqAiKiea71fd/82/V6zb1Tm9FbR4OoIiAimuda13UumXa7nfJLJPTW0SCqCIiI5rk233xvfufpeDw2TdN1Xe6d2ozeOhpEFQER0TzX2rad86nruvlcn+a3oPTW0SCqCIiI5rnm3xnWXcun9l7meut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nLFBEbIB80qXGOta4z1igiNgA+aRLjXWscZ+xQBGxAfJJlxrrWOM+Y4EiYgPkky411rHGfcYCRcQGyCddaqxjjfuMBYqIDZBPutRYxxr3GQsUERsgn3SpsY417jMWKCI2QD7pUmMd3T6zKdiAeMgnXWqsY/aRyhZxA+Ihn3SpsY7ZRypbxA2I5yaf2HRsAFA58knpBgCVI5+UbgBQOQYZAKBE5BMAoETkEwCgROQTAKBE5BMAoETkEwCgROQTAKBE5BMAoETkEwCgROQTAKBE5BMAoET/A5S7irsdnzjBAAAAAElFTkSuQmCC" alt="" width="278" height="199" />

在继承中,最高层的应该是最具有普遍性的、最符合一般情况的,往下一层,都应该比上一层更具体,在继承中子类能够自动共享基类中的成员属性和成员方法。

如何使用继承

在Java中,要继承,需要使用extends关键字,继承的语法格式如下:

[访问修饰符] class 子类名 extends 父类名 {
成员列表
} 如 public Student extends Person{
......
}

例子:

按照上面的举例,我们先定义一个Person类

public class Person{
public String name;
public int age; public void sleep(){
System.out.println("我正在睡觉");
} public void eat(){
System.out.println("我正在吃饭");
}
}

对于“人”来说,人都有一个名字,都有年龄,能够睡觉,也可以吃饭。

而我学如果此时需要有一个学生类,学生也属与人,学生也都有人的特征,此时我们便可以使学生继承于人。

public class Student extends Person{
public String sid; //学号
public String cls; //班级 public void study(){
System.out.println("我正在学习");
} }

对于学生来说,学生有自己的学号,有自己的班级,还有学习等等功能。

public class Test{
public static void main(String[] args){
Student stu = new Student();
stu.name = "张三";
stu.age = 20; //我们可以为Student类型的对象设置name和age,因为S图等继承自Person,所以也拥有Person类的属性和方法。
stu.sleep();
stu.eat(); //我们也可以使用Person类的方法。 stu.sid = "S20150101001"; //当然,我们也可以使用Student自己的属性和方法
stu.cls = "1001";
stu.study(); Person p = new Person();
p.study(); //错误,父类不可以使用子类的属性和方法。
}
}

当然,如果还需要有大学生等,我们可以定义大学生,使大学生继承自学生类,大学生便可以拥有学生类和“人”类的属性和方法。

需要注意的是,Java中的继承是单继承,也就是说,只能继承一个父类,如果Student继承了Person类,那就不能再直接继承其他的类。

继承中的构造方法

需要注意的是,父类中的构造方法是不能被继承的。

同时需要我们注意的是,在对象的构建过程中,会先构建出父类,在构建出子类,这个比较好理解,就想在现实生活中一样,只有先有父亲,而后才有儿子一样。

例如:

class ParentClass {  //定义父类
public ParentClass() { //构造方法
System.out.println("这是父类的构造方法。");
}
} class ChildClass extends ParentClass { //子类继承于父类
public ChildClass() { //构造方法
System.out.println("这是子类的构造方法。");
}
} public class ConstructorTest { //该类用于容纳main方法
public static void main(String[] args) {
ChildClass cc = new ChildClass(); //实例化子类对象
}
}

执行输出:

这是父类的构造方法。
这是子类的构造方法。

因此:

  • 当实例化子类的对象时,必须先执行父类的构造方法,然后再执行子类的构造方法。
  • 如果父类还有更上级的父类,就会先调用最高父类的构造方法,再逐个依次地将所有继承关系的父类构造方法全部执行。
  • 如果父类的构造方法执行失败,那么子类的对象也将无法实例化。

super关键字

在Java中,用super关键字可以对父类进行操作。

super主要有两种用途:

第一种用途是:在子类的构造方法中,super关键字可以显式地调用父类的构造方法,用于将参数传递给它;
其语法是:super(参数);
需要注意的是:该条语句必须是构造方法中的第一条语句。
 
第二中用途是:如果子类中有和父类相同名字的成员,在子类中是默认使用子类中的成员的,但是我们可以super关键字显式调用父类中的成员。
语法:super.成员名
需要注意的是:父类中该成员不是private的。
 

Java中继承的特点与继承作用

特点

继承是单向的,子类可以访问父类中的成员,但父类无法访问子类的成员。

Java中只允许单继承,一个子类只能拥有一个父类。

作用

引入继承,实现了代码重用;

引入继承,实现了递增式的程序设计。

能减少代码和数据的重复冗余度,并通过增强一致性来减少模块间的接口和界面,从而增强了程序的可维护性;

能清晰地体现出类与类之间的层次结构关系。

上一篇:Java用JSONObject-lib来解析json串


下一篇:【c3p0】目前使用它的开源项目有Hibernate,Spring等