• Java学习笔记(十四)


    在完成对C语言的学习后,我最近开始了对C++和Java的学习,目前跟着视频学习了一些语法,也跟着敲了一些代码,有了一定的掌握程度。现在将跟着视频做的笔记进行整理。本篇博客是整理Java知识点的第十四篇博客。

    本篇博客介绍了Java的形参和返回值,内部类,以及Math和System两个工具类。

    本系列博客所有Java代码都使用IntelliJ IDEA编译运行,版本为2022.1。所用JDK版本为JDK11

    目录

    形参和返回值

    类名作为形参和返回值

    抽象类名作为形参和返回值

    接口名作为形参和返回值

    内部类

    成员内部类

    局部内部类

    匿名内部类

    匿名内部类的使用

    Math

    System


    形参和返回值

    类名作为形参和返回值

    当方法的形参是类名时,需要的是该类的对象。方法的返回值是类名时,返回的是该类的对象。

    1. public class animal3 {
    2. public void eat(){
    3. System.out.println("Animal must eat");
    4. }
    5. public cat3 create(){
    6. cat3 cat = new cat3();
    7. return cat;
    8. }
    9. }

    这是父类animal3,其中成员方法create返回一个cat3类对象。

    1. public class cat3 extends animal3{
    2. public void eat(){
    3. System.out.println("Cat eat something");
    4. }
    5. }

    这是子类cat3,重写了父类的eat方法。

    1. public class animaltest4 {
    2. public static void main(String[] args){
    3. animal3 cat1 = new cat3();
    4. cat1.eat();
    5. cat3 cat2 = cat1.create();
    6. cat2.eat();
    7. }
    8. }

    这是测试类animaltest4,首先通过多态创建了一个类对象cat1,并调用eat方法。然后通过cat1的create方法返回一个cat3类对象,用cat2接收,再执行eat方法。 

    程序的输出是:

    Cat eat something
    Cat eat something
     

    抽象类名作为形参和返回值

    方法的形参是抽象类名时,需要的是抽象类的子类对象。方法的返回值是抽象类名时,返回的是该抽象类的子类对象。

    1. public abstract class animal4 {
    2. public abstract void eat();
    3. }

    animal4类是抽象类,有抽象方法eat。

    1. public class cat4 extends animal4{
    2. public void eat(){
    3. System.out.println("Cat can eat something");
    4. }
    5. }

    cat4类继承了animal4类,并重写了eat方法。

    1. public class animalthing {
    2. public void thing(animal4 animal){
    3. animal.eat();
    4. }
    5. public animal4 method(){
    6. animal4 cat = new cat4();
    7. return cat;
    8. }
    9. }

    animalthing类的thing方法接受一个animal4的子类对象,并执行其eat方法,method方法创建一个类对象并返回。

    1. public class animaltest5 {
    2. public static void main(String[] args){
    3. animalthing cat = new animalthing();
    4. animal4 cat1 = new cat4();
    5. cat.thing(cat1);
    6. animal4 cat2 = cat.method();
    7. cat2.eat();
    8. }
    9. }

    这是测试类,创建一个animalthing类对象cat,通过多态创建cat1对象,并作为参数传递给cat以执行其thing方法,随后执行cat的method方法,并将返回值用cat2接收,执行cat2的eat方法。

    程序的输出是:

    Cat can eat something
    Cat can eat something
     

    接口名作为形参和返回值

    当方法的形参是接口名时,需要的是该接口的实现类对象。方法的返回值是接口名时,返回的是该接口的实现类对象。

    1. public interface animal5 {
    2. public abstract void eat();
    3. }

    animal5是一个接口,有抽象方法eat。

    1. public class cat5 implements animal5{
    2. public void eat(){
    3. System.out.println("Cat can eat something");
    4. }
    5. }

    cat5是animal5的实现类,实现了eat方法。

    1. public class interfacething {
    2. public void show(animal5 cat){
    3. cat.eat();
    4. }
    5. public animal5 create(){
    6. animal5 cat = new cat5();
    7. return cat;
    8. }
    9. }

    interfacething类的成员方法show接受一个animal5的实现类对象,并执行其eat方法,create方法返回一个animal5的实现类cat5类对象。

    1. public class animaltest6 {
    2. public static void main(String[] args){
    3. interfacething cat = new interfacething();
    4. animal5 cat1 = new cat5();
    5. cat.show(cat1);
    6. animal5 cat2 = cat.create();
    7. cat2.eat();
    8. }
    9. }

    animaltest6是实现类,创建了一个interfacething类对象cat。然后将一个cat1作为参数传递给cat的show方法,再用cat2接受cat的create方法返回的类对象。

    程序的输出是:

    Cat can eat something
    Cat can eat something
     

    内部类

    内部类是在一个类中定义一个类,被定义在其他类内部的类就是内部类。

    内部类可以直接访问外部类成员,包括私有。但是外部类要访问内部类的成员,就必须创建对象。

    成员内部类

    内部类定义在类的成员位置,则称为成员内部类。内部类定义在类的局部位置,则称为局部内部类

    对于成员内部类,外界创建其类对象的格式是:

    外部类名.内部类名 对象名 = new 外部类对象.new 内部类对象

    不过,还有其他方法可以在其他类调用内部类。

    1. public class outer1{
    2. private int number = 10;
    3. private class inner1{
    4. public void show(){
    5. System.out.println("number = " + number);
    6. }
    7. }
    8. public void use(){
    9. inner1 in = new inner1();
    10. in.show();
    11. }
    12. }

    类outer1有一个private权限的变量number,初始值为10。有一个private权限的内部类inner1,该类有成员方法show,访问输出outer1类的成员方法number。outer1类的public权限成员方法use创建了一个inner1类对象in,并调用其show方法。

    1. public class outerinner1 {
    2. public static void main(String[] args){
    3. outer1 out = new outer1();
    4. out.use();
    5. }
    6. }

    outerinner1类的main方法创建了一个outer1类对象out,并调用其use方法。通过这样调用outer1类的内部类inner1。

    程序的输出是:
    number = 10

    局部内部类

    局部内部类定义在方法内部,因此外界无法直接使用。需要在方法内部创建对象并使用。

    该类可以直接访问外部类的成员,也可以访问方法内的局部变量。

    1. public class outer2 {
    2. private int number1 = 10;
    3. public void method(){
    4. class inner2{
    5. int number2 = 15;
    6. public void show() {
    7. System.out.println("number1 = " + number1);
    8. System.out.println("number2 = " + number2);
    9. }
    10. }
    11. inner2 in = new inner2();
    12. in.show();
    13. }
    14. }

    outer2类有一个private权限的变量number1,初始值为10。有一个public权限的方法method,该方法内有一个类inner2,inner2类有一个变量number2,值为15,成员方法show输出number1和number2的值。method方法创建一个inner2类对象in,并调用其show方法。

    1. public class outerinner2 {
    2. public static void main(String[] args){
    3. outer2 out = new outer2();
    4. out.method();
    5. }
    6. }

    outerinner2类的main方法创建一个outer2类对象out,并调用其method方法。程序的输出是:

    number1 = 10
    number2 = 15

    匿名内部类

    匿名内部类的前提是存在一个类(可以是抽象类也可以是具体类)或者接口。

    格式是:

    new 类名或接口名(){

    重写方法

    }

    匿名内部类的本质是一个继承了该类或者实现了该接口的子类匿名对象。

    1. public abstract class inner3 {
    2. public abstract void show();
    3. }

    inner3类是一个抽象类,show方法为抽象方法。

    1. public class outer3{
    2. public void method() {
    3. new inner3() {
    4. public void show() {
    5. System.out.println("Hello Java");
    6. }
    7. }.show();
    8. inner3 in = new inner3() {
    9. public void show() {
    10. System.out.println("Hello Java");
    11. }
    12. };
    13. in.show();
    14. }
    15. }

    outer3类的method方法创建了两个匿名内部类。这两个匿名内部类都实现了inner3类。一个匿名内部类在本体后直接调用show方法。另一个匿名内部类则用inner3类对象in接受。再调用in类的show方法。

    1. public class outerinner3 {
    2. public static void main(String[] args){
    3. outer3 out = new outer3();
    4. out.method();
    5. }
    6. }

    outerinner3类的main方法创建了outer3类对象out,并使用其method方法。

    程序的输出是:

    Hello Java
    Hello Java

    匿名内部类的使用

    1. public abstract class animal {
    2. public abstract void eat();
    3. }

    animal类是抽象类,内有抽象方法eat。

    1. public class cat extends animal{
    2. public void eat(){
    3. System.out.println("Cat can eat something");
    4. }
    5. }
    1. public class dog extends animal{
    2. public void eat(){
    3. System.out.println("Dog can eat something");
    4. }
    5. }

    cat和dog类都是animal类的子类,重写了eat方法。

    1. public class catdog {
    2. public void dothing(animal temp){
    3. temp.eat();
    4. }
    5. }

    catdog类接受一个animal的子类对象,并执行其eat方法。

    1. public class test {
    2. public static void main(String[] args){
    3. catdog temp = new catdog();
    4. cat acat = new cat();
    5. temp.dothing(acat);
    6. dog adog = new dog();
    7. temp.dothing(adog);
    8. temp.dothing(new animal(){
    9. public void eat(){
    10. System.out.println("Cat can eat something");
    11. }
    12. });
    13. temp.dothing(new animal(){
    14. public void eat(){
    15. System.out.println("Dog can eat something");
    16. }
    17. });
    18. }
    19. }

    test类的main方法首先利用多态,然后利用匿名内部类实现相同功能。

    程序的输出是:

    Cat can eat something
    Dog can eat something
    Cat can eat something
    Dog can eat something

    Math

    Math类包含执行基本数字运算的方法。Math类在java.lang下,不需要导包。下面是Math类的一些常见的方法:

    public static int abs(int a)返回参数绝对值。

    public static double ceil(double a)返回大于或等于参数的最小double值,等于一个整数。

    public static double floor(double a)返回小于或等于参数的最大double值,等于一个整数。

    public static int round(float a)返回四舍五入后的整数结果。

    public static int max(int a,int b)返回两个int值中的最大值。

    public static int min(int a,int b)返回两个int值中的最小值。

    public static double pow(double a,double b)返回a的b次幂。

    public static double random()返回值为double类型的正值,范围是0-1(包括0,不包括1)。

    1. public class math {
    2. public static void main(String[] args){
    3. System.out.println(Math.abs(-7.8));
    4. System.out.println(Math.abs(14.14));
    5. System.out.println((int)(Math.ceil(6.8)));
    6. System.out.println((int)(Math.ceil(-8.46)));
    7. System.out.println((int)(Math.floor(7.55)));
    8. System.out.println((int)(Math.floor(-14.49)));
    9. System.out.println(Math.round(5.81));
    10. System.out.println(Math.round(6.18));
    11. System.out.println(Math.round(20.50));
    12. System.out.println(Math.max(15,18));
    13. System.out.println(Math.min(16,12));
    14. System.out.println(Math.pow(2,10));
    15. System.out.println(Math.pow(3,10));
    16. System.out.println(Math.pow(5,-5));
    17. System.out.println(Math.random());
    18. System.out.println(Math.random());
    19. System.out.println(Math.random());
    20. System.out.println(Math.random());
    21. System.out.println(Math.random());
    22. }
    23. }

    这段程序利用Math类的一些静态方法进行一些运算。程序的输出是(最后五行结果是随机的):

    7.8
    14.14
    7
    -8
    7
    -15
    6
    6
    21
    18
    12
    1024.0
    59049.0
    3.2E-4
    0.405733030832076
    0.2988103113194771
    0.674404500012781
    0.9894402988477616
    0.1285441750319556

    System

    System包含几个有用的类字段和方法。此类在java.lang包下,且不能被实例化。

    public static void exit(int status)终止当前运行的程序,非零表示异常终止。

    public static long currentTimeMillis()返回当前时间,单位是毫秒。时间以1970年1月1日0时0分0秒开始,可以经过换算得到其他单位。

    1. public class system {
    2. public static void main(String[] args){
    3. System.out.println(System.currentTimeMillis()*1.0/1000/60/60/24/365);
    4. long start = System.currentTimeMillis();
    5. int i;
    6. for(i = 0; i < 10000; i += 1){
    7. System.out.println(i + 1);
    8. }
    9. long end = System.currentTimeMillis();
    10. System.out.println(end - start);
    11. }
    12. }

    main方法内第一行将单位换算为年。后面输出1-10000,并且记录开始和结束时间,通过差值得出循环一万次的时间。

    本次程序第一行输出52.650840751236686

    最后一行输出32。(输出的循环省略)

  • 相关阅读:
    java类比C++的STL库
    C++面试知识点
    Java 代码优化29个小技巧
    VS+CUDA环境配置
    2023成都.NET线下技术沙龙圆满结束
    Virtuoso: 最全安装(IC618,IC617等), 问题解决
    【AI+大模型】Meta发布最强开源大模型Llama 3
    Java并发 - (并发基础)
    1.4_12 Axure RP 9 for mac 高保真原型图 - 案例11 【动态面板-滚动条1】
    (黑马C++)L03 对象的构造和析构
  • 原文地址:https://blog.csdn.net/m0_71007572/article/details/126323605