In-depth understanding of java inherited from "My dad is Li Gang," Let's talk about

Introduction
This article explains many java inheritance, designed to allow beginners easy to understand, as to "My dad is Li Gang," Anyway, the landlord does not know who dad is Li Gang.
@

1, inherited Overview

1.1, inheritance origin

As for the origin of simple sentence: the presence of the same properties and behavior when a plurality of classes, the extracted content to a single class, then the class do not need to define a plurality of these properties and behavior.

Inheritance describes the relationship between things belong, this relationship is a is-arelationship.

1.2, the definition of inheritance

Inheritance: is the subclass inherits the parent class 属性and 行为such subclasses parent class object has the same attributes, the same behavior. Subclass can directly access the parent class 非私有attributes and behavior.

Here again it clear that the parent class is also called the superclass or base class . The subclass also known as derived classes it is very foundation!

1.3, the inherited advantages

  1. Improve code reusability .
  2. Generate relations between classes, polymorphic made a perfect bedding (do not understand it does not matter, then I will write another article in more than one state)

Although inherited many advantages but Java supports only single inheritance, do not support multiple inheritance .

1.4, the inherited form

By extendskeyword, you can declare a subclass inherits another parent class is defined in the following format:

  class 父类 {
   ... 
   }
   class 子类 extends 父类 { 
   ... 
   } 

2, after about inherited member variables

When generating a relationship between classes, member variables of various kinds, and what impact it? After about inherited member variables to start in two ways, one is not a member variable aspects of the same name, the second is the member variable aspects of the same name.

2.1, member variables are not the same name

If the member variable of the same name does not appear subclass the parent class, then access is no influence of. code show as below:

  class liGang {
        // 父类中的成员变量。
       String name ="李刚";//------------------------------父类成员变量是name
    }
    class LiXiaoGang extends liGang {
        // 子类中的成员变量
        String name2 ="李小刚";//--------------------------子类成员变量是name2
        // 子类中的成员方法
        public void show() {
            // 访问父类中的name,
            System.out.println("我爸是"+name);
            // 继承而来,所以直接访问。
            // 访问子类中的name2
            System.out.println("我是"+name2);
        }
    }
public class Demo {
        public static void main(String[] args) {
            // 创建子类对象
            LiXiaoGang z = new LiXiaoGang();
            // 调用子类中的show方法
            z.show();
        }
    }
    //演示结果: 我爸是李刚   我是李小刚

2.2, the same name member variables

If the member variable of the same name appears subclass the parent class, then access is influential in. code show as below:

class liGang {
        // 父类中的成员变量。
       String name ="李刚";//------------------------------父类成员变量是name
    }
    class LiXiaoGang extends liGang {
        // 子类中的成员变量
        String name ="李小刚";//---------------------------子类成员变量也是name
        // 子类中的成员方法
        public void show() {
            // 访问父类中的name,
            System.out.println("我爸是"+name);
            // 继承而来,所以直接访问。
            // 访问子类中的name2
            System.out.println("我是"+name);
        }
    }
public class Demo {
        public static void main(String[] args) {
            // 创建子类对象
            LiXiaoGang z = new LiXiaoGang();
            // 调用子类中的show方法
            z.show();
        }
    }
    //演示结果: 我爸是李小刚   我是李小刚

When a member variable of the same name appeared sub-parent classes, subclasses need access to the parent class-Africa private member variables, use the superkeyword, as before modifying the parent class member variable, similar to the learned this. Use the formatsuper.父类成员变量名

thisRepresented by this object, superit means that the parent object, similar to the usage!

class liGang {
        // 父类中的成员变量。
       String name ="李刚";
    }
    class LiXiaoGang extends liGang {
        // 子类中的成员变量
        String name ="李小刚";
        // 子类中的成员方法
        public void show() {
            // 访问父类中的name,
            System.out.println("我爸是"+super.name);
            // 继承而来,所以直接访问。
            // 访问子类中的name2
            System.out.println("我是"+this.name);  //当然this可省略
        }
    }
