Java基础入门day24

2024-03-30 07:36
文章标签 java 基础 入门 day24

本文主要是介绍Java基础入门day24,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

day24

abstract

抽象:似是而非,像又不是,具备某种对象的特征,但不完整

生活中的抽象:动物,并不真实存在的事物

程序中的抽象:不应该被创建的对象,动物近视一种会吃会睡的对象,再无其他行为,不够具体,不够完整

package com.saas.oo01;
​
public class Animal {
​String breed;
​int age;
​public void eat(){System.out.println("eating...");}
​public void sleep(){System.out.println("sleeping...");System.out.println("zzzzzzzzzZZZZZZZZZZZ");}
}
package com.saas.oo01;
​
public class Test {
​public static void main(String[] args) {Animal animal = new Animal();
​animal.breed = "taidi";animal.age = 5;
​animal.eat();System.out.println("------------------");animal.sleep();}
}

以上程序设计,我们说让程序员自觉地不进行创建对象,但是一旦使用new关键字调用,还是可以执行的,但是这样没有意义,所以这种方式没有办法从根本上杜绝问题存在。

我们在程序中不能对这种不够具体的事物创建其对象,可以使用abstract关键字来实现这种限制

package com.saas.oo02;
​
public abstract class Animal {
​String breed;
​int age;
​public void eat(){System.out.println("eating...");}
​public void sleep(){System.out.println("sleeping...");System.out.println("zzzzzzzzzZZZZZZZZZZZ");}
}
package com.saas.oo02;
​
public class Test {
​public static void main(String[] args) {Animal animal = new Animal();
​animal.breed = "taidi";animal.age = 5;
​animal.eat();System.out.println("------------------");animal.sleep();}
}

将Animal类使用abstract抽象关键字来修饰,一旦一个类用abstract修饰,那么该类就是抽象类,抽象类是无法创建对象,一旦想要直接创建对象,编译器会报错。所以可以从根本上解决创建对象的问题

一个类被abstract修饰,此类是不能new对象

被abstract修饰的类,成为抽象类

抽象类意为不够完整的类、不够具体的类,抽象类对象无法独立存在,即不能创建new对象

抽象类的作用:

  • 可以被子类继承,提供共性的属性和方法

  • 可声明为引用,可以更自然的使用多态

抽象方法

被abstract修饰的方法,被称之为抽象方法

抽象方法特点:只有方法声明,没有方法实现,意为不完整的方法,必须包含在抽象类中

抽象类中可以不必含有抽象方法

抽象类中是含有构造方法,之不能不能直接初始化抽象类对象本身,可以在该构造方法中初始化各个子类类型的对象

小结:

abstract修饰的类:不能new对象,但可以声明引用

abstract修饰的方法:只有方法声明,没有方法实现(需包含在抽象类中)

抽象类中不一定含有抽象方法,但是含有抽象方法的类一定是抽象类

抽象类中是含有构造方法的

子类继承抽象类后,必须重写父类的所有抽象方法,否则可以让子类继续保持抽象

static

package com.saas.oo4;
​
public class MyClass {
​int n;
}
package com.saas.oo4;
​
public class TestStatic {
​public static void main(String[] args) {MyClass mc1 = new MyClass();
​mc1.n = 100;
​System.out.println(mc1.n);
​System.out.println("=================");
​MyClass mc2 = new MyClass();
​mc2.n = 200;
​System.out.println(mc2.n);
​System.out.println("==================");
​System.out.println("mc1.n\t" + mc1.n +"\nmc2.n:\t" + mc2.n) ;}
}

运行结果如下:

100
=================
200
==================
mc1.n   100
mc2.n:  200

实例属性是每个对象各自持有的独立空间(多份),对象单方面修改,不会影响其他对象

有些时候,我们希望属性是整个类共同持有的共享空间(一份),任何对象修改,都会影响其他对象

package com.saas.oo4;
​
public class MyClass02 {
​static int n;
}
package com.saas.oo4;
​
public class TestStatic02 {
​public static void main(String[] args) {MyClass02 mc1 = new MyClass02();
​mc1.n = 100;
​System.out.println(mc1.n);
​System.out.println("=================");
​MyClass02 mc2 = new MyClass02();
​mc2.n = 200;
​System.out.println(mc2.n);
​System.out.println("==================");
​System.out.println("mc1.n\t" + mc1.n +"\nmc2.n:\t" + mc2.n) ;}
}

运行结果如下:

