13、声明static属性
static修饰的属性为公共属性,一个对象修改,所有对象都会被修改
应该由类来进行访问,而不是对象,可以由类名来调用
static 属性不受类实例化限制,不实例化也可以调用
进行类设计时首选非static属性,涉及公共信息才使用static
全局数据区 栈内存 堆内存 static 数据 person1 -> {name, age} person1 -> {name, age}
class Person { String name; static String country; public Person(String name){ this.name = name; } public static void main(String[] args) { Person person1 = new Person("张三"); Person person2 = new Person("李四"); Person.country = "中国"; System.out.println(person1.country); System.out.println(person2.country); // 中国 中国 } }
14、声明static方法
static方法只允许调用static属性或static方法
static属性和方法都可以在没有实例化对象的前提下使用
应用场景:
回避实例化对象调用并且描述公共属性的情况
15、static应用案例
实现实例化对象个数统计
class Person { static int count; public Person(){ count ++; System.out.println("第 " + count + " 个实例"); } public static void main(String[] args) { new Person(); new Person(); new Person(); /* 第 1 个实例 第 2 个实例 第 3 个实例 */ } }
16、普通代码块
代码块:由"{}"定义的结构
分类:
1、普通代码块
2、构造代码块
3、静态代码块
4、同步代码块(多线程)
普通代码块
定义在方法中的代码块
// 普通代码块 { int age = 12; System.out.println(age); // 12 } int age = 15; System.out.println(age); // 15
17、构造代码块
构造块优先于构造方法执行,每次实例化都会调用构造代码块
class Person { public Person(){ System.out.println("构造方法"); } { System.out.println("构造代码块"); } public static void main(String[] args) { new Person(); new Person(); /** 构造代码块 构造方法 构造代码块 构造方法 */ } }
18、静态代码块
static关键字定义的代码块
静态代码块优先于构造函数执行,不管实例化多少次只会执行一次
主要目的是为了静态属性初始化
静态代码块优先于主方法先执行
class Person { public Person(){ System.out.println("构造方法"); } { System.out.println("构造代码块"); } static { System.out.println("静态代码块"); } public static void main(String[] args) { System.out.println("主方法代码块"); new Person(); new Person(); /** 静态代码块 主方法代码块 构造代码块 构造方法 构造代码块 构造方法 / } }
19、案例分析一(Address)
实现一个地址类,包含国家,省份,城市,街道,邮政编码
实现一个简单java类
class Address { private String country; private String province; private String city; private String street; private String zipcode; // setter public void setCountry(String country){ this.country = country; } public void setProvince(String province){ this.province = province; } public void setCity(String city){ this.city = city; } public void setStreet(String street){ this.street = street; } public void setZipcode(String zipcode){ this.zipcode = zipcode; } // getter public String getCountry(){ return this.country; } public String getProvince(){ return this.province; } public String getCity(){ return this.city; } public String getStreet(){ return this.street; } public String getZipcode(){ return this.zipcode; } // info public String getInfo(){ return "国家: " + this.country + ", 省份: " + this.province + ", 城市: " + this.city + ", 街道: " + this.street + ", 邮政编码: " + this.zipcode; } public Address(String country, String province, String city, String street, String zipcode){ this.country = country; this.province = province; this.city = city; this.street = street; this.zipcode = zipcode; } public static void main(String[] args) { Address address = new Address("中国", "北京", "朝阳", "大望路", "10001"); System.out.println(address.getInfo()); // 国家: 中国, 省份: 北京, 城市: 朝阳, 街道: 大望路, 邮政编码: 10001 } }
20、案例分析二(Employee)
实现一个员工类,包含编号,姓名,薪水,税率,还包括薪水增长计算和增长后的工资
class Employee{ private long no; private String name; private double salary; private double rate; // setter getter ... public String getInfo(){ return "编号:" + this.no + ", 姓名: " + this.name + ", 薪水 " + this.salary + ", 涨幅: " + this.rate; } public void increaseSalary(){ this.salary = this.salary * (1 + this.rate); } public Employee(long no, String name, double salary, double rate){ this.no = no; this.name = name; this.salary = salary; this.rate = rate; } public static void main(String[] args){ Employee employee = new Employee(1L, "张三", 3000.0, 0.3); System.out.println(employee.getInfo()); // 编号:1, 姓名: 张三, 薪水 3000.0, 涨幅: 0.3 employee.increaseSalary(); System.out.println(employee.getInfo()); // 编号:1, 姓名: 张三, 薪水 3900.0, 涨幅: 0.3 } }
21、案例分析三(Dog)
创建Dog类,有名字,颜色,年龄,定义构造方法初始化属性
class Account{ private String name; private double balance; public Account(String name){ this(name, 0.0); } public Account(String name, double balance){ this.name = name;; this.balance = balance;; } // 查询余额 public double getBalance(){ return this.balance; } public static void main(String[] args) { Account account = new Account("张三", 2000.0); System.out.println(account.getBalance()); // 2000.0 } }