Java继承,重写

一、继承的概念
继承是java面向对象编程技术的一块基石,因为它允许创建分等级层次的类。
继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法,使得子类具有父类相同的行为。
二、类的继承格式

class 父类 {

}
class 子类 extends 父类 {

}

三、继承类型
需要注意的是 Java 不支持多继承,但支持多重继承
在这里插入图片描述
在这里插入图片描述

public class ExtendsDemo1 {
     public static void main(String[] args) {
           ChildClass childClass = new ChildClass();
           System.out.println(childClass.intA);
           childClass.print();
     }
}
class BaseClass{
     int intA = 10;
     public void print(){
           System.out.println("---print");
     }
}
class ChildClass extends BaseClass{
     
}
class ChildClass2 extends ChildClass{

     
}
示例:

public class Bird {
     int weight=0;
     public void fly(){
           System.out.println("我是鸟,我可以飞!");
     }
     public void eatting(){
           System.out.println("....吃.....");
     }
}
/**
* Eagle 鹰
* @author sxj
* 修饰符 class subclass extends super class
* {
*
* }
* 继承了 Bird中的fly和eatting方法
*/
public class Eagle extends Bird{


}
public class Demo1 {
     public static void main(String[] args) {
           Eagle eagle = new Eagle();
           eagle.weight = 3;
           System.out.println(eagle.weight);
           eagle.fly();
           eagle.eatting();
     }
}

2.方法的重写
重写是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变。
重写的好处在于子类可以根据需要,定义特定于自己的行为。 也就是说子类能够根据需要实现父类的方法。
重写方法不能抛出新的检查异常或者比被重写方法申明更加宽泛的异常。例如: 父类的一个方法申明了一个检查异常 IOException,但是在重写这个方法的时候不能抛Exception 异常,因为 Exception 是 IOException 的父类,只能抛出 IOException 的子类异常。
方法的重写规则:
•参数列表必须完全与被重写方法的相同;
•返回类型必须完全与被重写方法的返回类型相同;
•访问权限不能比父类中被重写的方法的访问权限更低。例如:如果父类的一个方法被声明为public,那么在子类中重写该方法就不能声明为protected。
•父类的成员方法只能被它的子类重写。
•声明为final的方法不能被重写。
•声明为static的方法不能被重写,但是能够被再次声明。
•子类和父类在同一个包中,那么子类可以重写父类所有除了声明为private和final的方法。
•子类和父类不在同一个包中,那么子类只能够重写父类的声明为public和protected的非final方法。
•重写的方法能够抛出任何非强制异常,无论被重写的方法是否抛出异常。但是,重写的方法不能抛出新的强制性异常,或者比被重写方法声明的更广泛的强制性异常,反之则可以。
•构造方法不能被重写。
•如果不能继承一个方法,则不能重写这个方法。

//----Bird.java
2./**
3. * Bird 鸟
4. * @author sxj
5. *
6. */
7.public class Bird {
8.    //
9.    int weight=0;
10.    //private 成员变量
11.    private int intPrivate=0;
12.
13.    //--方法
14.    public void fly(){
15.        System.out.println("我是鸟,我可以飞!");
16.    }
17.
18.    public void eatting(){
19.        System.out.println("....吃.....");
20.    }
21.
22.    // private 方法
23.    private void methodPrivate(){
24.        System.out.println("....test.....");
25.    }
26.}
27.
28.//----Ostrch.java
29./**
30. * Ostrch 鸵鸟
31. * @author sxj
32. *
33. */
34.public class Ostrch extends Bird{
35.    int weigth=10;
36.    //重写父类的方法 (override)
37.    public void fly(){
38.        System.out.println("我是鸵鸟,虽然我是鸟但我不能飞!");
39.    }
40.}
41.
42.//----Demo2.java
43.public class Demo2 {
44.    public static void main(String[] args) {
45.        //实例化 Ostrch 类
46.        Ostrch ostrch=new Ostrch();
47.        ostrch.fly();
48.        ostrch.eatting();
49.    }
50.}

3.super 与 this 关键字
super关键字:我们可以通过super关键字来实现对父类成员的访问,用来引用当前对象的父类。
this关键字:指向自己的引用。

//----Bird.java
2./**
3. * Bird 鸟
4. * @author sxj
5. *
6. */
7.public class Bird {
8.    //
9.    int weight=0;
10.    //private 成员变量
11.    private int intPrivate=0;
12.
13.    //--方法
14.    public void fly(){
15.        System.out.println("我是鸟,我可以飞!");
16.    }
17.
18.    public void eatting(){
19.        System.out.println("....吃.....");
20.    }
21.
22.    // private 方法
23.    private void methodPrivate(){
24.        System.out.println("....test.....");
25.    }
26.}
27.
28.//----Ostrch.java
29.
30./**
31. * Ostrch 鸵鸟
32. * @author sxj
33. *
34. */
35.public class Ostrch extends Bird{
36.    int weight=10;
37.    //重写父类的方法 (override)
38.    public void fly(){
39.        System.out.println("我是鸵鸟,虽然我是鸟但我不能飞!");
40.    }
41.
42.    //super this 关键字
43.    public void spuerThisDemo(){
44.        //this,调用自身的方法或成员变量
45.        System.out.println(this.weight);
46.        this.fly();
47.        //super,子类中调用父类的方法或成员变量
48.        System.out.println(super.weight);
49.        super.fly();
50.    }
51.}

4.重写与重载之间的区别
在这里插入图片描述
•方法重载是一个类中定义了多个方法名相同,而他们的参数的数量不同或数量相同而类型和次序不同,则称为方法的重载(Overloading)。
•方法重写是在子类存在方法与父类的方法的名字相同,而且参数的个数与类型一样,返回值也一样的方法,就称为重写(Overriding)。
•方法重载是一个类的多态性表现,而方法重写是子类与父类的一种多态性表现。

猜你喜欢

转载自blog.csdn.net/weixin_42530700/article/details/89603621