继承几乎是所有oop语言重要的部分,java 是单继承,所有\的类都隐含继承Object类,继承的语法很简单,也挺容易理解,但是如果把继承跟其他放一起,例如 继承关系的执行顺序,对象创建过程等,就有些费劲,需要用点心。 具体了解 可参考我的另两篇文章
http://blog.csdn.net/xu511739113/article/details/52302498
http://blog.csdn.net/xu511739113/article/details/52316682
例如,我们要设计两个类 dog 和 cat ,首先我们先考虑 dog有什么属性,也就是dog"有什么"? 它有 年龄,性别,所以它有属性 sex 和 age ,其次考虑dog"会干什么",也就是它的方法,dog会 吃 会 睡 还能 看门 所以它有3个方法
public class Dog {
/** 年龄 */
private int age;
/** 性别 (true 男 false 女) */
private Boolean sex;
public void eat(){
System.out.println("eat~~");
}
public void sleep(){
System.out.println("sleep~~");
}
public void lookHome(){
System.out.println("lookHome~~");
}
/**
* 返回 #{bare_field_comment}
*
* @return #{bare_field_comment}
*/
public int getAge() {
return age;
}
/**
* 设置 #{bare_field_comment}
*
* @param age
* #{bare_field_comment}
*/
public void setAge(int age) {
this.age = age;
}
/**
* 返回 #{bare_field_comment}
*
* @return #{bare_field_comment}
*/
public Boolean getSex() {
return sex;
}
/**
* 设置 #{bare_field_comment}
*
* @param sex
* #{bare_field_comment}
*/
public void setSex(Boolean sex) {
this.sex = sex;
}
}
我们在考虑cat "有什么" cat 也有年龄 跟 性别 ,所以它有属性 sex,age 。cat "会什么" cat 会吃 会睡 还能捉老鼠 所有他三个方法
public class Cat {
/** 年龄 */
private int age;
/** 性别 (true 男 false 女) */
private Boolean sex;
public void eat(){
System.out.println("eat~~");
}
public void sleep(){
System.out.println("sleep~~");
}
public void catchingMouse(){
System.out.println("cat catchignMouse");
}
/**
* 返回 #{bare_field_comment}
*
* @return #{bare_field_comment}
*/
public int getAge() {
return age;
}
/**
* 设置 #{bare_field_comment}
*
* @param age
* #{bare_field_comment}
*/
public void setAge(int age) {
this.age = age;
}
/**
* 返回 #{bare_field_comment}
*
* @return #{bare_field_comment}
*/
public Boolean getSex() {
return sex;
}
/**
* 设置 #{bare_field_comment}
*
* @param sex
* #{bare_field_comment}
*/
public void setSex(Boolean sex) {
this.sex = sex;
}
}
设计完了之后我们发现 dog 和 cat 其实有好多共性的 属性和 方法是可以抽出来的,他们都有 性别 年龄, 都会 吃 睡 所以我们将共性部分抽出来,作为Animal
public class Animal {
/** 年龄 */
private int age;
/** 性别 (true 男 false 女) */
private Boolean sex;
public void eat(){
System.out.println("eat~~");
}
public void sleep(){
System.out.println("sleep~~");
}
/**
* 返回 #{bare_field_comment}
*
* @return #{bare_field_comment}
*/
public int getAge() {
return age;
}
/**
* 设置 #{bare_field_comment}
*
* @param age
* #{bare_field_comment}
*/
public void setAge(int age) {
this.age = age;
}
/**
* 返回 #{bare_field_comment}
*
* @return #{bare_field_comment}
*/
public Boolean getSex() {
return sex;
}
/**
* 设置 #{bare_field_comment}
*
* @param sex
* #{bare_field_comment}
*/
public void setSex(Boolean sex) {
this.sex = sex;
}
}
我们称 Animal为 dog 和 cat 的父类,反之dog 和 cat 为 Animal的子类,在java代码中我们就可以使用extends 来实现继承关系,这样我们就可以这样来重新设计
public class Dog extends Animal{
public void lookHome(){
System.out.println("lookHome~~");
}
}
public class Dog extends Animal{
public void lookHome(){
System.out.println("lookHome~~");
}
}
public class TestExtends {
public static void main(String[] args) {
new Dog().eat();
new Cat().eat();
}
}
运行结果
eat~~
eat~~
极大提高了代码的可重用性.给人感觉更加直观,层次分明。
如果我们有特殊需求还可能在进行 方法重写,例如
public class Dog extends Animal{
public void lookHome(){
System.out.println("lookHome~~");
}
@Override
public void eat(){
System.out.println("dog eat Bone");
}
}
public class Cat extends Animal{
public void catchingMouse(){
System.out.println("cat catchignMouse");
}
@Override
public void eat(){
System.out.println("cat eat fish");
}
}
public class TestExtends {
public static void main(String[] args) {
new Dog().eat();
new Cat().eat();
}
}
运行结果
dog eat Bone
cat eat fish
最后 java中有些方法是不能被继承的
1.构造方法
2.private 修饰的属性和方法(注:平时做项目是一个javaBean 有时候想扩展字段,重新建个bean来继承它,其实是继承了get/set 方法)
3.不在同一个包下,并用default修饰的属性和方法
4.final修饰的类不能被继承
5.final 修饰的方法虽然能被继承 但不能被重写
其实也就是,只有子类能够访问到的属性和方法才能被继承。