xerga há 1 ano atrás
pai
commit
c3fac67d8d

+ 28 - 0
JavaSE/day09/src/com/lc/day09/final01/Person.java

@@ -0,0 +1,28 @@
+package com.lc.day09.final01;
+
+/**
+ * ClassName: Person
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/12/17 15:02
+ * @Version 1.0
+ */
+final public class Person { //修饰类 不能被继承
+    //修饰 属性 不能更改
+    public static final String country = "中国";
+
+    //修饰方法 不能重写
+    public final void method(){
+
+    }
+
+    public static void main(String[] args) {
+        //分开写可以
+        final  int age;
+        age = 10;
+
+        //age = 20;
+
+    }
+
+}

+ 16 - 0
JavaSE/day09/src/com/lc/day09/final01/Student.java

@@ -0,0 +1,16 @@
+package com.lc.day09.final01;
+
+/**
+ * ClassName: Student
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/12/17 15:05
+ * @Version 1.0
+ */
+public class Student /*extends Person*/{
+
+//    @Override
+//    public void method() {
+//
+//    }
+}

+ 52 - 0
JavaSE/day09/src/com/lc/day09/final01/Test.java

@@ -0,0 +1,52 @@
+package com.lc.day09.final01;
+
+/**
+ * ClassName: Test
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/12/17 15:15
+ * @Version 1.0
+ */
+public class Test {
+
+    public int finalMethod(final int x) {
+        //return ++x; // ++ 改变自身
+         return x + 1;  //那种会有问题 ?
+    }
+
+    public static void main(String[] args) {
+        Other o = new Other();
+        new Test().finalMethod(o);
+
+
+
+        //
+        System.out.println("-------------------------");
+
+
+        //写法1:
+        //final int c = 0;
+        for (int i = 0; i < 10; i++) {
+            //c = i;
+            //System.out.println(c);
+        }
+        //写法2:
+        for (int i = 0; i < 10; i++) {
+            final int c = i; //每次都被重新定义
+            System.out.println(c);
+        }
+
+
+        //
+        System.out.println("-------------");
+        Other other = new Other(); //0x1122
+        finalMethod(other);
+    }
+    public static void finalMethod(final Other o) { // o = 0x1122 属性 改变
+        // o = new Other(); // final 修饰的引用类型变量,不能重新赋值
+        o.i++;
+    }
+}
+class Other {
+    public int i;
+}

+ 28 - 0
JavaSE/day09/src/com/lc/day09/span01/Test01.java

@@ -0,0 +1,28 @@
+package com.lc.day09.span01;
+
+/**
+ * ClassName: Test01
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/12/17 11:42
+ * @Version 1.0
+ */
+public class Test01 {
+
+
+    public void method(){
+
+        int a = 0;
+
+        //限定 局部变量的范围
+        {
+            //限定 局部变量的范围
+            int b = 1;
+        }
+
+        System.out.println(a);
+        //System.out.println(b);
+
+    }
+
+}

+ 26 - 0
JavaSE/day09/src/com/lc/day09/span02/Person.java

@@ -0,0 +1,26 @@
+package com.lc.day09.span02;
+
+/**
+ * ClassName: Person
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/12/17 11:45
+ * @Version 1.0
+ */
+public class Person {
+
+    static String city;
+
+    //通过 多个步骤获取。 业务逻辑
+    // static {}
+    // 静态代码块
+    // 静态访问静态
+    // 第一次被使用  new class加载 在其他类中有声明 创建其他类 执行。
+    static {
+        city = "北京";
+
+        System.out.println("执行了。。。。");
+    }
+
+
+}

+ 19 - 0
JavaSE/day09/src/com/lc/day09/span02/Test.java

@@ -0,0 +1,19 @@
+package com.lc.day09.span02;
+
+/**
+ * ClassName: Test
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/12/17 11:48
+ * @Version 1.0
+ */
+public class Test {
+
+    public static void main(String[] args) throws ClassNotFoundException {
+        //加载
+        Class.forName("com.lc.day09.span02.Person");
+
+
+
+    }
+}

+ 36 - 0
JavaSE/day09/src/com/lc/day09/span03/Person.java