public class Demo {
        public static void main(String[] args) {
            // 创建子类对象
            LiXiaoGang z = new LiXiaoGang();
            // 调用子类中的show方法
            z.show();
        }
    }
    //演示结果: 我爸是李刚   我是李小刚

2.3, a problem of succession in the member variable worth considering

Students Have you thought about such a problem. If the member variable parent class of
non-private: the subclass can be directly accessed.
Private: subclass is not directly accessible. As follows:
Here Insert Picture Description
Of course, you want the students to experience their own experience compilation error process, the plug did not experience a sense of awkward, ~ vomit, no place to place you this charm, unreasonable demands, my Buddha, okay ~

  class liGang2 {
        // 父类中的成员变量。
        private String name ="李刚";

    }
    class LiXiaoGang2 extends liGang2 {
        // 子类中的成员变量
        String name ="李小刚";
        // 子类中的成员方法
        public void show() {
            // 访问父类中的name,
            System.out.println("我爸是"+super.name);//------编译失败不能直接访问父类私有属性(成员变量)
            // 继承而来,所以直接访问。
            // 访问子类中的name2
            System.out.println("我是"+this.name);  //当然this可省略
        }
    }
public class PrivateVariable {
        public static void main(String[] args) {
            // 创建子类对象
            ExtendDemo.LiXiaoGang z = new ExtendDemo.LiXiaoGang();
            // 调用子类中的show方法
            z.show();
        }
    }

Normally development of coding, we follow the principle of the package, using the privatemodified member variable, then how to access the private member variable parent do? In fact, this time in public in the parent class getXxxmethod and the setXxxmethod on it. code show as below:

class liGang {
        // 父类中的成员变量。
      private String name ="李刚";

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
    class LiXiaoGang extends liGang {
        // 子类中的成员变量
        String name ="李小刚";
        // 子类中的成员方法
        public void show() {
            // 访问父类中的name,
            System.out.println("我爸是"+super.getName());
            // 继承而来,所以直接访问。
            // 访问子类中的name2
            System.out.println("我是"+this.name);  //当然this可省略
        }
    }
public class Demo {
        public static void main(String[] args) {
            // 创建子类对象
            LiXiaoGang z = new LiXiaoGang();
            // 调用子类中的show方法
            z.show();
        }
    }
    //演示结果: 我爸是李刚   我是李小刚

analyse as below:Here Insert Picture Description

3, members of the methods later on inheritance

分析完了成员变量,现在我们一起来分析分析成员方法。
想一想,当类之间产生了关系,其中各类中的成员方法,又产生了哪些影响呢? 同样我们依旧从两方面分析。
#### 3.1、成员方法不重名
如果子类父类中出现不重名的成员方法,这时的调用是没有影响的。对象调用方法时,会先在子类中查找有没有对 应的方法,若子类中存在就会执行子类中的方法,若子类中不存在就会执行父类中相应的方法。代码如下:

 class liGang3 {
        // 父类中的成员方法。
       public void zhuangRen1(){//--------------------------父类方法名zhuangRen1
           System.out.println("我叫李刚,人不是我撞的,别抓我,我不认识李小刚");
       }
    }
    class LiXiaoGang3 extends liGang3 {

