java继承_方法重载_抽象类_itEighteen

在类的内部,通过this来访问当前对象,它是指向自身的指针。
构造函数调用其他构造函数的方式? this()

这里写图片描述

继承
-------------
    1.抽象共性.
    2.多个类是子类,抽象的类叫父类(超类)
    3.子类可以访问父类的非private成员。
    4.通过extends继承。
    5.类只支持单重继承 + 多层继承.

这里写图片描述

这里写图片描述


super | this
-----------------
    类内置的成员变量,指向的是父类对象的引用。只能在类的内部使用,类似于this.
    如果当前类有和父类相同的成员,若访问父类成员,需要用到super.

super() | this()
-----------------
    调用的是父类的构造函数。必须是第一条语句。

图解为什么super的位置是第一行

这里写图片描述

/**
 * extends继承
 * super()调用父类的构造函数.
 */
class ExtendsDemo2{
    public static void main(String[] args){
        //创建对象
        BMWSportCar mycar = new BMWSportCar();
        mycar.color = "red";
        mycar.velocity = 200;
        mycar.price = 300;
        mycar.run();
    }
}

class Car{
    public String color ;
    public void run(){
        System.out.println("跑了~~");
    }
    public Car(){
        System.out.println("new Car()");
    }
    public Car(String color){
        this.color = color ;
        System.out.println("new Car("+color+")");
    }
}

class SportCar extends Car{
    public int velocity ;
    public SportCar(int velocity){
        this.velocity = velocity ;
        System.out.println("new SportCar("+velocity+")");
    }
}

class BMWSportCar extends SportCar{
    public int price ;

    public BMWSportCar(){
        super(220);
        System.out.println("new BMWSportCar()");
    }
}

方法重载(overload)
-----------------
解决了同一个类中,相同的功能方法名不同的问题,既然是相同的功能,那么方法的名字就应该相同 

方法覆盖(重写)override(overwrite)、继承是前提条件。
----------------------------------
    1.和父类方法签名相同
    2.private方法无法覆盖
    3.super可以访问父类方法。
    4.注意事项
        a.权限要放大。不可以缩小
           public > ... > private 
        b.静态和非静态必须是一致的。

//
Animal(name,color)
//

Monkey(climb() | )

//
GoldenMonkey(int weight , climb())

这里写图片描述

这里写图片描述

final
----------------
    1.修饰方法、类、变量
    2.修饰类
        final class Dog     //不能继承.终态类。
    3.修饰方法
        不能重写。
    4.修饰字段
        不能修改。


Car类{String color , int tires , run();}

Benz
--------------
    1.继承Car
    2.不能被继承
    3.商标属性不能修改
        String brand = "BENZ" ;
    4.点火方法不能重写。
        fire()
    5.private + final ?
        没有意义。
    6.static + final 修饰常量.

类成员
-------------
    1.成员变量
    2.成员函数
    3.构造代码块
    4.构造函数
    5.静态代码块
    6.内部类

内部类
----------------
    1.定义在class内部的类。
    2.编译产生OuterClass$Innerc.class
    3.内部类访问外部类的局部变量,需要是final修饰。

抽象类(抽象类也有构造函数)
-----------------
    1.不能实例化的类。
    2.abstract
        抽象修饰的类.
    3.抽象类和具体类(具体类不能有抽象方法,因为抽象方法没有方法体,没法执行)相对

这里写图片描述

这里写图片描述

猜你喜欢

转载自blog.csdn.net/weixin_37243717/article/details/80786314