IT评测·应用市场-qidao123.com技术社区

标题: Java中类的成员先容 [打印本页]

作者: 饭宝    时间: 2024-9-3 03:38
标题: Java中类的成员先容
我的后端学习大纲
    我的Java学习大纲
  
4.类的成员:

3.1.类的成员 -> 属性先容(成员变量):

a.语法格式:


b.变量的分类:


比较项 1成员变量局部变量声明的位置直接声明在类中方法形参或内部、代码块内、构造器内等修饰符private、public、static、final等不能用权限修饰符修饰,可以用final修饰初始化值有默认初始化值没有默认初始化值,必须显式赋值,方可利用内存加载位置堆空间 或 静态域内栈空间
3.2.类的成员 ->行 为(方法)先容:

a.方法特点:


b.方法的声明格式:


c.代码练习:

  1. package com.jianqun.package01;
  2. /**
  3. * @author jianqun
  4. * @email:1033586391@qq.com
  5. * @creat 2022-01-31-23:15
  6. */
  7.     public class Object02 {
  8.         public static void main(String[] args) {
  9.             int [][] map = {{0,0,1},{0,0,2},{0,0,3}};
  10.             MyTools tools = new MyTools();
  11.             tools.printlnArr(map);
  12.         }
  13.     }
  14.     class MyTools{
  15.         public void printlnArr(int[][] map){
  16.             //遍历数组
  17.             for (int i = 0; i < map.length;i++){
  18.                 for (int j = 0; j < map[i].length; j++){
  19.                     System.out.println(map[i][j]);
  20.                 }
  21.             }
  22.         }
  23.     }
复制代码

d.方法执行的内存加载机制

d1.图像剖析方法的加载机制


d2.成员方法利用中的留意事项:


  1. package com.jianqun.package01;
  2. /**
  3. * @author jianqun
  4. * @email:1033586391@qq.com
  5. * @creat 2022-01-31-23:15
  6. */
  7.     public class Object02 {
  8.         public static void main(String[] args) {
  9.             AA aa = new AA();
  10.             int[] sumAndSub = aa.getSumAndSub(1, 1);
  11.             System.out.println("****两数相加********" + sumAndSub[0] + "******两数相减********" + sumAndSub[1]);
  12.         }
  13.     }
  14.     class AA{
  15.         public int[] getSumAndSub(int n,int m){
  16.             int[] ints = new int[2];
  17.             ints[0] = n + m;
  18.             ints[1] = n - m;
  19.             return ints;
  20.         }
  21.     }
复制代码


e.成员方法的调用说明

e1.代码实现成员方法之间的调用


  1. package com.jianqun.package01;
  2. /**
  3. * @author jianqun
  4. * @email:1033586391@qq.com
  5. * @creat 2022-02-01-0:48
  6. */
  7. public class Object03 {
  8.     public static void main(String[] args) {
  9.         Ao ao = new Ao();
  10.         ao.m1();
  11.     }
  12. }
  13. class Ao{
  14.     public void print(int n){
  15.         System.out.println("print方法被调用");
  16.     }
  17.     public void sayOk(){
  18.         print(10);
  19.         System.out.println("继续执行sayOk方法");
  20.     }
  21.     public void m1(){
  22.         //创建B类方法
  23.         Bo b = new Bo();
  24.         b.hi();
  25.     }
  26. }
  27. class Bo{
  28.     public void hi(){
  29.         System.out.println("B类的hi方法被调用");
  30.     }
  31. }
复制代码

f.方法传参机制:

f1.变量赋值:




f2.方法的形参传递机制:值传递

   Java中方法的参数传递只有一种方式:值传递,即讲现实参数值的副本传入方法内,而参数本身不受影响
  f2-1.根本数据范例的传参


f2-2.引用数据范例的传参机制

   
  
  1. // 类名:Object04.java
  2. package com.jianqun.package01;
  3. /**
  4. * @author jianqun
  5. * @email:1033586391@qq.com
  6. * @creat 2022-02-01-10:15
  7. */
  8. public class Object04 {
  9.     public static void main(String[] args){
  10.        Bb b = new Bb();
  11.        int[] arr = {1,2,3};
  12.         //调用方法,传递数组参数
  13.        b.test1(arr);
  14.         System.out.println("主方法中循环输出");
  15.         for (int a = 0; a < arr.length; a++) {
  16.             System.out.println(arr[a]);
  17.         }
  18.     }
  19. }
  20. class Bb{
  21.     public void test1(int[] arr){
  22.         arr[0] = 100;
  23.         System.out.println("类方法中循环输出");
  24.         //遍历数组
  25.         for (int a = 0; a < arr.length; a++) {
  26.             System.out.println(arr[a]);
  27.         }
  28.     }
  29. }
复制代码


  1. package com.jianqun.package01;
  2. /**
  3. * @author jianqun
  4. * @email:1033586391@qq.com
  5. * @creat 2022-02-01-10:15
  6. */
  7. public class Object05{
  8.     public static void main(String[] args){
  9.         Person p = new Person();
  10.         p.name = "李四";
  11.         p.age = 105;
  12.         
  13.         B b = new B();
  14.         b.test2(p);
  15.     }
  16. }
  17. class Person{
  18.     String name;
  19.     int age;
  20. }
  21. class B{
  22.     public void test2(Person p){
  23.         Person person = new Person();
  24.         person.age = 10;
  25.         person.name = "张三";
  26.     }
  27. }
复制代码


  1. package com.jianqun.day08;
  2. public class TransferTest3 {
  3.     public static void main(String[] args) {
  4.         TransferTest3 transferTest3 = new TransferTest3();
  5.         transferTest3.first();
  6.     }
  7.     public void first(){
  8.         int i = 5;
  9.         Value value = new Value();
  10.         value.i = 25;
  11.         second(value,i);
  12.         System.out.println(value.i);//20
  13.     }
  14.     public void second(Value v,int i){
  15.         i = 0;
  16.         v.i = 20;
  17.         Value value = new Value();
  18.         v = value;
  19.         System.out.println(v.i + "::" + i);//15::0
  20.     }
  21. }
  22. class Value{
  23.     int i = 15;
  24. }
复制代码


g.方法的overload(重载):

g1.根本概念


g2.重载的好处


g3.实现重载



h.可变个数形参的方法:


h1.根本语法:

  1. 访问修饰符 返回值类型 方法名(数据类型... 形参名)
  2.         {  
  3.                 //代码
  4.         }
复制代码
h2.详细利用:


  1. package com.jianqun.package01;
  2. /**
  3. * @author jianqun
  4. * @email:1033586391@qq.com
  5. * @creat 2022-02-01-20:53
  6. */
  7. public class Object08 {
  8.     public static void main(String[] args) {
  9.         int[] arr = {1,2,12};
  10.         HspMethod hspMethod = new HspMethod();
  11.         hspMethod.sum(arr);
  12.     }
  13. }
  14. class HspMethod{
  15.     //    public int sum(int n1,int n2){
  16.     //        return n1 + n2;
  17.     //    }
  18.     //    public int sum(int n1,int n2,int n3){
  19.     //        return n1+ n2;
  20.     //    }
  21.     /**
  22.      *
  23.      * 优化:使用可变参数进行优化
  24.      */
  25.     public int sum(int... nums){
  26.         System.out.println("接受的参数个数量可变" + nums.length);
  27.         int res = 0;
  28.         for (int i = 0; i < nums.length; i++) {
  29.             res += nums[i];
  30.         }
  31.         System.out.println(res);
  32.         return res;
  33.     }
  34. }
复制代码

i.递归方法:


i1.递归调用的内存分析


i2. 递归之阶乘举例分析:


i3. 递归的紧张原则


i4.实现斐波那契


  1. package com.jianqun.package01;
  2. /**
  3. * @author jianqun
  4. * @email:1033586391@qq.com
  5. * @creat 2022-02-01-16:01
  6. */
  7. public class Object06 {
  8.     public static void main(String[] args) {
  9.         To t = new To();
  10.         System.out.println("当n=7的时候,斐波那契数是:" + t.fibonacci(7));
  11.     }
  12. }
  13. class To {
  14.     /**
  15.      *  递归实现斐波那契数:1 1 2 3 5 8 13........
  16.      **/
  17.     public int fibonacci(int n){
  18.         if (n >=1){
  19.             if (n==1 || n==2){
  20.                 return 1;
  21.             }else{
  22.                 return fibonacci(n-1) + fibonacci(n-2);
  23.             }
  24.         }else{
  25.             System.out.println("输入的数据错误");
  26.             return -1;
  27.         }
  28.     }
  29. }
复制代码

4.类的成员 -> 构造器(构造方法)Constructor

4.1.根本语法

1.[修饰符] 方法名(形参列表){方法体}
4.2.构造器作用


4.3.构造器特点


4.4.构造器快速入门

  1. package com.jianqun.package02;
  2. /**
  3. * @author jianqun
  4. * @email:1033586391@qq.com
  5. * @creat 2022-02-02-10:01
  6. */
  7. public class Constructor01 {
  8.     public static void main(String[] args) {
  9.         //当我们new一个对象的时候,直接是通过构造器指定名字和年龄
  10.         //创建对象的时候就直接指定对象的名字和年龄
  11.         Person3 smith = new Person3("smith", 80);
  12.         System.out.println("p1的信息如下" + smith.name + smith.age);
  13.     }
  14. }
  15. class Person3{
  16.     String name;
  17.     int age;
  18.     //构造器
  19.     //构造器名字和类名一致
  20.     //构造器无返回值,不可以写void
  21.     //(String name, int age)是构造器的形参列表,和成员方法一个样
  22.     public Person3(String name, int age) {
  23.         this.name = name;
  24.         this.age = age;
  25.     }
  26. }
复制代码

4.5.对象创建的流程分析



5.类成员-代码块

5.1.代码块有什么作用:



5.2.语法

  1. 修饰符{
  2.         //代码
  3. }
复制代码


5.3.代码块利用的留意事项:

1.类加载的时机:

2.静态代码类与非静态代码类对比:


5.4.演示

a.案例演示1:代码块简化构造器中重复语句

   代码块相当于另外一种形式的构造器(对构造器哟补充作用),可以做初始化操作
  1.场景:当多个构造器中有雷同的重复语句时,可以初始化到代码块中,提高代码的重用性

2.代码块办理:代码块的调用要优先于构造器

b.案例演示2:代码块利用留意事项;

  1. package com.jianqun.static_;
  2. /**
  3. * @author: jianqun
  4. * @email: 1033586391@qq.com
  5. * @creat: 2022-04-04-13:28
  6. * @Description:
  7. */
  8. public class CodeBlockDetail01 {
  9.     public static void main(String[] args) {
  10.         //1.创建对象实例,虽然创建了两个对象,但是静态代码块只被调用一次
  11.         CC cc1 = new CC();
  12.         CC cc2 = new CC();
  13.         //2.创建子类对象实例,父类也会被加载
  14.          AA aa2 = new AA();
  15.         //3.调用静类的静态成员时(静态属性和静态方法)
  16.         System.out.println(Cat.n1);
  17.     }
  18. }
  19. class CC{
  20.     static {
  21.         System.out.println("CC 静态代码块被调用");
  22.         System.out.println("==================");
  23.     }
  24.     {
  25.          System.out.println("普通代码块");
  26.     }
  27. }
  28. class Cat{
  29.     public static int n1 = 9999;
  30.    
  31.     static {
  32.         System.out.println("==================");
  33.         System.out.println("Cat 静态代码块被调用");
  34.     }
  35.    
  36.     {
  37.         System.out.println("Cat普通代码块");
  38.     }
  39. }
  40. class BB{
  41.     static {
  42.         System.out.println("BB 静态代码块被调用");
  43.     }
  44. }
  45. class AA extends BB{
  46.     static {
  47.         System.out.println("AA 静态代码块被调用");
  48.     }
  49. }
复制代码
c.案例演示3:代码块、静态代码块、构造器调用顺序:

1.创建对象的时候,,在一个类中调用的顺序是:

  1. package com.jianqun.static_;
  2. /**
  3. * @author: jianqun
  4. * @email: 1033586391@qq.com
  5. * @creat: 2022-04-04-13:28
  6. * @Description:
  7. */
  8. public class CodeBlockDetail02 {
  9.     public static void main(String[] args) {
  10.         FF ff = new FF();
  11.     }
  12. }
  13. class FF{
  14.     //静态属性初始化
  15.     private static int n1 = getN1();
  16.     //静态代码块
  17.     static {
  18.         System.out.println("AAA 静态代码块");
  19.     }
  20.     public static int getN1(){
  21.         System.out.println("getN1被执行了");
  22.         return 100;
  23.     }
  24. }
复制代码





免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4