        // 子类中的成员方法
        public void zhuangRen() {//--------------------------子类方法名zhuangRen
            System.out.println("有本事你们告去,我爸是李刚");  
        }
    }
    public class MemberMethod {
        public static void main(String[] args) {
            // 创建子类对象
            LiXiaoGang3 liXiaoGang = new LiXiaoGang3();
            // 调用子类中的show方法
            liXiaoGang.zhuangRen();
            liXiaoGang.zhuangRen1();
        }
    }
    
打印结果:有本事你们告去,我爸是李刚
        我叫李刚,人不是我撞的,别抓我,我不认识李小刚

Here Insert Picture Description
#### 3.2、成员方法重名 【方法重写】
成员方法重名大体也可以分两种情况:

1、方法名相同返回值类型、参数列表却不相同(优先在子类查找,没找到就去父类)
2、方法名、返回值类型、参数列表都相同,没错这就是重写(Override)

这里主要讲方法重写 :子类中出现与父类一模一样的方法时(返回值类型,方法名和参数列表都相同),会出现覆盖效果,也称为重写或者复写。声明不变,重新实现。 代码如下:

    class liGang3 {
        // 父类中的成员方法。
       public void zhuangRen(int a){
           System.out.println("我叫李刚,人不是我撞的,别抓我");
       }
    }
    class LiXiaoGang3 extends liGang3 {

        // 子类中的成员方法
        public void zhuangRen(int a) {
            System.out.println("有本事你们告去,我爸是李刚");
        }
    }
    public class MemberMethod {
        public static void main(String[] args) {
            // 创建子类对象
            LiXiaoGang3 liXiaoGang = new LiXiaoGang3();
            // 调用子类中的zhuangRen方法
            liXiaoGang.zhuangRen(1);

        }
    }
    结果打印:有本事你们告去,我爸是李刚

#### 3.3、继承中重写方法的意义
子类可以根据需要,定义特定于自己的行为。既沿袭了父类的功能名称,又根据子类的需要重新实现父类方法,从而进行扩展增强。比如李刚会开车,李小刚就牛了,在父类中进行扩展增强还会开车撞人,代码如下:

 class liGang3 {
        // 父类中的成员方法。
       public void kaiChe(){
           System.out.println("我会开车");
       }
    }
    class LiXiaoGang3 extends liGang3 {
        // 子类中的成员方法
        public void kaiChe(){
            super.kaiChe();
            System.out.println("我还会撞人");
            System.out.println("我还能一撞撞俩婆娘");
        }
    }
    public class MemberMethod {
        public static void main(String[] args) {
            // 创建子类对象
            LiXiaoGang3 liXiaoGang = new LiXiaoGang3();
            // 调用子类中的zhuangRen方法
            liXiaoGang.kaiChe();

打印结果:   我会开车
           我还会撞人
           我还能一撞撞俩婆娘
        }
    }

不知道同学们发现了没有,以上代码中在子类中使用了 super.kaiChe();super.父类成员方法,表示调用父类的成员方法。

最后重写必须注意这几点:

1、方法重写时, 方法名与形参列表必须一致。
2、子类方法覆盖父类方法时,必须要保证子类权限 >= 父类权限。
3、方法重写时,子类的返回值类型必须要 <= 父类的返回值类型。
4、方法重写时,子类抛出的异常类型要 <= 父类抛出的异常类型。

粗心的同学看黑板,look 这里【注意:只有访问权限是>=,返回值、异常类型都是<=

下面以修饰权限为例,如下:

Here Insert Picture Description

4、关于继承之后的构造方法

为了让你更好的体会,首先我先编写一个程序

