java.封装
1.构造方法:可分为无参构造法和有参构造法。
无参构造法:
无参构造方法就是构造方法没有任何参数。构造方法在创建对象(new Dog())调用,无参构造方法中一般用于给属性赋值默认值。(出现了有参构造法,必须写上无参构造法。)
有参构造法:
当构造/实例化一个对象时,可以向构造方法中传递参数,这样的构造方法称为有参构造。
无参构造法 eg: 有参构造法 eg:
public class Dog { public class Dog{
String name; String name;
int health; int healt;
int love; int love;
String strain; String strain;
public Dog { public Dog (String name,int,health,int love,String strain)
System.out.println("无参构造法"); this. name=name;
health=100; this health;=health;
love=50; this love=love;
} this strain =strain;
} }
}
有参构造也可这样表示 public Dog(String _name,int _health,int _love,String _strain){ name = _name; health = _health; love = _love; strain = _strain; }
总结:
在开发过程中,如果开发者提供了有参构造方法,一定要习惯性的提供无参构造。
2.This关键字
this 是一个关键字,表示对象本身,本质上this中存有一个引用,引用对象本身。
this用于访问本对象属性,同时解决局部变量和成员变量同名的问题。
通过打印this中的引用,可以看出对象dog和this指向同一内存。
一般而言,dog用于类的外部,this用于类的内部。因为类的内部根本不知道dog变量名的存在。
3.方法的调用内存:
1.加载Class类到方法区。
2读取Class文件中定义的属性和方法,根据定义属性计算申请内存需要的字节。
3.new申请内存并初始化内存,赋值默认值=》this(地址)
优化方法代码
public void showInfo(){ System.out.print("我的名字叫"+this.name); System.out.print(",健康值"+this.health); System.out.print(",亲密度"+this.love); System.out.println(",我是一只"+this.strain); }
4.静态变量
static 修饰的变量称为静态变量/静态属性,形式: static 类型 变量名称 [=初始值}
static int count = 0;
静态变量归类所有,也叫类变量,访问方式
[1] 类名.静态变量(推荐)
[2] 对象.静态变量
public class Car{ String brand; String type; float price; static int count = 0; public Car(){ Car.count++; } public Car(String brand,String type,float price){ this.brand = brand; this.type = type; this.price = price; Car.count++; } public void showInfo(){ System.out.println("车辆信息:"); System.out.println("品牌:"+this.brand); System.out.println("型号:"+this.type); System.out.println("价格:"+this.price); System.out.println("我是第"+Car.count+"辆车"); } public static int getCarCount(){ // 在静态方法中访问实例变量 // System.out.println("品牌:"+brand); //showInfo(); //this.showInfo(); return Car.count; } }
总结
[1]实例方法可以访问静态成员。
[2]静态方法不能访问非静态成员。
5.类加载机制
Car car = new Car(…);
当实例化一个对象时,jvm首先把Car.class加载到方法区
[1]读取Car.class 根据声明的成员变量计算申请内存需要的字节数
[2]读取Car.class 中的静态成员,给静态变量分配空间并初始化。
new Car 申请内存得到一个car对象,此时才有对象的空间。showInfo才可以通过car对象调用。
6.This 关键字
this表示对象本身。 [1] this调用属性 [2] this调用方法 public Dog(String name,int health,int love,String strain){ this.setName(name); this.setHealth(health); this.setLove(love); this.setStrain(strain); // showInfo(); this.showInfo(); } [3] this调用本类的构造方法,形式 this(arg1,arg2,…) public Dog(){ } public Dog(String name,int health,int love){ this.setName(name); this.setHealth(health); this.setLove(love); } public Dog(String name,int health,int love,String strain){ //this.setName(name); //this.setHealth(health); //this.setLove(love); // this调用本类的其他构造方法 // System.out.println("test"); this(name,health,love); this.setStrain(strain); // showInfo(); //this.showInfo(); } 注意:this调用其他构造方法必须写到构造方法的第一句。
7.静态常量
在程序运行过程中,如果一个量的值不会发生改变,可以把该量声明为静态常量,用static final修饰。 public class Penguin{ private String name; private int health; private int love; private String gender; static final String SEX_MALE = "雄"; static final String SEX_FEMALE = "雌"; public void setName(String name){ this.name = name; } public String getName(){ return this.name; } public void setHealth(int health){ if(health>100 && health<1){ this.health = 60; System.out.println("健康值必须在1-100之间,默认为60"); }else{ this.health = health; } } public int getHealth(){ return this.health; } public void setLove(String love){ this.love = love; } public int getLove(){ return this.love; } public void setGender(String gender){ this.gender = gender; } public String getGender(){ return this.gender; } public Penguin(){ } public Penguin(String name,String gender){ this.setName(name); this.setGender(gender); } public Penguin(String name,int health,int love,String gender){ this(name,gender); this.setHealth(health); this.setLove(love); } public void showInfo(){ System.out.print("我的名字叫"+name); System.out.print(",健康值"+health); System.out.print(",亲密度"+love); System.out.println(",性别"+gender); } } public class Test02{ public static void main(String[] args){ Penguin penguin = new Penguin("大脚",100,0,Penguin.SEX_MALE); } }