@@ -0,0 +1,36 @@
+package com.lc.day09.span03;
+
+/**
+ * ClassName: Person
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/12/17 11:45
+ * @Version 1.0
+ */
+public class Person {
+
+    String name;
+
+    static String city;
+
+
+    static {
+        System.out.println("静态代码块执行");
+    }
+
+    // 非静态代码块
+    // 非静态属性赋值
+    // 代码块 优先构造方法执行
+    // 每次构造方法执行完 都会执行。
+    {
+        System.out.println("代码块执行");
+    }
+
+    public Person() {
+        System.out.println("构造方法执行");
+    }
+
+    public Person( String name ) {
+        System.out.println("构造方法执行-有参数");
+    }
+}

+ 29 - 0
JavaSE/day09/src/com/lc/day09/span03/Test.java

@@ -0,0 +1,29 @@
+package com.lc.day09.span03;
+
+
+/**
+ * ClassName: Test
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/12/17 11:48
+ * @Version 1.0
+ */
+public class Test {
+
+    public static void main(String[] args) throws ClassNotFoundException {
+        //创建对象
+        Person person = new Person();
+        new Person();
+        new Person("坤坤");
+
+        System.out.println(person.name);
+
+        //执行顺序
+
+        // 静态代码块 加载执行。 优先于 代码块。
+        // 静态代码块 只会执行一次
+        // 代码块 执行 在构造方法前执行,每次都会执行。
+        // 构造 new 对象执行。
+
+    }
+}

+ 39 - 0
JavaSE/day09/src/com/lc/day09/span04/T.java

@@ -0,0 +1,39 @@
+package com.lc.day09.span04;
+
+/**
+ * ClassName: Test03
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/12/17 14:28
+ * @Version 1.0
+ */
+public class T {
+    // 静态优先
+    //
+    public static int k = 0;
+    public static T t1 = new T("t1");
+    public static T t2 = new T("t2");
+    public static int i = print("i");
+    public static int n = 99;
+    public int j = print("j");
+
+    {
+        print("构造块");
+    }
+    static{
+        print("静态块");
+    }
+    public T(String str){
+        System.out.println((++k) + ":" + str + "  i=" + i + "  n=" + n);
+        ++n;
+        ++i;
+    }
+    public static int print(String str){
+        System.out.println((++k) + ":" + str + "  i=" + i + "  n=" + n);
+        ++n;
+        return ++i;
+    }
+    public static void main(String[] args) {
+
+    }
+}

+ 45 - 0
JavaSE/day09/src/com/lc/day09/span04/Test01.java

@@ -0,0 +1,45 @@
+package com.lc.day09.span04;
+
+/**
+ * ClassName: Test01
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/12/17 13:49
+ * @Version 1.0
+ */
+public class Test01 {
+
+    public static void main(String[] args) {
+        new Son();
+        // 执行子类 保证父类被初始化。
+        // 初始化
+        //   new  实例化
+        //   类初始化。
+
+        //
+    }
+}
+
+class Father{
+    static{
+        System.out.println("(1)父类的静态代码块");
+    }
+    {
+        System.out.println("(2)父类的非静态代码块");
+    }
+    Father(){
+        System.out.println("(3)父类的无参构造");
+    }
+}
+class Son extends Father{
+    static{
+        System.out.println("(4)子类的静态代码块");
+    }
+    {
+        System.out.println("(5)子类的非静态代码块");
+    }
+    Son(){
+        super();
+        System.out.println("(6)子类的无参构造");
+    }
+}

+ 31 - 0
JavaSE/day09/src/com/lc/day09/span04/Test02.java

@@ -0,0 +1,31 @@
+package com.lc.day09.span04;
+
+/**
+ * ClassName: Test02
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/12/17 14:23
+ * @Version 1.0
+ */
+public class Test02 {
+    static int x, y, z; //初始值 0  x -1
+
+    static {
+        int x = 5;
+        x--;//就近 x = 5
+    }
+    static {
+        x--;
+    }
+    public static void main(String[] args) {
+        System.out.println("x=" + x); // -1
+        z--;
+        method();
+        System.out.println("result:" + (z + y + ++z));  // y = 0 z = 1  2
+    }
+    public static void method() {
+        y = z++ + ++z;
+        //  -1 ++ + ++1
+    }
+
+}

+ 23 - 0
JavaSE/day09/src/com/lc/day09/static01/Person.java

@@ -0,0 +1,23 @@
+package com.lc.day09.static01;
+
+/**
+ * ClassName: Person
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/12/17 9:48
+ * @Version 1.0
+ */
+public class Person {
+
+    String name;
+    int age;
+
+    // 用 static 修饰
+    // 使用 权限修饰符  static  成员变量类型  成员变量名。
+    static String city;
+
+    public Person(String name, int age ) {
+        this.name = name;
+        this.age = age;
+    }
+}

