• Java Class09


    Java Class09

    访问修饰符

    在这里插入图片描述
    访问修饰符对类中的成员做了限制,分出了分类等级,让哪些成员可以在哪些级别下可以访问

    private:私有
    default:默认
    protected:受保护
    public:公共

    static

    成员变量分为两种,dynamic(动态的)和static(静态的),在代码中默认的就是动态类型,而使用static需要在变量和方法前添加用来说明

    dynamic是调用才生成,但static在类生成的时候就跟着一起被调用,直到全部执行完被当做垃圾清空时才停止使用(与类同生共死)

    public class Test {
        public static int number=100;//定义静态属性
    
        static{//定义静态初始化块
            System.out.println("number的值为:");
        }
    
        public static int result(){//定义静态方法
            System.out.println(number+100);
            return number;
        }
    
        public static void main(String[] args) {
            Test test=new Test();//实例化对象
            //自动调用静态初始化块
            System.out.println(test.number);//直接输出静态属性的值
            result();//不使用对象直接调用方法
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    static可以修饰属性,方法,初始化块

    public class C extends C0{
        public C(){
            super(100);
            System.out.println(1);
        }
        {
            System.out.println(2);
        }
        static{
           System.out.println(3);
        }
        public C(int age){
            this();
            System.out.println(8);
    
        }
        public static void main(String[] args) {
            new C(100);
        }
    }
    class C0{
        public C0(){
            System.out.println(6);
        }
        public C0(int age){
            this();
            System.out.println(7);
        }
        static{
            System.out.println(5);
        }
        {
            System.out.println(4);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    在这里插入图片描述
    调用先后顺序及底层逻辑
    在这里插入图片描述

    调用顺序:静态代码块>调用的静态方法>继承>代码块>调用方法顺序

    Final关键字

    final意为不可改变的,最终的。final关键字可以修饰类,属性,方法
    被final修饰的成员不可被覆盖,例如final类不可做父类(相当于太监类)

    在这里插入图片描述

    final可以用来修饰全局变量(全局不可更改)

    public static final int i=100;

    全局的 直接可以使用的 不可修改的 类型变量的值

    Abstract与Interface

    两者有很多共同点与不同点

    Abstract

    抽象类,在内部有一些抽象方法(有抽象方法的类就是抽象类)

    public abstract class Father {
        public void gotoSchool(){//定义成员方法
            System.out.println("去上学");
        }
        
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    public class Son extends Father{
        @Override
        public void gotoCollege() {
            System.out.println("去上大学");
        }
    
        public static void main(String[] args) {
            Son son=new Son();
            son.gotoSchool();
            son.gotoCollege();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

    父类中含有可实现方法gotoSchool,也有抽象方法gotoCollege,子类继承父类,直接就可以使用gotoSchool方法,但对于gotoCollege方法只有两个选择:实现和不实现。

    如果实现就如代码写的那样,在调用时正常使用;
    如果不实现则则必须要转成抽象类,交给其能实现的子类

    在这里插入图片描述
    想在父类中直接实例化是不可以的,因为抽象类不可实例化。如果非要使用则使用下节内容的内部类实现的方式去解决

    Interface

    接口是比抽象类更抽象的,它在内部只允许有抽象的方法,接口是为了解决java中不能实现多继承而实现的

    public interface A {
        public void method();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    public interface A1 {
        public void method1();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    public class B implements A,A1 {
        @Override
        public void method() {
            System.out.println("生成解决方案");
        }
    
    
        @Override
        public void method1() {
            System.out.println("输出解决方案");
        }
    
    
        public static void main(String[] args) {
            B b = new B();
            b.method();
            b.method1();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在这里插入图片描述
    abstract和interface的区别

    1.方法实现:

    abstract中抽象方法和普通方法都可以有,但interface只允许抽象方法

    2.属性使用:

    abstract中的访问控制符无限制,但interface只能是public

    3.静态方法:

    abstract中可以有静态方法,但interface不能有

    4.静态代码块:

    abstract中可以有静态代码块,但interface不能有

    5.多继承:

    abstract是但继承,但interface是多继承

    多态

    多态是在继承的基础上产生的,子类继承父类时重写父类的方法,这种在同一方法中不同子类表现出的不同形式就是多态。

    多态分为编译时多态运行时多态

    编译时多态:方法的重载

    public class A {
        private int age;
        private String name;
        public void method(int age){
            System.out.println(age);
        }
        
        public void method(int age,String name){
            System.out.println(age+name);
        }
        
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    运行时多态:方法的重写

    public class A {
        private int age;
        private String name;
        public void method(int age){
            System.out.println(age);
        }
    
        public void method2(int age,String name){
            System.out.println(age+name);
        }
    
    }
    
    
    class B extends A{
        @Override
        public void method(int age){
            System.out.println(age+10);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    instanceof运算符

    变量 instanceof

    意为判断这个变量是否属于这个类中,是的话返回true,否则返回false

    public class Son extends Father{
        public static void main(String[] args) {
            GrandPa g=new Father();
            Father f= new Son();
            GrandPa g1=new Uncle();
            System.out.println(g instanceof Father);//true
            System.out.println(f instanceof Son);//true
            System.out.println(g1 instanceof Uncle);//true
        }
    
    
    }
    
    class Uncle extends GrandPa{
    
    }
    
    class Father extends GrandPa{
    
    }
    
    class GrandPa{
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 相关阅读:
    信息论笔记:信息量+熵+相对熵+交叉熵+损失函数
    倍福tnzip,tszip,tpzip文件的打开方式
    码蹄集需要频繁登录?如何做到“一劳永逸”——码蹄集只登录一次久久不掉线的教程
    kotlin
    RV1126笔记四十一:RV1126移植LIVE555
    网络割接用VRRP替换HSRP
    滚动条样式修改
    从零开始的 dbt 入门教程 (dbt core 命令进阶篇)
    嵌入式分享合集19
    jupyter显示内核启动失败或者网络连接失败——解决办法
  • 原文地址:https://blog.csdn.net/qq_45325217/article/details/127734194