Day01-Java进阶-static修饰成员变量特点/内存图解/工具类&继承&方法重写&权限修饰符&this/super/final关键字

本文详细探讨了Java中static关键字的特性,如何使用static修饰成员变量,以及类加载机制。还介绍了继承、方法重写、权限修饰符、this和super的关键概念,通过实例展示了这些概念在实际编程中的应用。
摘要由CSDN通过智能技术生成

1. static 修饰成员变量

1.1 static修饰成员变量特点

  • 所有static修饰的成员变量和方法都是随着类的加载而加载, 而非static修饰的成员变量和方法只有在调用的时候踩在堆和栈中进行相应的加载, 故不可以通过static方法访问非静态成员.
package com.itheima.mystatic;

public class Student {
    String name;
    int age;
    static String school;
}
package com.itheima.mystatic;

public class StaticDemo1 {
    /*
        static 关键字 : 修饰符, 可以修饰成员变量, 成员方法

        特点 :
                1. 被类的所有对象所共享
                2. 多了一种调用方式, 可以通过类名进行调用(推荐使用类名调用)
                3. 随着类的加载而加载, 优先于对象存在
     */

    public static void main(String[] args) {


        Student.school = "传智专修学院";
        Student stu1 = new Student();
        stu1.name = "张三";
        stu1.age = 23;

        System.out.println(stu1.name+"---"+stu1.school);


        System.out.println("--------------------------------");

        Student stu2 = new Student();
        stu2.name = "李四";
        stu2.age = 24;

        System.out.println(stu2.name+"---"+stu2.school);
    }
}

1.2 static修饰成员变量内存图解

1.3 工具类案例

package com.itheima.tools;

public class ArrayTools {

    private ArrayTools(){}

    public static int getMax(int[] arr) {
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        return max;
    }

    public static int getMin(int[] arr) {
        int min = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] < min) {
                min = arr[i];
            }
        }
        return min;
    }

    public static void printArray(int[] arr) {
        System.out.print("[");
        for (int i = 0; i < arr.length - 1; i++) {
            System.out.print(arr[i] + ", ");
        }
        System.out.println(arr[arr.length - 1] + "]");
    }


}
package com.itheima.tools;

public class Test {
    /*
        1. 成员方法什么时候加入 static
                - 常用于制作工具类

        2. 工具类: 不是描述事物的, 而是帮我们完成一些事情 (打工)

        3. 如果发现一个类中, 所有的方法, 全都是 static 所修饰
                    - 私有该类的构造方法
                    - 目的: 为了不让其他类, 再创建对象
     */
    public static void main(String[] args) {
        int[] arr = {11,22,33};

        System.out.println(ArrayTools.getMax(arr));
        System.out.println(ArrayTools.getMin(arr));
        ArrayTools.printArray(arr);

        System.exit(0);
    }
}

2. 继承

2.1 继承介绍

共性的内容进行抽取如下:

package com.itheima.mextends;

public class ExtendsDemo1 {
    public static void main(String[] args) {
        Coder c = new Coder();
        c.setName("张三");
        c.setAge(23);
        c.setSalary(12000);

        System.out.println(c.getName() + "---" + c.getAge() + "---" + c.getSalary());

    }
}

class Employee {
    private String name;
    private int age;
    private double salary;

    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;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
}

/**
 * 创建类的细节:
 * 一个.java文件中可以编写多个class
 * 1. 保证类与类之间是平级关系
 * 2. 只能有一个被public修饰
 */
class Coder extends Employee {

}

class Manager extends Employee {

}

2.2 继承中成员访问特点 

package com.itheima.mextends;

public class ExtendsDemo2 {
    /*
        子父类中, 出现了方法声明一模一样的方法 (方法名, 参数, 返回值)
                    在创建子类对象, 调用方法的时候, 会优先使用子类的方法逻辑
                    这虽然是就近原则的现象, 但其实是子类的方法, 对父类的方法, 进行了重写操作
     */
    public static void main(String[] args) {
        Zi z = new Zi();
        z.method();
        z.show();
    }
}

class Fu{
    int num = 10;

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

class Zi extends Fu{
    int num = 20;

