zoukankan      html  css  js  c++  java
  • 什么是静态代码块?java中如何使用空参构造方法自动生成不同名字的对象,使用非静态的属性和静态属性有什么区别,原因是什么?如何理解static关键字

    静态代码块?类加载就执行,最先执行

    class demo{

    static int num;

    static{

    num=10;

    num*=3;

    System.out.println("haha");

    }

    static void show(){

    System.out.println("num="+num);

    }

    }

    空参构造自动生成对象时,使用非静态的属性

    代码:

    package com.swift;
    //使用无参构造方法自动生成对象,序号不断自增
    public class Person {
        private int count; //如果在定义类时,使用的是非静态的属性,则得到的结果都是相同的,都为1。因为这个count生命周期短,只在对象内部
        public int id;
        public String name;
        public int age;
        public String city;
        public Person() {
            super();
            count++;
            this.id=count;
            this.name="NoName-"+count;
            this.age=20;
            this.city="蜀国"+count;
            
        }
        public Person(int id ,String name,int age,String city) {
            this.id=id;
            this.name=name;
            this.age=age;
            this.city=city;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        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 String getCity() {
            return city;
        }
        public void setCity(String city) {
            this.city = city;
        }
        public String getInfo() {
            return "The Person is id=" + id + ", name=" + name + ", age=" + age + ", city=" + city ;
        }
        
    }

    结果:

    空参构造自动生成对象时,使用静态的属性

    代码:

    package com.swift;
    //使用无参构造方法自动生成对象,序号不断自增
    public class Person {
        private static int count; //如果在定义类时,使用的是静态的属性,则得到的结果是不同的。count生命周期长,与类相同
        public int id;
        public String name;
        public int age;
        public String city;
        public Person() {
            super();
            count++;
            this.id=count;
            this.name="NoName"+count;
            this.age=20;
            this.city="蜀国";
            
        }
        public Person(int id ,String name,int age,String city) {
            this.id=id;
            this.name=name;
            this.age=age;
            this.city=city;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        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 String getCity() {
            return city;
        }
        public void setCity(String city) {
            this.city = city;
        }
        public String getInfo() {
            return "The Person is id=" + id + ", name=" + name + ", age=" + age + ", city=" + city ;
        }
        
    }

    结果:

     Demo类

    package com.swift;
    
    public class DemoPerson {
    
        public static void main(String[] args) {
            new Person();
            new Person();
            new Person();
            new Person();
            new Person();
    //        Person p3=new Person();
    //        Person p4=new Person();
    //        p1.id=100001;
    //        p1.name="刘备";
    //        p1.age=40;
    //        p1.city="蜀国";
    //        p2.id=100002;
    //        p2.name="关羽";
    //        p2.age=35;
    //        p2.city="蜀国";
    //        p3.id=100003;
    //        p3.name="张飞";
    //        p3.age=33;
    //        p3.city="蜀国";
    //        p4.id=100004;
    //        p4.name="诸葛亮";
    //        p4.age=30;
    //        p4.city="蜀国";
    //        System.out.println(p1.getInfo());
    //        System.out.println(p2.getInfo());
    //        System.out.println(p3.getInfo());
    //        System.out.println(p4.getInfo());
        }
    
    }
  • 相关阅读:
    后端框架:SpringBoot+Mybatis+Dubbox(zookeeper+dubbo-admin)
    java代码执行mysql存储过程
    SpringBoot整合Junit
    摘要算法之MD5
    java如何控制下载的文件类型是txt还是doc?如何将文件名返回给前端?Content-disposition
    Android Atuido 连接模拟器
    verilog CRC 校验
    Altium Designer(AD软件)如何导出gerber文件
    SpringBoot集成mybatisplus
    C++ | 通过智能指针实现资源管理
  • 原文地址:https://www.cnblogs.com/qingyundian/p/7718503.html
Copyright © 2011-2022 走看看