Java基础入门:面向对象知识点补充

95 阅读3分钟

Java程序是如何编译运行的

  • 1.编译
    • ① 写源代码:Main.java
    • ② 编译:javac —— compiler
    • ③ Main.java → Main.class (字节码文件-bytecode)
  • 2.运行(JVM - java virtual machine)
    • ④ 类加载器(class loader),加载 Main.class
    • ⑤ 再运行
    • 整个运行命令:java Main.class

变量的分类与作用域

  • 变量分类

    • Class Variables (Static Fields) 类变量
    public class VariableScope {
        // 类变量的作用域:整个类到处都能用,只有一个,类加载的时候
        private static int number;
    
        public void test() {
            number = 100;
        }
    
        {
            number = 200;
        }
    
        public static void main(String[] args) {
            number = 300;
        }
    }
    
    • Instance Variables (Non-Static Fields) 实例变量
    public class VariableScope {
        // 实例变量的作用域:类里面能用(除static context方法外),但是实例没有了就不行了
        // 实例变量每创建一个实例就有一个
        private String name;
    
        private static void testStatic() {
            // 报错:'com.jirengu.java.oop.VariableScope.this' cannot be referenced from a static context
            // this.name = "CC";
        }
    
        {
            this.name = "DD";
        }
    
        // 报错:'com.jirengu.java.oop.VariableScope.this' cannot be referenced from a static context
        static {
            // this.name = "DD";
        }
    
        public void test() {
            this.name = "EE";
        }
    
        public static void main(String[] args) {
            VariableScope scope = new VariableScope();
            VariableScope scope1 = new VariableScope();
    
            scope.name = "AA";
            scope1.name = "BB";
        }
    }
    
    • Local Variables 局部变量 (局部变量仅对声明它们的方法可见)
    public class VariableScope {
        // 实例变量的作用域:类里面能用(除static context方法外),但是实例没有了就不行了
        // 实例变量每创建一个实例就有一个
        private String name;
    
        public static void testStatic() {
            // 局部变量的作用域:就是花括号内
            // 也就是说,花括号内的花括号,也能用外层花括号的局部变量
            int a = 100;
        }
    
        {
            this.name = "DD";
            int a = 100;
        }
    
        public void test() {
            this.name = "EE";
            int a = 100;
        }
    
        public static void main(String[] args) {
            VariableScope scope = new VariableScope();
            VariableScope scope1 = new VariableScope();
            int a = 100;
    
            scope.name = "AA";
            scope1.name = "BB";
        }
    }
    
    • Parameters 方法参数
  • 变量的作用域

内部类

  • 目的:更加细粒度的封装
  • 非静态内部类可以使用外部类的成员方法/属性
  • 静态内部类无法直接使用外部类的成员方法/属性
  • 分类:非静态内部类/静态内部类/匿名内部类
public class Outer {
    private int number;

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

    public void useInnerMethod() {
        Inner inner = new Inner();
        inner.innerMethod();
        System.out.println("Non-static inner object field: " + inner.getName());
    }

    public static void staticOuterMethod() {
        System.out.println("Static outer method");
    }

    private class Inner {
        public Inner(String name) {
            this.name = name;
        }

        public String getName() {
            return "JAVA";
        }

        private String name;
        public Inner() {
        }

        public void innerMethod() {
            // 内部类能调用外部类方法
            // 不能加this,加了就成调内部类的方法
            outerMethod();
            staticOuterMethod();
            System.out.println("Inner method");
        }
    }

    protected static class StaticInner {
        private Outer outer;

        public StaticInner(Outer outer) {
            this.outer = outer;
        }

        public void staticInnerMethod() {
            // 报错:Non-static method 'outerMethod()' cannot be referenced from a static context
            // outerMethod();
            staticOuterMethod();
            outer.outerMethod();

            System.out.println("Static inner method");
        }

        public static void main(String[] args) {
            Outer outer1 = new Outer();
            StaticInner staticInner = new StaticInner(outer1);
            staticInner.staticInnerMethod();
        }
    }

    public static void main(String[] args) {
        Outer outer = new Outer();
        outer.useInnerMethod();
    }
}

匿名内部类(Anonymous Class)

  • 没有名字的类,内部类的简化写法
  • 本质:继承该类或者实现接口的子类匿名对象
  • 用途:当某些方法用抽象类,或者接口作为参数,该方法仅调用一次,可以使用匿名内部类简化
public class TestDemo {
    public void collect(Bird bird) {
        System.out.println(bird);
    }

    public void show(Flyable flyable) {
        flyable.fly();
    }

    public static void main(String[] args) {
        TestDemo demo = new TestDemo();
        Bird bird = new Bird() {
            @Override
            public void fly() {
                System.out.println("Anonymous Bird is flying");
            }
        };
        demo.collect(bird);
        String name = "JAVA";
        Bird javaBird = new Bird(name) {
            @Override
            public void fly() {
                System.out.println("JAVA BIRD is flying");
            }
        };
        demo.show(javaBird);

        // Flyable flyableThing = () -> System.out.println("Something is flying");
        Flyable flyableThing = new Flyable() {
            @Override
            public void fly() {
                System.out.println("Something is flying");
            }
        };

        demo.show(flyableThing);
    }
}