100
=================
200
==================
mc1.n   200
mc2.n:  200

通过两次的运行结果发现:

一旦属性用static修饰,那么该属性就属于静态属性,静态属性是属于整个类的,不专属于某个对象。只要有任何对象对于静态属性做了修改,由于在整个内存中是共享空间,所以后面对象得到的该属性值已经是修改过的值

概念:

static静态可以修饰属性和方法

被static修饰的属性被称之为静态属性(类属性),被static修饰的方法称之为静态方法(类方法)

静态成员是全类所有对象共享的成员

在全类中只有一份,不会因为创建多个对象而产生多份

不必创建对象,可以直接通过类名来访问

静态方法:

被static修饰的方法被称之为静态方法

静态方法与静态属性一样,也可以通过对象来调用,但是不推荐,推荐使用类直接调用

package com.saas.oo4;
​
public class MyClass03 {
​public static void test01(){System.out.println("this is test01");}
​public static void test02(){System.out.println(" this is test02");test01();}
}
package com.saas.oo4;
​
public class TestStatic04 {
​public static void main(String[] args) {
//        MyClass03 mc = new MyClass03();
//
//        mc.test01();          //  test01是MyClass03类中的静态方法,可以通过对象来调用,但是不推荐MyClass03.test01();     //  静态方法推荐使用类来直接调用}
}

在同一个类当中,静态方法可以直接调用其他的静态方法,直接写方法名即可

在其他类中静态方法可以通过当前的类名来访问静态方法

之前学过的静态方法:

Arrays.copyOf();

System.arrayCopy();

Math.random();

静态的特点:

  • 静态方法允许直接访问静态成员

  • 静态方法不能直接访问非静态成员,必须通过对象来访问

  • 静态方法不允许使用this或者super关键字

  • 静态方法可以被继承,不能重写,没有多态

package com.saas.oo4;
​
public class Father {
​public static void test(){System.out.println("this is test in Father class");}
}
package com.saas.oo4;
​
public class Son  extends Father{
​public static void test(){System.out.println("this is test in Son class");}
}
package com.saas.oo4;
​
public class TestStatic05 {
​public static void main(String[] args) {Father.test();Son.test();
​Father fs = new Son();Son ss = new Son();
​fs.test();ss.test();}
}

最终的运行结果如下:

this is test in Father class
this is test in Son class
this is test in Father class
this is test in Son class

动态代码块和静态代码块

package com.saas.oo5;
​
public class MyClass {
​String field01 = "实例属性";static String filed02 = "静态实例属性";
​{System.out.println("this is non-static code block01");}
​{System.out.println("this is non-static code block02");}
​public MyClass(){System.out.println("this is MyClass Constructor.");}
​static {System.out.println("this is static code block02");}
​static {System.out.println("this is static code block01");}
}
package com.saas.oo5;
​
public class Test {
​public static void main(String[] args) {
//        System.out.println(MyClass.filed02);new MyClass();System.out.println("========================");new MyClass();System.out.println("========================");new MyClass();System.out.println("========================");new MyClass();System.out.println("========================");new MyClass();}
}

运行结果如下:

this is static code block02
this is static code block01
this is non-static code block01
this is non-static code block02
this is MyClass Constructor.
========================
this is non-static code block01
this is non-static code block02
this is MyClass Constructor.
========================
this is non-static code block01
this is non-static code block02
this is MyClass Constructor.
========================
this is non-static code block01
this is non-static code block02
this is MyClass Constructor.
========================
this is non-static code block01
this is non-static code block02
this is MyClass Constructor.

以上的运行结果可以得出如下结论

一个类中如果同时定义了静态代码块,非静态代码块和构造方法时,其执行顺序如下:

如果只是访问当前类的静态成员,则按顺序直接执行当前类的静态代码块,以及静态成员

如果要访问当前类的非静态成员,则第一步按顺序执行当前类的静态代码块,非静态代码块,构造方法再到当前类的非静态成员

如果创建多个对象,则静态代码块还是按照顺序只执行一次,而非静态代码块次数是创建几个对象,则执行几次非静态代码块以及走几次构造方法

带有继承关系的父子类之间的静态代码块和非静态代码块的运行结果:

package com.saas.oo5;
​
public class Father {
​String field01 = "Father实例属性";static String filed02 = "Father静态实例属性";
​{System.out.println("this is Father non-static code block01");}
​{System.out.println("this is Father non-static code block02");}
​public Father(){System.out.println("this is Father Constructor.");}
​static {System.out.println("this is Father static code block02");}
​static {System.out.println("this is Father static code block01");}
}
package com.saas.oo5;
​
public class Son extends Father {
​String field01 = "Son实例属性";static String filed02 = "Son静态实例属性";
​{System.out.println("this is Son non-static code block01");}
​{System.out.println("this is Son non-static code block02");}
​public Son(){System.out.println("this is Son Constructor.");}
​static {System.out.println("this is Son static code block02");}
​static {System.out.println("this is Son static code block01");}
}
package com.saas.oo5;
​
public class Test02 {
​public static void main(String[] args) {System.out.println(Son.filed02);}
}

运行结果如下:

this is Father static code block02
this is Father static code block01
this is Son static code block02
this is Son static code block01
Son静态实例属性

如果只是访问子类的静态成员,则执行结果为:

先顺序执行父类的静态代码块

再顺序执行子类的静态代码块

再执行子类的静态成员

package com.saas.oo5;
​
public class Test02 {
​public static void main(String[] args) {
//        System.out.println(Son.filed02);System.out.println(new Son().field01);System.out.println("====================");System.out.println(new Son().field01);System.out.println("====================");System.out.println(new Son().field01);}
}

运行结果如下:

this is Father static code block02
this is Father static code block01
this is Son static code block02
this is Son static code block01
this is Father non-static code block01
this is Father non-static code block02
this is Father Constructor.
this is Son non-static code block01
this is Son non-static code block02
this is Son Constructor.
Son实例属性
====================
this is Father non-static code block01
this is Father non-static code block02
this is Father Constructor.
this is Son non-static code block01
this is Son non-static code block02
this is Son Constructor.
Son实例属性
====================
this is Father non-static code block01
this is Father non-static code block02
this is Father Constructor.
this is Son non-static code block01
this is Son non-static code block02
this is Son Constructor.
Son实例属性

如果要访问子类的非静态成员,得创建子类对象,那么创建子类对象多次,将按照如下结果执行:

  1. 先顺序执行父类的静态代码块

  2. 再顺序执行子类的静态代码块

  3. 再顺序执行父类的非静态代码块

  4. 再执行父类的构造器

  5. 再按顺序执行子类的非静态代码块

  6. 再执行子类的构造器

  7. 再执行子类的成员

注意:创建几个子类对象,那么以上所有的非静态代码块和构造器都将分别执行几套

static小结:

static修饰的成员为静态成员,无需创建对象即可访问

静态方法不能直接访问非静态成员

静态方法中不能使用this和super

静态方法可以继承,不能被重写,没有多态

静态代码块再类加载的过程中默认被执行,而且只执行一次

非静态代码块在创建对象时默认被执行,创建几个对象,非静态代码块执行几次

final

这篇关于Java基础入门day24的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/860867

相关文章

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

java中long的一些常见用法

《java中long的一些常见用法》在Java中,long是一种基本数据类型,用于表示长整型数值,接下来通过本文给大家介绍java中long的一些常见用法,感兴趣的朋友一起看看吧... 在Java中,long是一种基本数据类型,用于表示长整型数值。它的取值范围比int更大,从-922337203685477

java Long 与long之间的转换流程

《javaLong与long之间的转换流程》Long类提供了一些方法,用于在long和其他数据类型(如String)之间进行转换,本文将详细介绍如何在Java中实现Long和long之间的转换,感... 目录概述流程步骤1:将long转换为Long对象步骤2:将Longhttp://www.cppcns.c

SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程

《SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程》LiteFlow是一款专注于逻辑驱动流程编排的轻量级框架,它以组件化方式快速构建和执行业务流程,有效解耦复杂业务逻辑,下面给大... 目录一、基础概念1.1 组件(Component)1.2 规则(Rule)1.3 上下文(Conte

SpringBoot服务获取Pod当前IP的两种方案

《SpringBoot服务获取Pod当前IP的两种方案》在Kubernetes集群中,SpringBoot服务获取Pod当前IP的方案主要有两种,通过环境变量注入或通过Java代码动态获取网络接口IP... 目录方案一:通过 Kubernetes Downward API 注入环境变量原理步骤方案二:通过

Springboot整合Redis主从实践

《Springboot整合Redis主从实践》:本文主要介绍Springboot整合Redis主从的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言原配置现配置测试LettuceConnectionFactory.setShareNativeConnect