   class liGang4 {
        // 父类的无参构造方法。
        public liGang4(){
            System.out.println("父类构造方法执行了。。。");
        }
    }
    class LiXiaoGang4 extends liGang4 {
        // 子类的无参构造方法。
       public LiXiaoGang4(){
           System.out.println("子类构造方法执行了====");
       }
    }
    public class ConstructionDemo {
        public static void main(String[] args) {
            // 创建子类对象
            LiXiaoGang4 z = new LiXiaoGang4();

        }
    }

用一分钟猜想一下结果是什么,猜好了再看下面结果:

父类构造方法执行了。。。
子类构造方法执行了====

好了,看了结果之后,你可能有疑惑。父类构造器方法怎么执行了?我们先来分析分析,首先在main方法中实例化了子类对象,接着会去执行子类的默认构造器初始化,这个时候在构造方法中默认会在第一句代码中添加super();没错,他就是开挂般的存在,不写也存在的!有的调~读四声“跳”~皮的同学就会说,你说存在就存在啊,无凭无据 ~呀,你这个该死的靓仔~ 如下:
Here Insert Picture Description
构造方法的名字是与类名一致的,所以子类是无法继承父类构造方法的。 构造方法的作用是初始化成员变量的。所以子类的初始化过程中,必须先执行父类的初始化动作。子类的构造方法中默认会在第一句代码中添加super(),表示调用父类的构造方法,父类成员变量初始化后,才可以给子类使用。

当然我已经强调很多遍了 super() 不写也默认存在,而且只能是在第一句代码中,不在第一句代码中行不行,答案是当然不行,这样会编译失败,如下:
Here Insert Picture Description

5、关于继承的多态性支持的例子

直接上代码了喔

class A{
    public String show(C obj) {
        return ("A and C");
    }

    public String show(A obj) {
        return ("A and A");
    }

}
class B extends A{
    public String show(B obj) {
        return ("B and B");
    }
}
class C extends B{
    public String show(A obj) {
        return ("A and B");
    }
}
public class Demo1 {
    public static void main(String[] args) {
        A a=new A();
        B b=new B();
        C c=new C();
        System.out.println("第一题 " + a.show(a));
        System.out.println("第二题 " + a.show(b));
        System.out.println("第三题 " + a.show(c));
    }
}
运行结果:
        第一题 A and A
        第二题 A and A
        第三题 A and C

其实吧,第一题和第三题都好理解,第二题就有点意思了,会发现A类中没有B类型这个参数,这个时候,你就应该知道子类继承就是父类,换句话说就是子类天然就是父类,比如中国人肯定是人,但是人不一定是中国人(可能是火星人也可能是非洲人),所以父类做为参数类型,直接传子类的参数进去是可以的,反过来,子类做为参数类型,传父类的参数进去,就需要强制类型转换。

6、super与this的用法

了解他们的用法之前必须明确一点的是父类空间优先于子类对象产生

在每次创建子类对象时,先初始化父类空间,再创建其子类对象本身。目的在于子类对象中包含了其对应的父类空间,便可以包含其父类的成员,如果父类成员非private修饰,则子类可以随意使用父类成员。代码体现在子类的构 造方法调用时,一定先调用父类的构造方法。理解图解如下:
Here Insert Picture Description
#### 5.1、 super和this的含义:

super :代表父类的存储空间标识(可以理解为父亲的引用)。

 

this :代表当前对象的引用(谁调用就代表谁)。

#### 5.2、 super和this访问成员

this.成员变量 ‐‐ 本类的
super.成员变量 ‐‐ 父类的
this.成员方法名() ‐‐ 本类的
super.成员方法名() ‐‐ 父类的

#### 5.3、super和this访问构造方法

this(...) ‐‐ 本类的构造方法
super(...) ‐‐ 父类的构造方法

#### 5.4、super()和this()能不能同时使用?

Can not be used, thisand supercan not appear at the same time inside a constructor, because thisinevitably call other constructors, other constructor must also have superthe presence of statements, so in the same constructor which has the same statement, lost the significance of the statement, the compiler will not pass.
#### 5.5, to sum up and this super

Each constructor subclass are the default super(), null call reference configuration of the parent class. Manual call the parent class constructor overrides the default super(). super()And this()it must be in the constructor of the first row , so can not appear at the same time .

Here, java inheritance you get to the baa, baa, please get to cry, just pick a point ~ praise chant ~

Recommended reading the next article in this column java

[Java] understand the basis of many state polymorphic transition up from down, "Mommy, I want to eat roast yam" Let's talk about

I welcome you to focus on the public number, to explore technology, yearning technology, the pursuit of technology ...

Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/yichunguo/p/11875343.html