/* 看板娘 */ canvas#live2dcanvas { border: 0 !important; left: 0; }

Java 的内部类、成员类部类、局部类、匿名内部类、常用的API、System、Object

目录

1、内部类

2、成员类部类

3、局部类

4、匿名内部类

匿名内部类在开发中的使用

5、常用的API

6、System

7、Object 之 toString

   Object 之 equals

 

一、类的总汇

1 内部类的基本使用

  内部类概念

    在一个类中定义一个类。举例:在一个类A的内部定义一个类B,类B就被称为内部类
  内部类定义格式

     格式&举例:

/*
    格式:   
    class 外部类名{     
        修饰符 class 内部类名    
        }
    }
 */


class Outer {     
    public class Inner {             
    }
}

内部类的访问特点

  内部类可以直接访问外部类的成员,包括私有

  外部类要访问内部类的成员,必须创建对象

示例代码:

package com.itheima_01;
/*
    内部类格式:
        public class 类名{
              修饰符 class 类名{

              }
        }

    内部类访问特点:
        内部类可以直接访问外部类的成员,包括私有
        外部类要访问内部类的成员,必须创建对象
 */
public class Outer {

    private int num = 10;

    public class Inner {

        public void show() {
            System.out.println(num);
        }

    }

    public void method() {
//        show();

        Inner i = new Inner();
        i.show();
    }

}

2 成员内部类

  成员内部类的定义位置

    在类中方法,跟成员变量是一个位置

  外界创建成员内部类格式 

    格式:外部类名.内部类名 对象名 = 外部类对象.内部类对象; 

    举例:Outer.Inner oi = new Outer().new Inner();

  成员内部类的推荐使用方案

    将一个类,设计为内部类的目的,大多数都是不想让外界去访问,所以内部类的定义应该私有化,私有 化之后,再提供一个可以让外界调用的方法,方法内部创建内部类对象并调用。

  示例代码:

package com.itheima_02;

public class Outer {

    private int num = 10;

    /*
    public class Inner {
        public void show() {
            System.out.println(num);
        }
    }
    */

    private class Inner {
        public void show() {
            System.out.println(num);
        }
    }

    public void method() {
        Inner i = new Inner();
        i.show();
    }

}


// -----------------------------------------------------------------------

package com.itheima_02;
/*
    测试类
 */
public class InnerDemo {
    public static void main(String[] args) {
        //创建内部类对象,并调用方法
//        Inner i = new Inner();

//        Outer.Inner oi = new Outer().new Inner();
//        oi.show();

        Outer o = new Outer();
        o.method();

    }
}

3 局部内部类

  局部内部类定义位置

    局部内部类是在方法中定义的类

  局部内部类方式方式

    局部内部类,外界是无法直接使用,需要在方法内部创建对象并使用

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

示例代码

package com.itheima_03;

public class Outer {

    private int num = 10;

    public void method() {
        int num2 = 20;
        class Inner {
            public void show() {
                System.out.println(num);
                System.out.println(num2);
            }
        }

        Inner i = new Inner();
        i.show();

    }

}


// -----------------------------------------------------------------------

package com.itheima_03;
/*
    测试类
 */
public class OuterDemo {
    public static void main(String[] args) {
        Outer o = new Outer();
        o.method();
    }
}

 4 匿名内部类

  匿名内部类的前提
    存在一个类或者接口,这里的类可以是具体类也可以是抽象类
  匿名内部类的格式

    格式:new 类名 ( ) { 重写方法 } new 接口名 ( ) { 重写方法 }

  举例:

 

 

 

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

    匿名内部类可以通过多态的形式接受

 

  匿名内部类直接调用方法

 

 

 匿名内部类在开发中的使用

  匿名内部类在开发中的使用
    当发现某个方法需要,接口或抽象类的子类对象,我们就可以传递一个匿名内部类过去,来简化传统的 代码
  示例代码:

 

package com.itheima_05;

/*
    接口
    跳高接口
 */
public interface Jumpping {

    void jump();
}

// ---------------------------------------------------------------------------

package com.itheima_05;

/*
    接口操作类,里面有一个方法,方法的参数是接口名
 */
public class JumppingOperator {

    public void method(Jumpping j) { //new Cat();   new Dog();
        j.jump();
    }

}

// ----------------------------------------------------------------------

package com.itheima_05;

public class Cat implements Jumpping {

    @Override
    public void jump() {
        System.out.println("猫可以跳高了");
    }
}


// ------------------------------------------------------------------------

package com.itheima_05;

public class Dog implements Jumpping {

    @Override
    public void jump() {
        System.out.println("狗可以跳高了");
    }
}


// -----------------------------------------------------------------------

package com.itheima_05;

/*
    测试类
 */
public class JumppingDemo {
    public static void main(String[] args) {
        //需求:创建接口操作类的对象,调用method方法
        JumppingOperator jo = new JumppingOperator();
        Jumpping j = new Cat();
        jo.method(j);

        Jumpping j2 = new Dog();
        jo.method(j2);
        System.out.println("--------");

        jo.method(new Jumpping() {
            @Override
            public void jump() {
                System.out.println("猫可以跳高了");
            }
        });

        jo.method(new Jumpping() {
            @Override
            public void jump() {
                System.out.println("狗可以跳高了");
            }
        });

    }
}

5 常用API 

   1、Math类概述 Math

    包含执行基本数字运算的方法

   2、Math中方法的调用方式

    Math类中无构造方法,但内部的方法都是静态的,则可以通过   类名.进行调用

  3、Math类的常用方法

 

  

 

 

 示例代码

package com.itheima;

/*
    Math类的常用方法
 */
public class MathDemo {
    public static void main(String[] args) {
        //public static int abs​(int a):返回参数的绝对值
        System.out.println(Math.abs(88));
        System.out.println(Math.abs(-88));
        System.out.println("--------");

        //public static double ceil​(double a):返回大于或等于参数的最小double值,等于一个整数
        System.out.println(Math.ceil(12.34));
        System.out.println(Math.ceil(12.56));
        System.out.println("--------");

        //public static double floor​(double a):返回小于或等于参数的最大double值,等于一个整数
        System.out.println(Math.floor(12.34));
        System.out.println(Math.floor(12.56));
        System.out.println("--------");

        //public static int round​(float a):按照四舍五入返回最接近参数的int
        System.out.println(Math.round(12.34F));
        System.out.println(Math.round(12.56F));
        System.out.println("--------");

        //public static int max​(int a,int b):返回两个int值中的较大值
        System.out.println(Math.max(66,88));
        System.out.println("--------");

        //public static int min​(int a,int b):返回两个int值中的较小值
     System.out.println(Math.min(66,88));
     System.out.println("--------");
     
//public static double pow​(double a,double b):返回a的b次幂的值 System.out.println(Math.pow(2.0,3.0)); System.out.println("--------"); //public static double random​():返回值为double的正值,[0.0,1.0)      // System.out.println(Math.random()); System.out.println((int)(Math.random()*100) + 1); } }

6、System

 System类的常用方法 

代码演示

package com.itheima;

/*
    System类的常用方法
 */
public class SystemDemo {
    public static void main(String[] args) {
        /*
        System.out.println("开始");
        //public static void exit(int status):终止当前运行的 Java 虚拟机,非零表示异常终止
        System.exit(0);
        System.out.println("结束");
        */

        //public static long currentTimeMillis():返回当前时间(以毫秒为单位)
//        System.out.println(System.currentTimeMillis());

//        System.out.println(System.currentTimeMillis() * 1.0 / 1000 / 60 / 60 / 24 / 365 + "年");

        long start = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            System.out.println(i);
        }
        long end = System.currentTimeMillis();
        System.out.println("共耗时:" + (end - start) + "毫秒");
    }
}

7、Object

  Object类概述

    Object 是类层次结构的根,每个类都可以将 Object 作为超类。所有类都直接或者间接的继承自该类, 换句话说,该类所具备的方法,所有类都会有一份

  查看方法源码的方式

    选中方法,按下Ctrl + B

  重写toString方法的方式

    1. Alt + Insert 选择toString

    2. 在类的空白区域,右键 -> Generate -> 选择toString

  toString方法的作用:

    以良好的格式,更方便的展示对象中的属性值

  示例代码

package com.itheima_01;

public class Student extends Object {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}


// ----------------------------------------------------------------------------

package com.itheima_01;

/*
    Object 是类层次结构的根,每个类都可以将 Object 作为超类。所有类都直接或者间接的继承自该类

    看方法源码:选中方法,按下Ctrl+B

    建议所有子类重写此方法:toString()

    怎么重写呢?自动生成即可
 */
public class ObjectDemo {
    public static void main(String[] args) {
        Student s = new Student();
        s.setName("林青霞");
        s.setAge(30);
        System.out.println(s); //com.itheima_01.Student@3f3afe78
        System.out.println(s.toString()); //com.itheima_01.Student@3f3afe78

        /*
        public void println(Object x) { //x = s;
            String s = String.valueOf(x);
            synchronized (this) {
                print(s);
                newLine();
            }
        }

        public static String valueOf(Object obj) { //obj = x;
            return (obj == null) ? "null" : obj.toString();
        }

        public String toString() {
            return getClass().getName() + "@" + Integer.toHexString(hashCode());
        }
         */

    }
}

Object类的equals

  equals方法的作用

    用于对象之间的比较,返回true和false的结果

    举例:s1.equals(s2);    s1和s2是两个对象

  重写equals方法的场景

    不希望比较对象的地址值,想要结合对象属性进行比较的时候。

  重写equals方法的方式

    1. alt + insert  选择equals() and hashCode(),IntelliJ Default,一路next,finish即可

    2. 在类的空白区域,右键 -> Generate -> 选择equals() and hashCode(),后面的同上。

  示例代码:

 

package com.itheima_02;

public class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        //this -- s1
        //o -- s2
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o; //student -- s2

        if (age != student.age) return false;
        return name != null ? name.equals(student.name) : student.name == null;
    }

}

// -----------------------------------------------------------------------

package com.itheima_02;

/*
    测试类

    public boolean equals​(Object obj):指示一些其他对象是否等于此
 */
public class ObjectDemo {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.setName("林青霞");
        s1.setAge(30);

        Student s2 = new Student();
        s2.setName("林青霞");
        s2.setAge(30);

        //需求:比较两个对象的内容是否相同
//        System.out.println(s1 == s2);

        System.out.println(s1.equals(s2));

        /*
            public boolean equals(Object obj) {
                //this -- s1
                //obj -- s2
                return (this == obj);
            }
         */

    }
}

 

 

System.out.println(Math.max(66,88));
posted @ 2021-08-23 14:15  群临天下  阅读(226)  评论(0编辑  收藏  举报
/* 看板娘 */