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

相关文章

Java设计模式---迭代器模式(Iterator)解读

《Java设计模式---迭代器模式(Iterator)解读》:本文主要介绍Java设计模式---迭代器模式(Iterator),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录1、迭代器(Iterator)1.1、结构1.2、常用方法1.3、本质1、解耦集合与遍历逻辑2、统一

Java内存分配与JVM参数详解(推荐)

《Java内存分配与JVM参数详解(推荐)》本文详解JVM内存结构与参数调整,涵盖堆分代、元空间、GC选择及优化策略,帮助开发者提升性能、避免内存泄漏,本文给大家介绍Java内存分配与JVM参数详解,... 目录引言JVM内存结构JVM参数概述堆内存分配年轻代与老年代调整堆内存大小调整年轻代与老年代比例元空

深度解析Java DTO(最新推荐)

《深度解析JavaDTO(最新推荐)》DTO(DataTransferObject)是一种用于在不同层(如Controller层、Service层)之间传输数据的对象设计模式,其核心目的是封装数据,... 目录一、什么是DTO?DTO的核心特点:二、为什么需要DTO?(对比Entity)三、实际应用场景解析

Java 线程安全与 volatile与单例模式问题及解决方案

《Java线程安全与volatile与单例模式问题及解决方案》文章主要讲解线程安全问题的五个成因(调度随机、变量修改、非原子操作、内存可见性、指令重排序)及解决方案,强调使用volatile关键字... 目录什么是线程安全线程安全问题的产生与解决方案线程的调度是随机的多个线程对同一个变量进行修改线程的修改操

从原理到实战深入理解Java 断言assert

《从原理到实战深入理解Java断言assert》本文深入解析Java断言机制,涵盖语法、工作原理、启用方式及与异常的区别,推荐用于开发阶段的条件检查与状态验证,并强调生产环境应使用参数验证工具类替代... 目录深入理解 Java 断言(assert):从原理到实战引言:为什么需要断言?一、断言基础1.1 语

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

Java中的雪花算法Snowflake解析与实践技巧

《Java中的雪花算法Snowflake解析与实践技巧》本文解析了雪花算法的原理、Java实现及生产实践,涵盖ID结构、位运算技巧、时钟回拨处理、WorkerId分配等关键点,并探讨了百度UidGen... 目录一、雪花算法核心原理1.1 算法起源1.2 ID结构详解1.3 核心特性二、Java实现解析2.

SpringBoot整合liteflow的详细过程

《SpringBoot整合liteflow的详细过程》:本文主要介绍SpringBoot整合liteflow的详细过程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋...  liteflow 是什么? 能做什么?总之一句话:能帮你规范写代码逻辑 ,编排并解耦业务逻辑,代码

JavaSE正则表达式用法总结大全

《JavaSE正则表达式用法总结大全》正则表达式就是由一些特定的字符组成,代表的是一个规则,:本文主要介绍JavaSE正则表达式用法的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录常用的正则表达式匹配符正则表China编程达式常用的类Pattern类Matcher类PatternSynta

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat