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实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Spring AI集成DeepSeek的详细步骤

《SpringAI集成DeepSeek的详细步骤》DeepSeek作为一款卓越的国产AI模型,越来越多的公司考虑在自己的应用中集成,对于Java应用来说,我们可以借助SpringAI集成DeepSe... 目录DeepSeek 介绍Spring AI 是什么?1、环境准备2、构建项目2.1、pom依赖2.2