    public void method(){
        System.out.println(num);        // 20
        System.out.println(super.num);  // 10
    }
}

2.3 方法重写

package com.itheima.mextends;

public class ExtendsDemo3 {
    /*

         方法重载(Overload) : 在同一个类中, 方法名相同, 参数不同, 与返回值无关
                                参数不同: 类型不同, 个数不同, 顺序不同

         方法重写(Override) : 在子父类当中, 出现了方法声明一模一样的方法 (方法名, 参数, 返回值)

                             目标1: 能够独立识别出, 方法是不是重写的方法
                                        - 注释: @Override

                             目标2: 方法重写的使用场景.
                                        - 当子类需要父类的方法, 但是觉得父类的方法逻辑不好 (修改 | 增强) 就可以对父类的方法进行重写


     */
    public static void main(String[] args) {
        Son s = new Son();
        s.method();

    }
}

class Father {

    public void method(){
        System.out.println("Father...method..");
    }

}

class Son extends Father {

    @Override
    public void method() {
        System.out.println("Son...method");
    }
}

3. 权限修饰符

3.1 权限修饰符介绍

package com.itheima.a;

public class Fu {
    protected void show(){
        System.out.println("protected...fu...show");
    }
}
package com.itheima.b;

import com.itheima.a.Fu;

/*
    不同包下的子类
 */
public class Zi extends Fu {
    public void method(){
        super.show();
    }

}
package com.itheima.b;

/*
    Fu类 和 Test类
    - 不同包下的无关类
 */
public class Test {
    public static void main(String[] args) {
        Zi z = new Zi();
        z.method();
    }
}

3.2 继承的特点

3.3 继承中构造方法的访问特点

package com.itheima.mextends.constructer;

public class Test {
    /*
        除了Object类, 在所有构造方法的第一行代码, 都默认隐藏了一句话 super();
        通过这句话, 访问父类的空参构造方法

        细节: Java当中所有的类, 都直接或间接的继承到了 Object 类
     */
    public static void main(String[] args){
        Zi z1 = new Zi();
        Zi z2 = new Zi(10);

    }
}


class Fu{
    public Fu(){
        System.out.println("Fu类的空参构造方法");
    }

    public Fu(int num){
        System.out.println("Fu类的带餐构造方法...");
    }
}

class Zi extends Fu{

    public Zi(){
        // super();
        System.out.println("Zi类的空参构造方法");
    }

    public Zi(int num){
        // super();
        System.out.println("Zi类的带餐构造方法...");
    }
}

运行结果如下:

3.4 继承中的内存图解

3.5 继承的案例

package com.itheima.mcase;

import org.w3c.dom.ls.LSOutput;

public class Case {
    public static void main(String[] args) {

        Coder c = new Coder("张三",23,15000);
        Manager m = new Manager("李四", 24, 18000, 5000);
        c.work();
        m.work();

    }
}

class Employee{
    private String name;
    private int age;
    private double salary;

    public Employee(String name, int age, double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    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;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public void work(){
        System.out.println("员工工作...");
    }
}

class Coder extends Employee{
    public Coder(String name, int age, double salary) {
        super(name, age, salary);
    }

    @Override
    public void work() {
        System.out.println("姓名为"+super.getName()+", 年龄为"+super.getAge()+", 工资为"+super.getSalary()+"的程序员正在编写代码");
    }
}

class Manager extends Employee{
    private double bonus;

    public Manager(String name, int age, double salary, double bonus) {
        super(name, age, salary);
        this.bonus = bonus;
    }

    public double getBonus() {
        return bonus;
    }

    public void setBonus(double bonus) {
        this.bonus = bonus;
    }

    @Override
    public void work() {
        System.out.println("姓名为"+super.getName()+", 年龄为"+super.getAge()+", 工资为"+super.getSalary()+", 奖金为"+bonus+"的项目经理正在分配任务...");
    }
}

4. this 和 super

5. final关键字

  • 基本数据类型: 数据值不可改变
  • 引用数据类型: 地址值不可改变, 但是内容可以改变

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码里码理~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值