封装的特色:java
封装代码的实现:dom
Tips:只有getXXX方法的属性是只读属性;只有setXXX方法的属性是只写属性工具
Java封装的意义:测试
package Chapter10.Lecture1.cat; /** * 宠物猫类的封装 */ public class Cat { //1.修改属性的可见性--private,限定只能在当前类内被访问 private String name; //昵称 private int month; //年龄 public Cat(){ } //经过带参构造方法赋值 public Cat(int month) { this.setMonth(month); } //2. 建立getter/setter方法 public String getName() { return "我是一只叫" + name + "的宠物猫"; } public void setName(String name) { this.name = name; } public int getMonth() { return month; } public void setMonth(int month) { if (month <= 0) { System.out.println("信息输入错误,年龄必须大于0"); } else { this.month = month; } } }
package Chapter10.Lecture1.cat; public class CatTest { public static void main(String[] args) { //对象实例化 Cat one = new Cat(); //经过带参构造方法建立类 Cat two = new Cat(-2); //测试 //会输出属性month的初始值0 System.out.println("年龄:"+two.getMonth()); one.setName("花花"); System.out.println(one.getName()); one.setMonth(-1); if (one.getMonth() == 0) { return; } System.out.println("年龄:" + one.getMonth()); } }
package 包名;
,必须放在Java源文件中的第一行;一个Java源文件中只能有一个package语句导入包的方法:this
import 包名.*;
导入此包下的全部类import 包名.类名;
导入此包下的指定类包名.类名;
建议采用import 包名.类名;
的方式加载,提升效率code
Tips:加载类的顺序跟import导入语句的位置无关对象
import 包名.*;
只能访问到指定包名下的类,没法访问子包下的类java.lang | 包含Java语言的基础的类,该包系统加载时默认导入,如:System、String、Math |
---|---|
java.util | 包含Java语言中经常使用工具,如:Scanner、Random |
java.io | 包含输入、输出相关功能的类,如File、InputStream |
静态成员的访问方法:递归
推荐调用方式:类名.静态成员接口
package Chapter10.Lecture2.animal; public class Cat { //静态成员、类成员 public static int price; //售价 private String name; //昵称 private int month; //年龄 private double weight; //体重 private String species; //品种 { System.out.println("我是构造代码块"); } static { System.out.println("我是静态代码块"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public String getSpecies() { return species; } public void setSpecies(String species) { this.species = species; } public static void eat() { //静态方法中不能直接访问同一个类中的非静态成员,只能直接调用同一个类中的静态成员 //run(); //name = "小明"; //只能经过对象实例化后,对象.成员方法的方式访问非静态成员 Cat temp = new Cat(); temp.run(); temp.name = "小明"; //静态方法中不能使用this //this.name = "小明"; Cat.price = 100; System.out.println("小猫吃鱼"); } //在成员方法中,能够直接访问类中的静态成员 public void run() { { System.out.println("我是普通代码块"); } this.name = "凡凡"; Cat.price = 20; eat();//此行代码会致使栈溢出,注释掉后执行(缘由:递归调用) System.out.println("售价是" + Cat.price + "的" + this.name + "快跑"); } }
package Chapter10.Lecture2.test; import Chapter10.Lecture2.animal.Cat; public class Test { public static void main(String[] args) { Cat one = new Cat(); one.setName("花花"); one.setMonth(2); one.setSpecies("英国短毛猫"); //one.price = 2000; Cat.price = 2000; Cat two = new Cat(); two.setName("凡凡"); two.setMonth(1); two.setSpecies("中华田园猫"); //two.price = 150; Cat.price = 150; System.out.println(one.getName() + "的售价为:" + one.price); System.out.println(two.getName() + "的售价为:" + two.price); //one.eat(); Cat.eat(); System.out.println("----------------------"); one.run(); } }