+ 34 - 0
JavaSE/day09/src/com/lc/day09/static01/Test01.java

@@ -0,0 +1,34 @@
+package com.lc.day09.static01;
+
+/**
+ * ClassName: Test01
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/12/17 9:47
+ * @Version 1.0
+ */
+public class Test01 {
+
+    public static void main(String[] args) {
+
+//        new Person("zs",22,"北京");
+//        new Person("ls",23,"北京");
+        Person p1 = new Person("zs",22);
+        Person p2 = new Person("ls",22);
+
+        //被static修饰 类 成员变量
+        //不推荐 对象操作
+        //通过类操作
+        Person.city = "北京"; //加载的时候 静态区域  比对象要早。
+
+        //查看
+        System.out.println(p1.city); // 北京
+        System.out.println(p2.city); // 北京
+
+
+        p1.city = "上海";
+        // static 被所有对象共有。
+        System.out.println(p2.city);
+        System.out.println(p1.city);
+    }
+}

+ 48 - 0
JavaSE/day09/src/com/lc/day09/static02/Person.java

@@ -0,0 +1,48 @@
+package com.lc.day09.static02;
+
+/**
+ * ClassName: Person
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/12/17 9:48
+ * @Version 1.0
+ */
+public class Person {
+
+    String name;
+    int age;
+
+    // 用 static 修饰
+    // 使用 权限修饰符  static  成员变量类型  成员变量名。
+    static String city;
+
+    public Person(String name, int age ) {
+        this.name = name;
+        this.age = age;
+    }
+
+    //使用
+    //权限修饰符后面 static
+    //类 方法
+    public static void show(){
+        //输出信息
+
+        //静态的 优先于对象。
+        //静态的方法 只能静态变量。 非静态不能访问
+        System.out.println("父类 city = " + city);
+    }
+
+    //重载
+    public static void a(int a){
+
+    }
+    public static void a(int a ,int b){
+
+    }
+
+    //重写
+    // 静态方法不能被重写,子类中的只是 子类的静态方法。
+
+    // this 和 对象。
+
+}

+ 24 - 0
JavaSE/day09/src/com/lc/day09/static02/Student.java

@@ -0,0 +1,24 @@
+package com.lc.day09.static02;
+
+/**
+ * ClassName: Student
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/12/17 10:01
+ * @Version 1.0
+ */
+public class Student extends Person{
+    public Student(String name, int age) {
+        super(name, age);
+    }
+
+    //@Override
+    public static void show(){
+        //输出信息
+
+        //静态的 优先于对象。
+        //静态的方法 只能静态变量。 非静态不能访问
+        System.out.println("子类 city = " + city);
+    }
+
+}

+ 24 - 0
JavaSE/day09/src/com/lc/day09/static02/Test01.java

@@ -0,0 +1,24 @@
+package com.lc.day09.static02;
+
+/**
+ * ClassName: Test01
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/12/17 9:47
+ * @Version 1.0
+ */
+public class Test01 {
+
+    public static void main(String[] args) {
+        //静态方法
+        Person.city = "北京";
+
+        Person.show();
+
+        //方法
+        Person.show();
+        //可以继承
+        Student.show();
+
+    }
+}

+ 60 - 0
JavaSE/day09/src/com/lc/day09/static03/Rectangle.java

@@ -0,0 +1,60 @@
+package com.lc.day09.static03;
+
+/**
+ * ClassName: Rectangle
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/12/17 10:56
+ * @Version 1.0
+ */
+public class Rectangle extends Shape{
+
+    double width;
+    double height;
+
+    public Rectangle() {
+    }
+
+    public Rectangle(double width, double height) {
+        this.width = width;
+        this.height = height;
+    }
+
+    @Override
+    public double area() {
+
+        return width*height;
+    }
+
+    @Override
+    public double perimeter() {
+        return (width+height)*2;
+    }
+
+
+
+
+    public double getWidth() {
+        return width;
+    }
+
+    public void setWidth(double width) {
+        this.width = width;
+    }
+
+    public double getHeight() {
+        return height;
+    }
+
+    public void setHeight(double height) {
+        this.height = height;
+    }
+
+    @Override
+    public String toString() {
+        return "width=" + width +
+                ", height=" + height +
+                ", perimeter=" + perimeter() +
+                ", area=" + area() ;
+    }
+}

+ 24 - 0
JavaSE/day09/src/com/lc/day09/static03/Shape.java

@@ -0,0 +1,24 @@
+package com.lc.day09.static03;
+
+/**
+ * ClassName: Shape
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/12/17 10:54
+ * @Version 1.0
+ */
+public class Shape {
+
+    public double area(){
+        return 0.0;
+    }
+
+    public double perimeter(){
+        return 0.0;
+    }
+
+    @Override
+    public String toString() {
+        return "area:"+area() + "perimeter:"+perimeter();
+    }
+}

+ 36 - 0
JavaSE/day09/src/com/lc/day09/static03/ShapeTools.java

@@ -0,0 +1,36 @@
+package com.lc.day09.static03;
+
+/**
+ * ClassName: ShapeTools
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/12/17 11:03
+ * @Version 1.0
+ */
+public class ShapeTools {
+    public static int compare(Shape s1, Shape s2){
+        int i = 0;
+        if (s1.area() > s2.area()){
+            i = 1;
+        }else if ( s1.area() == s2.area() ){
+            i = 0;
+        }else {
+            i = -1;
+        }
+        return i;
+    }
+
+    public static void sort(Shape[] arr){
+        for (int i = 0; i < arr.length-1; i++) {
+            for (int j = 0; j < arr.length-1-i; j++) {
+                if (arr[j].area() > arr[j+1].area()){
+
+                    //Shape
+                    Shape temp = arr[j];
+                    arr[j] =  arr[j+1];
+                    arr[j+1] = temp;
+                }
+            }
+        }
+    }
+}

+ 41 - 0
JavaSE/day09/src/com/lc/day09/static03/Test.java

@@ -0,0 +1,41 @@
+package com.lc.day09.static03;
+
+/**
+ * ClassName: Test
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/12/17 11:10
+ * @Version 1.0
+ */
+public class Test {
+    public static void main(String[] args) {
+
+        Rectangle r1 = new Rectangle(11, 20);
+        Rectangle r2 = new Rectangle(10, 20);
+
+        Triangle triangle = new Triangle(2, 3, 2);
+        Triangle triangle1 = new Triangle(3, 3, 3);
+        Triangle triangle2 = new Triangle(2, 3, 3);
+
+        //静态方法
+        int compare = ShapeTools.compare(r1, r2);
+        System.out.println(compare);
+
+        //排序
+        Shape[] shapes = new Shape[3];
+        shapes[0] = triangle;
+        shapes[1] = triangle1;
+        shapes[2] = triangle2;
+
+        System.out.println(triangle.area());
+        System.out.println(triangle1.area());
+        System.out.println(triangle2.area());
+
+        ShapeTools.sort(shapes);
+
+        for (int i = 0; i < shapes.length; i++) {
+            System.out.println(shapes[i].toString());
+        }
+
+    }
+}

+ 67 - 0
JavaSE/day09/src/com/lc/day09/static03/Triangle.java

@@ -0,0 +1,67 @@
+package com.lc.day09.static03;
+
+/**
+ * ClassName: Triangle
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/12/17 10:58
+ * @Version 1.0
+ */
+public class Triangle extends Shape{
+
+    private int a,b,c;
+
+    public Triangle() {
+    }
+
+    public Triangle(int a, int b, int c) {
+        if ( a+b > c || a+c > b || b+c > a){
+            this.a = a;
+            this.b = b;
+            this.c = c;
+        }else{
+            System.out.println("提示 :三角形两边之和不能大于第三边 ");
+        }
+
+    }
+
+    @Override
+    public double area() {
+        double p = (double) (a+b+c) /2;
+        return Math.sqrt(p * (p - a) * (p - b) * (p - c));
+    }
+
+    @Override
+    public double perimeter() {
+        return a+b+c;
+    }
+
+    @Override
+    public String toString() {
+        return super.toString() + "a=" + a + ",b=" + b + ",c=" + c;
+    }
+
+    public int getA() {
+        return a;
+    }
+
+    public void setA(int a) {
+        this.a = a;
+    }
+
+    public int getB() {
+        return b;
+    }
+
+    public void setB(int b) {
+        this.b = b;
+    }
+
+    public int getC() {
+        return c;
+    }
+
+    public void setC(int c) {
+        this.c = c;
+    }
+}